1
+ package com .cdac .collections .collectionsutils ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .Collections ;
5
+ import java .util .HashMap ;
6
+ import java .util .HashSet ;
7
+ import java .util .List ;
8
+ import java .util .Map ;
9
+ import java .util .Set ;
10
+
11
+ public class CollectionsDemo {
12
+
13
+ public static void main (String [] args ) {
14
+ ArrayList <String > list = new ArrayList <>();
15
+ list .add ("One" );
16
+ list .add ("Two" );
17
+ list .add ("One" );
18
+
19
+ Set <String > set = new HashSet <>();
20
+ set .add ("Three" );
21
+ set .add ("Four" );
22
+ set .add ("One" );
23
+
24
+ System .out .println (set );
25
+ set .addAll (list );
26
+ System .out .println (set );
27
+
28
+ Collections .sort (list );
29
+ System .out .println ("Sort : " +list );
30
+ Collections .sort (list , Collections .reverseOrder ());
31
+ System .out .println ("Reverse Order : " +list );
32
+
33
+ String max = Collections .max (list );
34
+ System .out .println ("Max : " +max );
35
+ String min = Collections .min (list );
36
+ System .out .println ("Min : " +min );
37
+
38
+ List <Integer > iList = new ArrayList <>();
39
+ iList .add (10 );
40
+ iList .add (20 );
41
+ iList .add (40 );
42
+ iList .add (30 );
43
+
44
+ int index = Collections .binarySearch (iList , 190 );
45
+ System .out .println ("Binary Search index is : " +index );
46
+
47
+ ArrayList <Integer > newList = new ArrayList <>();
48
+ newList .add (11 );
49
+ newList .add (12 );
50
+ newList .add (13 );
51
+ newList .add (14 );
52
+ newList .add (15 );
53
+ Collections .copy (newList , iList ); //newList should have the same size as src list
54
+ System .out .println ("Copied List : " +newList );
55
+
56
+ Set <String > emptySet = Collections .emptySet ();
57
+ System .out .println ("Empty Set : " +emptySet .size ());
58
+ //emptySet.add("Try"); //not allowed its immutable
59
+
60
+ Collections .replaceAll (iList , 10 , 100 );
61
+ System .out .println (iList );
62
+
63
+ Collections .shuffle (iList );
64
+ System .out .println (iList );
65
+
66
+ Set <String > singletonSet = Collections .singleton ("Java" );
67
+ System .out .println (singletonSet );
68
+ // singletonSet.add("Hello"); //not supported its immutable
69
+
70
+ Map <Integer , String > map = new HashMap <>();
71
+ map = Collections .synchronizedMap (map );
72
+ }
73
+ }
0 commit comments