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.

Sort part array elements sort()

On sorting of array elements, three important and often used methods exist with Array class. They are discussed in this tutorial "Sort part array elements sort()" in easy terms, example and screenshot. Sorting array elements with sort() Sorting array elements with sort() and Comparator Sorting few array elements with sort() After seeing the first two …

Sort part array elements sort() Read More »

ArrayList of arrays Java

Sometimes it may be necessary to store arrays in ArayList in coding. It is also necessary to retrieve the elements of the arrays when required. Another similar concept program is available "Array of Array Lists" that stores array lists in an array of array list. Example on ArrayList of arrays import java.util.*; public class Demo …

ArrayList of arrays Java Read More »

Java Convert Array to ArrayList

Java Convert Array ArrayList Summary: It is quiet common in Java coding to convert array to array list and vice versa. 4 styles are given in simple terms, examples in this tutorial "Java Convert Array ArrayList". Following are the methods signature used Java Convert Array ArrayList. 1. Defined in Array class static List asList(T array): …

Java Convert Array to ArrayList Read More »

Java ArrayList to Array

Java ArrayList to Array Summary: After seeing the 4 styles of converting array elements into array list, now let us do the opposite way of converting "Java ArrayList to Array". Note: There are two methods, asList() and toArray(), that bridges collection classes (data structures) and array. These methods are defined in Arrays class and Collection …

Java ArrayList to Array Read More »

Java Shuffle Array shuffle()

Java collections framework comes with two classes Collections and Arrays (from java.util) to operate on the elements of collection classes (data structures) and arrays. To shuffle the elements of a data structure, the Collections class includes a method shuffle() but Arrays class does not come with one. To shuffle the elements of an array, we …

Java Shuffle Array shuffle() Read More »

Java Array vs ArrayList

Java Array vs ArrayList Summary: This tutorial "Java Array vs ArrayList", tables you the differences between array and arraylist at a glance to remember easily. Both array and ArrayList are well familiar and used by the programmer very often. Both are interoperable – array to arraylist and arraylist to array. Following table gives the differences …

Java Array vs ArrayList Read More »

Sorting complete array elements sort()

Arrays.sort() method sorts all the array elements in ascending order. It avoids writing tedious sorting algorithms. Two methods exist with Array class for sorting. Following are the method signatures. static void sort(int array1[]): Sorts all the elements of the array array1 in ascending numerical order. The sorting algorithm used is Quicksort (Mergesort) with Collections.sort()). The …

Sorting complete array elements sort() Read More »

Sorting array elements sort() Comparator

The earlier sort() example sorts implicitly in natural ascending order. The order can be customized with the overloaded sort() method that takes a Comparator object as parameter. Two methods exist for sorting array elements with Arrays class. Following are the method signatures. static void sort(int array1[]): Sorts all the elements of the array array1 in …

Sorting array elements sort() Comparator Read More »

Compare arrays equality equals()

Arrays.equals() method compares two arrays and tests whether they have the same elements are not. Returns a boolean value of true if they come with the same elements else false. Here, both arrays should have the same elements in the same order. If the order changes, the method returns false (even though they contain same …

Compare arrays equality equals() Read More »

Convert array to List with asList()

Arrays.asList() converts all the elements of the array into a List. It is the easiest way to pass the elements of an array to a List; an interoperability feature between arrays and collections framework classes. Convert array to List with asList() Following is the method signature static List asList(T array): It is useful to convert …

Convert array to List with asList() Read More »

Get hashcode with hashCode()

About Hashcode Generally, a programmer writes a lengthy for loop to compare two strings for their equality, checking each character in both the strings. It is a time consuming process and performance goes down especially when the strings are lengthy. To overcome this, Java designers introduced hash code. It is an integer number that can …

Get hashcode with hashCode() Read More »

Search array element binarySearch()

Arrays.binarySearch() method is useful to find out the index number of an element in the array. Two methods exist in the Arrays class for the purpose and following are their method signatures static int binarySearch(int array1[], int searchNum): Searches the element searchNum in the array array1. This method should be used in combination with sort() …

Search array element binarySearch() 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 »