In this post under Java Collections, I will show with example how to convert an array into its string representation. In Java, any object when converted to its string representation, it calls the object’s “toString” method. The default behavior of “toString” method is to print the below information getClass().getName() + ‘@’ + Integer.toHexString(hashCode()) In case…… Continue reading Arrays toString method
Category: Core
Sorting Array
In this post under Java, I will show with example how to sort an array. Below is the complete main method for reference. Main class package core.collection; import java.util.Arrays; public class ArraysSortDemo { public static void main(String[] args) { int[] integerList = new int[] {5, 2, 3, 1, 4}; Arrays.sort(integerList); for(int value : integerList) {…… Continue reading Sorting Array
Converting Array to Stream
In this post under Java Collections, I will show with example how to convert an existing array to Stream. For this purpose, Java provides static method named “stream” in Arrays class. This method takes an array as an input and convert it into a stream. Below is the complete main code for your reference Main…… Continue reading Converting Array to Stream
Arrays copyOfRange method example
In this post under Java collections, I will show with example the purpose of “Arrays” class “copyOfRange” method. In the previous post under Java collections, I showed with example the purpose of “Arrays” class “copyOf” method. This method copies all the elements of input array to destination array. But what if we want to copy…… Continue reading Arrays copyOfRange method example
Arrays.copyOf method
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
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