In this post under Java, I will show with example the purpose of static “addAll” method in “Collections” class. The “addAll” method populates a collection with the given list of elements. This method takes two arguments1) the collection that it has to fill or populate2) variable length arument that will contain the elements that has…… Continue reading Populating collection with multiple elements in one single method call
Tag: Collections
Collections frequency method
In this post under Java, I will show with example the purpose of “frequency” static method in “Collections” class. The “frequency” method returns the number of times an element is repeated in a collection. This method takes two argument1) the collection it has to search2) the element for which it has to return the frequency.…… Continue reading Collections frequency method
Shuffling elements of a list
In this post under Java Collections, I will show with example how to randomly shuffle elements of a list. Below is the complete code 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 ShuffleListDemo { 8 public static void main(String[] args) {…… Continue reading Shuffling elements of a list
Swapping elements of a list
In this post under Java Collections, I will explain with example how to swap elements of an list. Below is the complete main code 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 SwappingListDemo { 8 public static void main(String[] args) {…… Continue reading Swapping elements of a list
Reverse the order of elements in the list
In this post under Java Collections, I will show with example how to reverse the elements order in a list. Below is the complete code 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 ReverseListDemo { 8 public static void main(String args[])…… Continue reading Reverse the order of elements in the list
Collections disjoint method
In this post under Java Collections, I will show with example the purpose of Collections class static “disjoint” method. This method compares two collections provided as input and returns true if both the collections have no elements in common. For our example, we will use the below main class. Main class 1 package core.collection; 2…… Continue reading Collections disjoint method
Converting a map to immutable map
In this post under Java Collections, I will show with example how to obtain an immutable map. Below is the complete main code for your reference Main class 1 package core.collection; 2 3 import java.util.Collections; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 public class ImmutableMapDemo { 8 public static void main(String[] args) { 9…… Continue reading Converting a map to immutable map
Converting a collection to immutable collection
In this post under Java Collections, I will show with example how to create immutable collections. Below is the complete code for your reference. Main class 1 package core.collection; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.Collections; 6 import java.util.List; 7 8 public class ImmutableCollectionDemo { 9 public static void main(String args [])…… Continue reading Converting a collection to immutable collection
Creating a synchronized map
In this post under Java Collections, I will explain with example how to create synchronized map. Below is the complete code for your reference Main class 1 package core.collection; 2 3 import java.util.Collections; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 public class SyncMapDemo { 8 public static void main(String args []) { 9 Map<Integer,…… Continue reading Creating a synchronized map
Creating a synchronized collection
In this post under Java Collections, I will show with example how to create a synchronized collection. Below is the complete main code for your reference. 1 package core.collection; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.Collections; 6 import java.util.List; 7 8 public class SyncCollectionDemo { 9 public static void main(String[] args) {…… Continue reading Creating a synchronized collection