Searching an array for a particular element

In this post under Java Collections, I will show with example how to search an array for a specific element. Please note there is a precondition that must be satisfied before searching an array. The precondition is that the array should already be sorted. Java “Arrays” class provides “binarySearch” method which does the searching. This…… Continue reading Searching an array for a particular element

Arrays to List conversion

In this post under Java Collections, I will explain with example how to convert an array to list. Please note this conversion is possible if the array is a collection of objects and not primitive elements. To achieve our goal, we use “Arrays” class static method “asList”. This method returns a list backed by the…… Continue reading Arrays to List conversion

Converting Collection to unmodifiable Set

In this post under Java, I will show with example how to convert a collection to unmodifiable set. To convert a collection to an immutable Set, we take help of “Set” interface static method “copyOf”. Below is the complete code for your reference. Main class 1 package core.collection; 2 3 import java.util.ArrayList; 4 import java.util.List;…… Continue reading Converting Collection to unmodifiable Set

Converting Collection to unmodifiable List

In this post under Java, I will show with example how to convert a collection to unmodifiable list. To convert a collection to an immutable List, we take help of “List” interface static method “copyOf”. Below is the complete code for your reference. Main class 1 package core.collection; 2 3 import java.util.HashSet; 4 import java.util.List;…… Continue reading Converting Collection to unmodifiable List

Iterating a list in both direction

In this post under Java, I will show with example how to iterate a list in both directions. For iterating in both direction, we will take the help of “ListIterator” class. We get an instance of “ListIterator” by calling “listIterator” method on a list. Below is the code snippet, where “integerList” is a list of…… Continue reading Iterating a list in both direction

Sorting a list

In this post under Java, I will show with example how to sort a list. Below is the complete main class for your reference Main class 1 package core.collection; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 public class SortingList { 8 public static void main(String []args) { 9 List<Integer> integerList…… Continue reading Sorting a list

Collections checkedCollection example

In this post under Java, I will explain with example the purpose of Collections “checkedCollection” method. From Java 5 onwards, we started using Generics to ensure compile time type safety. As a result of which we were unable to add element of one data type to collections of elements of another data type. At compile…… Continue reading Collections checkedCollection example

Collections indexOfSubList method

In this post under Java Core, I will explain with example the pupose of Collections’ “indexOfSubList” method. Lets say they are two lists list1 and list2. “indexOfSubList” will search in list1 for position from where the elements of “list2” are repeated in “list1”. Once the position is found it is returned as index. In other…… Continue reading Collections indexOfSubList method

Collections replaceAll method

In this post under Java Collections, I will explain with example the purpose of Collections “replaceAll” method. This method replaces all occurrences of a particular element with its replacements. This method can be used only with “List” data structure. Below is the main class showing an example Main class 1 package core.collection; 2 3 import…… Continue reading Collections replaceAll method

Java Comparable interface

In this post under Java Collections, I will explain with example the purpose of “Comparable” interface. Before we understand “Comparable” interface we need to understand the difference between “ordered” and “sorted” collections. In Ordered collections, elements are placed in a collection according to insertion order or according to some order and the order is not…… Continue reading Java Comparable interface