Collection Framework in Java

Collection Framework in Java,The Java collections framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures.

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 »

Java Rotate elements with rotate()

The Collections algorithms includes a typical method rotate() that rotates the elements in cyclic order. After the last few elements, the first elements can be moved. This method is useful to change the order of elements. Note, it is not swapping or shuffling. In shuffling, elements are moved in between randomly. But here, only the …

Java Rotate elements with rotate() Read More »

Find Min Max values with min() max()

There comes two methods from Collections class to find the elements with minimum and maximum values of a data structure. These methods can be applied to any Collection class (subclasses of Collection interface). In the following code ArrayList is used. Following are the method signatures. static Object min(Collection col1): Returns the element with the minimum …

Find Min Max values with min() max() Read More »

Copy one list to another with copy()

The Collections.copy() method takes two list objects as parameters. The first one is the destination list and the second is the source list. The source list elements are copied into target list. Here, copy operation does actually replacement. Following is the method signature static void copy(List destination1, List source1): Copies all the elements of List …

Copy one list to another with copy() Read More »

Java Filling List elements with fill()

With the Collections fill() static method (Collections methods are known as algorithms), a data structure can be filled up (or replaced) with one element. After this method call, the data structure will have the same element. Following is the method signature static void fill(List list1, Object obj1): Replaces all the elements of list1 with obj1. …

Java Filling List elements with fill() Read More »

Printing array elements with toString()

Arrays.toString() method makes it easier to print the elements of an array, forgetting the tedious iteration with for loops. Following is the method signature static String toString(int array1[]): Returns the array in string form useful to be displayed in a text field. This display the array elements comma-separated and placed within square brackets, "[]". Returns …

Printing array elements with toString() Read More »

Java Swap two elements with swap()

The Collections method swap() is very useful to swap two values in a programming code. For example, the prices of two different dates can be swapped. This method comes into existence with JDK 1.4. Following is the method signature of swap() method static void swap(List list1, int index1, int index2): List list1 elements at index …

Java Swap two elements with swap() Read More »

Shuffle elements randomly with shuffle()

The shuffle() method of Collections class shuffles the elements randomly. The same method shuffle() called multiple times gives different order of elements. The algorithm is developed by designers with some randomness basing on a seed value. Shuffling is useful in game development. For example, using shuffle() method, the playing cards can be shuffled. This method …

Shuffle elements randomly with shuffle() Read More »

Java Search element with binarySearch()

There comes binarySearch() method to search for an element in a data structure. The name is derived so because the searching algorithm followed by the designers is binary search. binarySearch() is overloaded that takes the List object and the element to be searched as parameters; and the other using a Comparator object. Following are the …

Java Search element with binarySearch() Read More »

Sort with Collections sort() Comparator

Using Comparator, the sorting algorithm of Java can be customized. sort() method is overloaded that takes a Comparator object as parameter. Two methods exist, with Collections class, for sorting elements. Following are the signatures. static void sort(List list1): Sorts the list list1 into ascending order according to the natural sequence of the elements. static void …

Sort with Collections sort() Comparator Read More »

Sort ArrayList with Collections sort()

Collections algorithms makes a programmer's work easier and avoids lots of simple, but extra code. It is proved in each method of Collections class. The sorting algorithm places the elements in ascending order, known as natural order (a before b and 1 before 2). Internally, the data structure elements are converted into an array and …

Sort ArrayList with Collections sort() Read More »

Comparator Example Java

After knowing the fundamentals of interface Comparator, let us make a small example. ArrayList elements are printed in reverse order using Comparator. Example code on Comparator Example Java import java.util.*; public class ComparatorExample { public static void main(String args[]) { ArrayList stones = new ArrayList(); stones.add(“pearl”); stones.add(“diamond”); stones.add(“ruby”); stones.add(“emerald”); stones.add(“topaz”); Comparator comp1 = Collections.reverseOrder(); System.out.println(“Default …

Comparator Example Java Read More »