-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionFramework.java
146 lines (109 loc) · 4.04 KB
/
CollectionFramework.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class CollectionFramework {
//always use parameterized types for collection framework
public static void main(String[] args) {
/*ARRAY LIST*/
ArrayList<String> words = new ArrayList<String>();
/*Faster for retrieval, slower for manipulation*/
//Adding items
words.add("Hello"); words.add("there"); words.add("sir"); words.add("69"); words.add("420");
//size --> words.size()
//Removing items
words.remove(1);
//System.out.println(words);
System.out.println(words.get(0) + " " + words.get(1));
for(String value : words) {
System.out.println(value + " " + "okay");
}
/*LINKED LIST*/
LinkedList<Integer> ll = new LinkedList<Integer>();
/*Slower for retrieval, faster for manipulation*/
for(int i = 0; i < 10; i++) {
ll.add(i*10);
}
ll.remove(2);
ll.remove();//null --> removes the first data in the list
ll.remove();
System.out.println(ll);
/*SET DS*/
/*HASH SET*/ //Unordered
HashSet<Integer> val = new HashSet<Integer>();
val.add(12);
val.add(43);
val.add(67);
val.add(100);
val.add(12);
val.add(67);
System.out.println(val);//unsorted hashset
List<Integer> list = new ArrayList<Integer>(val);
Collections.sort(list);
System.out.println(list);//sortedhashset
/*LINKED HASH SET*/ //Ordered
LinkedHashSet<Integer> val1 = new LinkedHashSet<Integer>();
val1.add(12);
val1.add(43);
val1.add(67);
val1.add(100);
val1.add(12);
val1.add(67);
//val1.clear();
boolean has = val1.contains(67);
System.out.println(has);
System.out.println(val1);
/*HASH MAP*/ //Unordered
HashMap<String, String> dictionary = new HashMap<String, String>(); //two data types <K, V>
//entry sets
dictionary.put("Red", "a bright color somewhat resembling a tomato.");
dictionary.put("Brave", "ready to face and endure danger or pain; showing courage.");
dictionary.put("Joy", "a feeling of great pleasure and happiness.");
dictionary.put("Confidence", "the feeling or belief that one can have faith in or rely on someone or something.");
for(String word: dictionary.keySet()) {
System.out.println(word);
}
for(String values: dictionary.values()) {
System.out.println(values);
}
/*LINKED HASH MAP*/ //Ordered
LinkedHashMap<String, String> dictionary1 = new LinkedHashMap<String, String>(); //two data types <K, V>
//entry sets
dictionary1.put("Red", "a bright color somewhat resembling a tomato.");
dictionary1.put("Brave", "ready to face and endure danger or pain; showing courage.");
dictionary1.put("Joy", "a feeling of great pleasure and happiness.");
dictionary1.put("Confidence", "the feeling or belief that one can have faith in or rely on someone or something.");
for(String word1: dictionary1.keySet()) {
System.out.println(word1);
}
for(String values1: dictionary1.values()) {
System.out.println(values1);
}
//displaying both keys and values
System.out.println(dictionary1);//method 1
for(Map.Entry<String, String> entry: dictionary.entrySet()) {
System.out.println(entry);//method 2
}
/*TREE MAP*/ //Displays the keys in natural order (numerically/lexicographically)
TreeMap<String, String> tree = new TreeMap<String, String>(); //two data types <K, V>
//entry sets
tree.put("Brave", "null");
tree.put("Red", "a bright color somewhat resembling a tomato.");
tree.put("Brave", "ready to face and endure danger or pain; showing courage.");
tree.put("Joy", "a feeling of great pleasure and happiness.");
tree.put("Confidence", "the feeling or belief that one can have faith in or rely on someone or something.");
for(String word: tree.keySet()) {
System.out.println(word);
}
for(String values: tree.values()) {
System.out.println(values);
}
System.out.println(tree);
}
}