Collections Section Index | Page 2
The arraycopy method of System allows you to do this but... starting in Java 6, there is a new copyOf() method added to the Arrays class that is slightly more flexible.
In order to insert an element into a List add(int index, E element) must be used since the List interface does not provide an insert method. If the index is out of rage ie. index < 0 || index &...more
First things first. Where can Stacks and Queue's be found? Java provides a Stack class, which can be found within the java.util namespace. Within the same namespace you will find a Queue interf...more
nCopies(int n, T o) can be used to create an immutable List which contains a specific number of copies of an Object. Care must be taken when trying to add elements to or extract elements from the...more
Finding the maximum element within a Collection is easy. The following method can be used which can be found within the Collections class. public static <T extends Object & Comparable<? s...more
The easiest way to obtain a Map Entry or (key-value pair) is by invoking the following method provided by the Map interface. Set<Map.Entry<K,V>> entrySet(); The entrySet() metho...more
Is there a way determine how many times an Object occurs within a Collection?
Unmodifiable Collections can be easily created using various static methods which the Collections class provides. Any attempts to modify the returned Collection, whether direct or via its iterat...more
The Map interface defines a method named keySet() which concrete classes such as HashMap and TreeMap implement. Depending on the implementation on which keySet() is invoked the returned Set migh...more
In order to traverse a List backwards a ListIterator must be used. The List interface provides a method, which returns a ListIterator. ListIterator<E> listIterator() Using a returned L...more
Two methods are needed in this case. boolean containsAll(Collection<?> c) boolean disjoint(Collection<?>c1 Collection<?>c2) Since containsAll(Collection<?> c) is define...more
The Collections class which can be found within the java.util namespace provides two methods which suffle the elements of a Collection. static void shuffle(List<?> list) static void shuffl...more
One of the biggest additions since the creation of the Collections framework is Generics.This long awaited update to the type system is a welcomed feature, which C++ developers have had in their ...more
When taking a peek at the Java docs you will notice certain classes implement an interface named Comparable. Take a look at some of the subclasses of Number such as Byte, Integer, Long, Float or ...more
Every concrete implementation provides a constructor, which takes a Collection as an argument. Care must be taken when creating a Collection based another Collection, this is because depending o...more