Collections

ArrayList vs LinkedList

There comes two classes ArrayList and LinkedList to store objects. Internally, ArrayList stores elements as an array form and LinkedList stores elements in node form. Due to their internal style of storing the elements, they come with different performance heads depending the nature of action performed like addition and retrieval. Programs are available at ArrayList …

ArrayList vs LinkedList Read More »

List Extract Common Elements Java

It may be required sometimes to extract the common elements of two lists in Java. Given in simple terms in this tutorial List Extract Common Elements Java. Following code on List Extract Common Elements Java illustrates. import java.util.*; public class CommonElements { public static void main(String args[]) { ArrayList al1 = new ArrayList(); ArrayList al2 …

List Extract Common Elements Java Read More »

Reversing elements with reverse()

To reverse the elements of a data structure, there comes a method reverse() from Collections class. The method reverses all the elements in the original data structure itself. That is, after this method call, the original order in the list itself changes. Following is the method. static void reverse(List list1): Existing order of the elements …

Reversing elements with reverse() Read More »

Immutable Collection unmodifiableCollection()

The collection classes (data structures), we make, are generally modifiable. That is, elements can be added, deleted or replaced at any time. But sometimes, it may be required to have read-only collection classes where any modification to the collection cannot be done. For this, the Collections class (remember, Collection and Collections, both are different) comes …

Immutable Collection unmodifiableCollection() Read More »

Thread safe Collection synchronizedCollection()

The methods of legacy classes (the data structure that comes with JDK 1.0) like Vector and Hashtable are implicitly synchronized. They are best suitable for thread-safe operations. An operation is said to be thread-safe when multiple threads access the data, the data should not get corrupted or result in inconsistency. It is the precaution to …

Thread safe Collection synchronizedCollection() Read More »

Type safe Collection checkedCollection()

JDK 1.5 adds a feature called generics to JDK 1.2 collection classes. With generics, it is possible to add only one type of data to a data structure. But the legacy classes, introduced with JDK 1.0, like Vector or Hashtable do not have this opportunity. To overcome this, the Collections class comes with checkedCollection() method …

Type safe Collection checkedCollection() Read More »

Enumerated Collection enumeration()

With legacy classes (JDK 1.0 DS) we use Enumeration. With collections framework classes we use Iterator or ListIterator. As such, with collection classes it is not possible to use Enumeration. To overcome this, the Collections class comes with enumeration() method. It returns collection class elements as an Enumeration. It is a good example on the …

Enumerated Collection enumeration() Read More »

Enumeration to ArrayList with list(Enumeration)

After seeing the interoperability between legacy and collections classes with "Enumerated Collection with enumeration()", let us see one more interoperability feature with list(Enumeration e). With this Enumeration object, legacy classes (JDK 1.0 DS are known as legacy classes) can be converted into ArrayList. For example, the Vector elements can be converted into ArrayList elements. Following …

Enumeration to ArrayList with list(Enumeration) Read More »

Immutable List with nCopies()

The nCopies() method of Collections class returns an immutable object of List. The immutable object cannot be modified. It is read-only. If tried to modify, the JVM throws UnsupportedOperationException. Following is the method signature static List nCopies(int n1, Object obj1): Returns an immutable list containing n1 elements of the object obj1. That is add() method …

Immutable List with nCopies() Read More »

How many times element occurs with frequency()

In a data structure (that accepts duplicate elements), an element can exist any number of times as in the case of ArrayList etc. The frequency() method is useful to find out how many times the same element exist in a data structure. Following is the method signature static int frequency(Collection col1, Object obj1): Checks how …

How many times element occurs with frequency() Read More »

Common Elements Existence disjoint()

The method, disjoint(), of Collections class returns true if the two data structures under comparison does not have any elements in common. At least, one element is common in the both, it returns false. disjoint() method is very useful to compare (to know the existence of common elements) two lists or maps or sets. Following …

Common Elements Existence disjoint() Read More »

Adding Array to Collections addAll()

Generally, in realtime, the names of the share holders etc, the programmer stores in the form an array. This array can be converted into a data structure with addAll() method of Collections class. addAll() method can also be used with a single element also. Following is the method signature static boolean addAll(Collection col1, Object obj1): …

Adding Array to Collections addAll() Read More »

Singleton List with singletonList()

What is singleton? With singleton class, you can create only one object. If another object is tried to create, the JVM throws UnsupportedOperationException. A Singleton class contains only one element. If tried to add a second element, the add() method throws UnsupportedOperationException. Singleton class limits the developer from instantiating more than one object. This idea …

Singleton List with singletonList() Read More »

Find Sublist Index with indexOfSubList()

The Collections class comes with many static methods (known as algorithms) of which two are used to know whether a few elements of a list are available in another list. If available, with these methods it can be known at what index they are available. The following is the signature of these two methods which …

Find Sublist Index with indexOfSubList() Read More »

Java Replace elements with replaceAll()

The replaceAll() method of Collections algorithms is useful to replace an element in the list. The element may appear any number of times in the list and in all the places it is replaced with the new value. Following is the method signature static boolean replaceAll(List list1, Object oldObj, Object newObj): Replaces the old element …

Java Replace elements with replaceAll() Read More »