In this post under Java Collections, I will explain with example the purpose of “fill” static method in “Collections” utility class. This method takes any list as input and replace all the elements of the list with user given object. So if we have a list named “entries” containing below elements [1, 2, 3, 4,…… Continue reading List Fill demo
Tag: Collections
List nCopies demo
In this post under Java Collections, I will explain with example the purpose of “nCopies” static method in “Collections” utility class This method creates a list containing n copies of input object. Where n and input object are user specified. So basically this method takes two arguments.1) The object to be replicated in the list2)…… Continue reading List nCopies demo
List rotation demo
In this post under Java Collections, I will explain with example how to rotate elements in a list. Java “Collections” utility class has static method named “rotate” which will rotate elements in a list. This method takes two arguments.1) the list2) integer representing the distance Below is the complete main class for your reference Main…… Continue reading List rotation demo
Populating the entire array with different elements
In this post under Java Collections, I will show with example an api provided in “Arrays” class that will populate an array with different element. We are talking about “Arrays” class static method “setAll”. It takes two arguments1) the array to be populated2) a function that will generate the elements that are to be inserted…… Continue reading Populating the entire array with different elements
Populating the entire array with same element
In this post under Java Collections, I will show with example an api provided in “Arrays” class that will populate any array with a particular element. We are talking about “Arrays” class static method “fill”. It takes two arguments1) the array to be populated2) the element to be fill int array For our example I…… Continue reading Populating the entire array with same element
Arrays toString method
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
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