Collections Section Index | Page 2
How do I make a copy of an array?
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.
How can I insert an element into a List?
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
What is the difference between a Stack and a Queue?
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
How can I create an immutable List consisting of n Copies of an Object?
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
How can I find the maximum element contained within a Collection?
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
What is the easiest way to obtain a Map Entry?
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?
Is there a way determine how many times an Object occurs within
a Collection?
How can I create a read only 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
How can I get a sorted list of keys that are contained within a Map?
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
How can I traverse a List backwards?
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
How can i tell if two Collections contain the same elements or have no elements in common?
Two methods are needed in this case.
boolean containsAll(Collection<?> c)
boolean disjoint(Collection<?>c1 Collection<?>c2)
Since containsAll(Collection<?> c) is define...more
How can I shuffle the elements of a Collection?
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
What are Generics and how can I use them?
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
How can I define my own Comparable type so that it can be naturally sorted within a List?
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
How can I create a Collection based on another Collection?
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