GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java-Collection/DataStructureDemo.java at main · anykms729/Java-Collection Expand file tree/ Copy pathDataStructureDemo.javaMore file actions 196 lines (158 loc) · 6.21 KB / Copy pathDataStructureDemo.javaFile metadata and controls196 lines (158 loc) · 6.21 KB 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 package DataStructure; import javax.swing.*; import java.util.*; public class DataStructureDemo { public static void main(String[] args) { setInterface(); } // Collection // List(I) : Implemented by ArrayList(C), LinkedList(C) // Set(I) : Implemented by Hashset(C), LinkedHashSet(C) // Queue(I) : Implemented by PriorityQueue(C), LinkedList(C) public static void setInterface(){ // Duplicate(X), Insertion Order(x), Hashcode concept used when inserting data, Heterogeneous (0) HashSet hashSet = new HashSet(); hashSet.add(100); hashSet.add("Welcome"); hashSet.add(16.4); System.out.println(hashSet); System.out.println(hashSet.contains("Welcome")); System.out.println(hashSet.isEmpty()); for (Object e:hashSet){ System.out.println(e); } Iterator it = hashSet.iterator(); while (it.hasNext()){ System.out.println(it.next()); } HashSet<Integer> evenNumber = new HashSet<Integer>(); evenNumber.add(2); evenNumber.add(4); evenNumber.add(6); System.out.println("Hashset: "+evenNumber); HashSet<Integer> numbers = new HashSet<Integer>(); numbers.addAll(evenNumber); // Operation available: Union, Intersection, difference HashSet<Integer> set1 = new HashSet<Integer>(); HashSet<Integer> set2 = new HashSet<Integer>(); set1.add(1); set1.add(2); set1.add(3); set1.add(4); set2.add(2); set2.add(3); set2.add(4); // Union set1.addAll(set2); // Intersection set1.retainAll(set2); System.out.println("Intersections: "+set1); // Difference set1.removeAll(set2); System.out.println("Difference: "+set1); // Subset (all elements of set2 are contained in set1) System.out.println(set1.containsAll(set2)); } public static void queueInterface(){ // FIFO // Method // add(): Return true or Exception & offer(): Return true or null value // element(): Return head element if head is empty, return Exception & peak(): Return null value // remove() & poll() // child interface of queue interface // Deque(I) // BlockingQueue(I) // Implementation 2 Classes // PriorityQueue(C) - Insertion Order(O), Duplicate(O), Heterogeneous (X) // LinkedList(C) - Insertion Order(O), Duplicate(O), Heterogeneous (O) LinkedList linkedList = new LinkedList(); linkedList.add("A"); linkedList.add("B"); linkedList.add("C"); linkedList.add(100); System.out.println("Linkedlist is: "+linkedList); PriorityQueue priorityQueue = new PriorityQueue(); priorityQueue.add("A"); priorityQueue.add("B"); priorityQueue.add("C"); priorityQueue.add("C"); // priorityQueue.add(100); System.out.println(priorityQueue); // [A, B, C, C] System.out.println(priorityQueue.element()); // A System.out.println(priorityQueue.peek()); // A System.out.println(priorityQueue.poll()); // remove head System.out.println(priorityQueue); // [B, C, C] Iterator itr = priorityQueue.iterator(); while (itr.hasNext()){ System.out.println(itr.next()); } for (Object obj:priorityQueue){ System.out.println(obj); } } public static void hashMap(){ // Underlying DS is Hashtable // Insertion order(X) // n ull key allowed once, multiple null value is allowed // HashMap m = new HashMap(); HashMap <Integer, String> m= new HashMap<Integer,String>(); m.put(101, "John"); m.put(102, "David"); m.put(103, "Mary"); m.put(104, "Miso"); System.out.println(m.get(103)); System.out.println(m.containsKey(103)); System.out.println(m.isEmpty()); System.out.println(m.keySet()); System.out.println(m.values()); System.out.println(m.entrySet()); for(Object i:m.keySet()){ System.out.println(i+" "+m.get(i)); } for(Object i:m.values()){ System.out.println(i); } for(Map.Entry entry:m.entrySet()){ System.out.println(entry.getKey()+" "+entry.getValue()); } Set s =m.entrySet(); Iterator itr = s.iterator(); while (itr.hasNext()){ Map.Entry entry= (Map.Entry) itr.next(); System.out.println(entry.getKey()+" "+entry.getValue()); } } public void hashTable(){ // Hashtable(Non synchronized)& Hashmap = Map Interface Implementation class, both have same data structure "Hashtable" // Default capacity is 11, load factor 0.75 // Hashtable t = new Hashtable(initial capacity, fill ration/load factor); Hashtable<Integer, String> t = new Hashtable<Integer, String>(); t.put(101, "Miso"); t.put(102, "Biso"); t.put(103, "Ciso"); t.put(104, "Diso"); // No Null key value is allowed System.out.println(t); System.out.println(t.get(103)); // Ciso t.remove(101); System.out.println(t.containsKey(101)); System.out.println(t.containsValue("Ciso")); System.out.println(t.isEmpty()); System.out.println(t.keySet()); // [102,103,104] returned as set object System.out.println(t.values()); // [Diso, Ciso, Biso] returned as collection object for (int k:t.keySet()){ System.out.println(k+" "+t.get(k)); } for (Map.Entry entry:t.entrySet()) // entry = key + value set { System.out.println(entry.getKey()+" "+entry.getValue()); } // To use Iterator Set s = t.entrySet(); Iterator itr = s.iterator(); while (itr.hasNext()){ Map.Entry entry= (Map.Entry) itr.next(); System.out.println(entry.getKey()+" "+entry.getValue()); } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |