Series: class Collections

Operations on data structures elements with methods of class Collections explained in simple terms

Java Collections API Methods

A small confusion comes to a novice at this juncture between collection and collections. Both are very different. Collection is an interface and Collections is a class, both from java.util package. Collection is the root interface from which almost all data structures are derived, commonly known as collection framework. Collections class contains many static methods …

Java Collections API Methods 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 »

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 »

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 »

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 »

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 »

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 »

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 »

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 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 »

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 »

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 »

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 »

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 »

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 »

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 »

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 »