In this post under Java Collections, I will explain with example the purpose and how to use “Arrays” class public static method “copyOf”. Arrays.copyOf method takes a source array, creates a destination array, and copies the elements present in source array to destination array. Lets see an eample. Below is the main class Main class…… Continue reading Arrays.copyOf method
Tag: Java
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
Custom Canonical constructor in Record
In the previous post under Java Record, I mentioned how the compiler creates a long (canonical) constructor automatically. In this post, I will explain how to create a custom canonical constructor. Now why we need to provide a custom canonical constructor when it is already provided by compiler. The canonical constructor created by compiler will…… Continue reading Custom Canonical constructor in Record
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