File tree 3 files changed +225
-156
lines changed
3 files changed +225
-156
lines changed Original file line number Diff line number Diff line change
1
+
2
+ import java .util .HashSet ;
3
+ import java .util .Set ;
4
+ import java .util .TreeSet ;
5
+
6
+ public class Main {
7
+
8
+ public static void main (String [] args ) {
9
+
10
+ // Set demonstration using HashSet
11
+
12
+ Set <Integer > hashSet = new HashSet <>();
13
+ hashSet .add (23 );
14
+ hashSet .add (4 );
15
+ hashSet .add (4 );
16
+ hashSet .add (4 );
17
+ hashSet .add (10 );
18
+
19
+ for (int element : hashSet ) {
20
+ System .out .println (element + " " );
21
+ }
22
+
23
+ // hashSet.isEmpty(); // returns true, if Set is empty
24
+ // hashSet.contains(10); // returns true, if the element is found
25
+ // hashSet.remove(23); // returns true, if the element was deleted
26
+ // hashSet.clear(); // Deletes all element
27
+
28
+ System .out .println ();
29
+
30
+ Set <Integer > treeSet = new TreeSet <>();
31
+ treeSet .add (23 );
32
+ treeSet .add (4 );
33
+ treeSet .add (4 );
34
+ treeSet .add (4 );
35
+ treeSet .add (10 );
36
+ treeSet .add (1 );
37
+
38
+ for (int element : treeSet ) {
39
+ System .out .println (element + " " );
40
+ }
41
+ }
42
+ }
43
+
44
+ /*
45
+ * Set: Interface
46
+ * HashSet: Implementation
47
+ * TreeSet: Implementation [sorted]
48
+ *
49
+ * Properties:
50
+ * 1. Unordered Collection
51
+ * 2. Cannot store duplicate elements
52
+ * 3. It has more implementation such as HashSet, TreeHashSet and TreeSet
53
+ *
54
+ * TreeSet contains elements in Sorted Order
55
+ * */
Original file line number Diff line number Diff line change
1
+
2
+
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+
6
+ public class Main {
7
+
8
+ public static void main (String [] args ) {
9
+
10
+ /* KEY : VALUE
11
+
12
+ * "red" : "apple"
13
+ * "yellow" : "banana"
14
+ * "white" : "radish"
15
+ * "green" : "apple"
16
+ * */
17
+
18
+ Map <String , String > fruits = new HashMap <>();
19
+ fruits .put ("red" , "apple" );
20
+ fruits .put ("yellow" , "banana" );
21
+ fruits .put ("white" , "radish" );
22
+ fruits .put ("green" , "apple" );
23
+
24
+ // fruits.containsKey("red"); // returns true, if key is found
25
+ // fruits.containsValue("apple"); // returns true, if value is found
26
+ // fruits.size(); // returns the size of the MAP
27
+ // fruits.remove("red"); // Deletes the Entry whose key is "red"
28
+ // fruits.clear();
29
+
30
+ System .out .println (fruits .get ("red" ));
31
+
32
+ for (Map .Entry pairEntry : fruits .entrySet ()) {
33
+ System .out .println (pairEntry .getKey () + " : " + pairEntry .getValue ());
34
+ }
35
+ }
36
+ }
37
+
38
+ /*
39
+ * Map: Interface
40
+ * HashMap: Class that implements interface Map
41
+ *
42
+ * class HashMap implements Map {
43
+ * ...
44
+ * }
45
+ *
46
+ * Map Properties:
47
+ * 1. They contain values based on key
48
+ * 2. They are not ordered
49
+ * 3. "KEY" should be unique
50
+ * 4. "VALUE" can be duplicate
51
+ * */
You can’t perform that action at this time.
0 commit comments