├── README.md ├── collection-interface.png └── linked-list.png /README.md: -------------------------------------------------------------------------------- 1 | # COLLECTİON AND MAP INTERFACE 2 | 3 | # Collection Interface 4 | 5 | - [ ] Classes that inherit from the Collection interface are gathered under the "java.util" package. 6 | 7 | --- 8 | 9 | ✔️Functions that subclasses deriving from the collection interface must provide: 10 | 11 | - int size(): Returns the number of elements of the dataset. 12 | - boolean isEmpty(): Returns true if there are no elements in the dataset. 13 | - boolean contains(Object element): Indicates whether there is a searched element in the data set as true or false. 14 | - Iterator iterator(): Returns the object to navigate through the individual elements in the dataset. 15 | - Object[] toArray(): Returns the dataset as an array. 16 | - boolean add(E e): Allows adding elements to the dataset. 17 | - boolean remove(Object element): Allows deleting elements from dataset. 18 | - boolean addAll(Collection extends E elements): Adds another collection type dataset to the existing dataset in its entirety. 19 | - void clear(): Deletes all elements in the dataset. 20 | - boolean removeAll(Collection elements): Deletes the given set of elements from the current dataset. 21 | 22 | ![collection-interface.png](https://github.com/zehracakir/CollectionAndMapInterfaceJava/blob/main/collection-interface.png) 23 | 24 | ## COLLECTION 25 | 26 | ### SET INTERFACE 27 | 28 | - The Set interface does not allow the same elements to be found again in the dataset. 29 | - **Declaration:** The Set interface is declared as: 30 | 31 | ```java 32 | public interface Set extends Collection 33 | ``` 34 | 35 | **Creating Set Objects** 36 | 37 | ```java 38 | Set hSet = new HashSet<>(); 39 | ``` 40 | 41 | - Example 42 | 43 | ```java 44 | import java.util.HashSet; 45 | import java.util.Iterator; 46 | import java.util.Set; 47 | 48 | public class SetClass { 49 | public static void main(String[] args) { 50 | Set hSet= new HashSet<>(); 51 | 52 | //add element 53 | hSet.add("Zehra Çakır"); 54 | hSet.add("Yusuf"); 55 | hSet.add("Kerem Ege"); 56 | hSet.add("Hatice"); 57 | hSet.add("Fatma"); 58 | hSet.add("Çakır"); 59 | 60 | //size 61 | System.out.println("Size: "+hSet.size()); 62 | 63 | //isEmpty 64 | System.out.println("isEmpty "+hSet.isEmpty()); 65 | 66 | //Iterator 67 | Iterator iterator=hSet.iterator(); 68 | System.out.println("***With Iterator:***"); 69 | while (iterator.hasNext()){ 70 | System.out.println(iterator.next()); 71 | } 72 | 73 | //remove 74 | hSet.remove("Çakır"); 75 | System.out.println("***Then remove***"); 76 | for (String a:hSet) { 77 | System.out.println(a); 78 | } 79 | 80 | //contains 81 | System.out.println("Contains "+ hSet.contains("Zehra Çakır")); 82 | 83 | //clear 84 | hSet.clear(); 85 | System.out.println("***Then clear***"); 86 | for (String a:hSet) { 87 | System.out.println(a); 88 | } 89 | 90 | } 91 | } 92 | ``` 93 | 94 | ### HashSet Class 95 | 96 | - The objects that we insert into the HashSet do not guarantee to be inserted in the same order. The objects are inserted based on their hashcode. 97 | - This class also allows the insertion of NULL elements. 98 | 99 | ### LinkedHashSet Class 100 | 101 | - It is an ordered version of HashSet that keeps a doubly linked List among all elements. This class is used when iteration order needs to be maintained. 102 | - While iterating over a HashSet, the order is unpredictable, LinkedHashSet allows us to iterate over the elements in the order in which they were added. 103 | 104 | ### TreeSet Class 105 | 106 | - It behaves like a simple set, except that it stores the elements in a sorted format. TreeSet uses a tree data structure for storage. Objects are stored in sequential, ascending order. 107 | - However, we can iterate in descending order using the TreeSet.descendingIterator() method. 108 | 109 | ```java 110 | //Comparator: Provides the ability to sort objects by multiple data members 111 | @Override 112 | public int compare(Student o1,Student o2){ 113 | return 0; 114 | } 115 | 116 | //Comparable: The compareTo() method compares two strings lexicographically. 117 | @Override 118 | public int compareTo(Object o1){ 119 | return this.age-((Employee)o).age; 120 | } 121 | ``` 122 | 123 | ## LIST INTERFACE 124 | 125 | - Provides a way to store the ordered collection. 126 | - It is an ordered collection of objects where duplicate values can be stored. 127 | - Because the list preserves the order of insertion, it allows positional access and insertion of items. 128 | - Duplicate or null-valued elements can be kept in classes that inherit from the List interface. 129 | - **Declaration:** The List interface is declared as: 130 | 131 | ```java 132 | public interface List extends Collection ; 133 | ``` 134 | 135 | - Example 136 | 137 | ```java 138 | import java.util.ArrayList; 139 | import java.util.Iterator; 140 | import java.util.List; 141 | 142 | public class ListClass { 143 | public static void main(String[] args) { 144 | List list = new ArrayList<>(); 145 | 146 | //add 147 | list.add(10); 148 | list.add(20); 149 | list.add(30); 150 | list.add(40); 151 | list.add(50); 152 | 153 | //Iterator 154 | System.out.println("***With Iterator***"); 155 | Iterator iterator = list.iterator(); 156 | while (iterator.hasNext()) { 157 | System.out.println(iterator.next()); 158 | } 159 | 160 | //get(int index) 161 | System.out.println("get(): " + list.get(3)); 162 | 163 | //indexOf(Object) 164 | System.out.println("indexOf(): " + list.indexOf(50)); 165 | 166 | //remove(int index) 167 | System.out.println("remove(): " + list.remove(2)); 168 | //Let's print the list to see the change made with remove() 169 | for (Integer a : list) { 170 | System.out.println(a); 171 | } 172 | 173 | //set(int index,Object element) 174 | System.out.println("set(): " + list.set(2, 100)); 175 | //Let's print the list to see the change made with set() 176 | for (Integer a : list) { 177 | System.out.println(a); 178 | } 179 | 180 | //subList(int fromIndex,int toIndex) 181 | System.out.println("New List"); 182 | List list2 = list.subList(1, 3); 183 | for (Integer b : list2) { 184 | System.out.println(b); 185 | } 186 | } 187 | } 188 | ``` 189 | 190 | ### ArrayList CLass 191 | 192 | - It stores list data using dynamic arrays. 193 | - Default size is 10. If the size is not enough, a new array is defined at runtime, which is twice the size of the existing array. Elements in the old array are transferred to the new array, preserving their indices. 194 | 195 | ✴️ ArrayList is preferred for storing and accessing data. 196 | 197 | ✴️ It is necessary to scroll the list when insert and delete operations are done, which degrades performance. 198 | 199 | ✴️ It is not a safe thread, it breaks data integrity. 200 | 201 | ### LinkedList Class 202 | 203 | ![linked-list.png](https://github.com/zehracakir/CollectionAndMapInterfaceJava/blob/main/linked-list.png) 204 | 205 | - It is a linear data structure where the elements are not stored in contiguous locations and each element is a separate object with a data part and an address part. 206 | - Elements are linked using pointers and addresses. Each element is known as a node. 207 | - They are preferred over arrays because of the dynamism and ease of insertion and deletion. 208 | 209 | ### Vector Class 210 | 211 | - Vector implements a dynamic array meaning it can grow or shrink as needed. Like an array, it contains components that can be accessed using an integer index. 212 | - It is synchronized, this feature increases reliability. So it is slower than ArrayList. 213 | 214 | ### **Queue LinkedList Class** 215 | 216 | - Queue is a FIFO (first-in-first-out) structure. 217 | - The item that comes out first is at the beginning of the queue; It is retrieved with the remove() or poll() method. 218 | - element(): Returns the item at the beginning of the queue, but does not remove it from the queue. 219 | - add(eleman): Adds the element given in the parameter to the queue. It throws an error if the operation fails. 220 | - offer(eleman): Adds the element given in the parameter to the queue. Returns null if the operation fails. 221 | - poll(): Removes the element at the beginning of the queue from the queue. 222 | - peek(): It is used to reach the next element in the queue. 223 | 224 | ### **Priority Queue Class** 225 | 226 | - There is a FIFO structure, but this structure cannot solve all the possibilities that will be encountered. Thus, there is a need for a structure that orders the elements of the collection according to the desired priority. 227 | - In a PriorityQueue queue, items are either in their natural order or in the order in which the Comparator is used at installation time. Of course, this depends on which constructor is used. Null items cannot be placed in a PriorityQueue queue. An object that is incompatible (not comparable) with its elements cannot be put in a PriorityQueue queue in its natural order. Doing so will throw a ClassCastException from the compiler. 228 | 229 | ## Map Interface 230 | 231 | - Map Interface is one of its concrete classes. The HashMap class can also be called a mixed match. It is very effective in adding and removing elements to the mapping table and in finding the element whose key is given in the table. 232 | - Classes using the Map Interface have the following methods: 233 | - clear(): It deletes all the values found in the map. 234 | - containsKey(Object key): Queries whether a certain key has already been entered. 235 | - containsValue(Object value): Queries whether a certain object has been entered before. 236 | - get(Object key): Returns the object corresponding to the key. 237 | - put (Object key, Object value): Registers the key-value pair. 238 | - remove (Object key): Deletes the value corresponding to a certain key. 239 | - size(): Returns the key-value binary number registered up to that time. 240 | - Example 241 | 242 | ```java 243 | import java.util.HashMap; 244 | import java.util.Map; 245 | 246 | public class MapClass { 247 | public static void main(String[] args) { 248 | Map mapTest = new HashMap(); 249 | 250 | //put(Key,Value) 251 | mapTest.put("Beşiktaş", "BJK"); 252 | mapTest.put("Fenerbagçe", "FB"); 253 | mapTest.put("Galatasaray", "GS"); 254 | mapTest.put("Trabzonspor", "TS"); 255 | 256 | for (Map.Entry a : mapTest.entrySet()) { 257 | System.out.print(a.getKey() + ":"); 258 | System.out.println(a.getValue()); 259 | } 260 | 261 | } 262 | } 263 | ``` 264 | 265 | ### HashMap Class 266 | 267 | - It stores data in pairs (Key, Value) and you can access them with an index of another type (for example, an Integer). 268 | - It allows storing null keys as well, but there must only be one null key object and can be any number of nulls. 269 | - This class makes no guarantees regarding the order of the map. 270 | 271 | ### **LinkedHasMap Class** 272 | 273 | - LinkedHashMap contains key based values. It implements the Map interface and extends the HashMap class. 274 | - It contains only unique items. 275 | - It can have one null key and multiple null values. 276 | - It's out of sync. 277 | - It is the same as HashMap with an additional feature that it preserves the insertion order. For example, when we run the code with a HashMap, we get a different order of elements. 278 | 279 | ### TreeMap Class 280 | 281 | - This class is a member of the Java Collections Framework. 282 | - The class implements the Map interfaces including NavigableMap , SortedMap and extends the AbstractMap class. 283 | - TreeMap in Java does not allow null keys (like Map) and therefore a NullPointerException is thrown. However, multiple null values can be associated with different keys. 284 | - The input pairs and views returned by the methods in this class represent snapshots of the mappings at the time they were generated. They do not support the Entry.setValue method. 285 | -------------------------------------------------------------------------------- /collection-interface.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zehracakir/CollectionAndMapInterfaceJava/b0dfa6e7144156691368295d9bc70bebf209c1ff/collection-interface.png -------------------------------------------------------------------------------- /linked-list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zehracakir/CollectionAndMapInterfaceJava/b0dfa6e7144156691368295d9bc70bebf209c1ff/linked-list.png --------------------------------------------------------------------------------