Skip to content

Commit 527d01e

Browse files
authored
Java Interfcae implemented by various classes
List(I) : Implemented by ArrayList(C), LinkedList(C) Set(I) : Implemented by Hashset(C), LinkedHashSet(C) Queue(I) : Implemented by PriorityQueue(C), LinkedList(C)
0 parents  commit 527d01e

1 file changed

Lines changed: 196 additions & 0 deletions

File tree

DataStructureDemo.java

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package DataStructure;
2+
import javax.swing.*;
3+
import java.util.*;
4+
5+
public class DataStructureDemo {
6+
public static void main(String[] args) {
7+
setInterface();
8+
}
9+
10+
// Collection
11+
// List(I) : Implemented by ArrayList(C), LinkedList(C)
12+
// Set(I) : Implemented by Hashset(C), LinkedHashSet(C)
13+
// Queue(I) : Implemented by PriorityQueue(C), LinkedList(C)
14+
15+
public static void setInterface(){
16+
// Duplicate(X), Insertion Order(x), Hashcode concept used when inserting data, Heterogeneous (0)
17+
HashSet hashSet = new HashSet();
18+
hashSet.add(100);
19+
hashSet.add("Welcome");
20+
hashSet.add(16.4);
21+
22+
System.out.println(hashSet);
23+
System.out.println(hashSet.contains("Welcome"));
24+
System.out.println(hashSet.isEmpty());
25+
26+
for (Object e:hashSet){
27+
System.out.println(e);
28+
}
29+
30+
Iterator it = hashSet.iterator();
31+
while (it.hasNext()){
32+
System.out.println(it.next());
33+
}
34+
35+
HashSet<Integer> evenNumber = new HashSet<Integer>();
36+
37+
evenNumber.add(2);
38+
evenNumber.add(4);
39+
evenNumber.add(6);
40+
41+
System.out.println("Hashset: "+evenNumber);
42+
43+
HashSet<Integer> numbers = new HashSet<Integer>();
44+
numbers.addAll(evenNumber);
45+
46+
// Operation available: Union, Intersection, difference
47+
HashSet<Integer> set1 = new HashSet<Integer>();
48+
HashSet<Integer> set2 = new HashSet<Integer>();
49+
50+
set1.add(1);
51+
set1.add(2);
52+
set1.add(3);
53+
set1.add(4);
54+
55+
set2.add(2);
56+
set2.add(3);
57+
set2.add(4);
58+
59+
// Union
60+
set1.addAll(set2);
61+
62+
// Intersection
63+
set1.retainAll(set2);
64+
System.out.println("Intersections: "+set1);
65+
66+
// Difference
67+
set1.removeAll(set2);
68+
System.out.println("Difference: "+set1);
69+
70+
// Subset (all elements of set2 are contained in set1)
71+
System.out.println(set1.containsAll(set2));
72+
}
73+
74+
public static void queueInterface(){
75+
// FIFO
76+
77+
// Method
78+
// add(): Return true or Exception & offer(): Return true or null value
79+
// element(): Return head element if head is empty, return Exception & peak(): Return null value
80+
// remove() & poll()
81+
82+
// child interface of queue interface
83+
// Deque(I)
84+
// BlockingQueue(I)
85+
86+
// Implementation 2 Classes
87+
// PriorityQueue(C) - Insertion Order(O), Duplicate(O), Heterogeneous (X)
88+
// LinkedList(C) - Insertion Order(O), Duplicate(O), Heterogeneous (O)
89+
90+
LinkedList linkedList = new LinkedList();
91+
linkedList.add("A");
92+
linkedList.add("B");
93+
linkedList.add("C");
94+
linkedList.add(100);
95+
96+
System.out.println("Linkedlist is: "+linkedList);
97+
98+
PriorityQueue priorityQueue = new PriorityQueue();
99+
priorityQueue.add("A");
100+
priorityQueue.add("B");
101+
priorityQueue.add("C");
102+
priorityQueue.add("C");
103+
// priorityQueue.add(100);
104+
105+
System.out.println(priorityQueue); // [A, B, C, C]
106+
System.out.println(priorityQueue.element()); // A
107+
System.out.println(priorityQueue.peek()); // A
108+
System.out.println(priorityQueue.poll()); // remove head
109+
System.out.println(priorityQueue); // [B, C, C]
110+
111+
Iterator itr = priorityQueue.iterator();
112+
while (itr.hasNext()){
113+
System.out.println(itr.next());
114+
}
115+
116+
for (Object obj:priorityQueue){
117+
System.out.println(obj);
118+
}
119+
}
120+
121+
public static void hashMap(){
122+
// Underlying DS is Hashtable
123+
// Insertion order(X)
124+
// n ull key allowed once, multiple null value is allowed
125+
126+
// HashMap m = new HashMap();
127+
HashMap <Integer, String> m= new HashMap<Integer,String>();
128+
m.put(101, "John");
129+
m.put(102, "David");
130+
m.put(103, "Mary");
131+
m.put(104, "Miso");
132+
System.out.println(m.get(103));
133+
System.out.println(m.containsKey(103));
134+
System.out.println(m.isEmpty());
135+
System.out.println(m.keySet());
136+
System.out.println(m.values());
137+
System.out.println(m.entrySet());
138+
139+
for(Object i:m.keySet()){
140+
System.out.println(i+" "+m.get(i));
141+
}
142+
for(Object i:m.values()){
143+
System.out.println(i);
144+
}
145+
for(Map.Entry entry:m.entrySet()){
146+
System.out.println(entry.getKey()+" "+entry.getValue());
147+
}
148+
149+
Set s =m.entrySet();
150+
Iterator itr = s.iterator();
151+
while (itr.hasNext()){
152+
Map.Entry entry= (Map.Entry) itr.next();
153+
System.out.println(entry.getKey()+" "+entry.getValue());
154+
}
155+
}
156+
157+
public void hashTable(){
158+
// Hashtable(Non synchronized)& Hashmap = Map Interface Implementation class, both have same data structure "Hashtable"
159+
160+
// Default capacity is 11, load factor 0.75
161+
// Hashtable t = new Hashtable(initial capacity, fill ration/load factor);
162+
Hashtable<Integer, String> t = new Hashtable<Integer, String>();
163+
t.put(101, "Miso");
164+
t.put(102, "Biso");
165+
t.put(103, "Ciso");
166+
t.put(104, "Diso");
167+
// No Null key value is allowed
168+
System.out.println(t);
169+
System.out.println(t.get(103)); // Ciso
170+
t.remove(101);
171+
System.out.println(t.containsKey(101));
172+
System.out.println(t.containsValue("Ciso"));
173+
System.out.println(t.isEmpty());
174+
System.out.println(t.keySet()); // [102,103,104] returned as set object
175+
System.out.println(t.values()); // [Diso, Ciso, Biso] returned as collection object
176+
177+
for (int k:t.keySet()){
178+
System.out.println(k+" "+t.get(k));
179+
}
180+
181+
for (Map.Entry entry:t.entrySet()) // entry = key + value set
182+
{
183+
System.out.println(entry.getKey()+" "+entry.getValue());
184+
}
185+
186+
// To use Iterator
187+
Set s = t.entrySet();
188+
Iterator itr = s.iterator();
189+
190+
while (itr.hasNext()){
191+
Map.Entry entry= (Map.Entry) itr.next();
192+
System.out.println(entry.getKey()+" "+entry.getValue());
193+
}
194+
}
195+
196+
}

0 commit comments

Comments
 (0)