Most of the programs use collections in order to represent data. This article will explain more general information about Java Collection Framework.
What is it Java Collection Framework?
Java Collection Framework is a set of standard interfaces and their implementations. This set has some structure, which we will discuss in detail.
Additionally, a couple of words about collections. A collection is a group of elements with similar properties of each item. You can iterate on elements, get by index, remove or add new items to collections.
The basic structure of Java Collections
The root of the collection is represented by two interfaces. The first one is Collection and the second Map.
The main reason why Map interface is not a part of Collection, is that the Map is not iterable. You cannot iterate on this collection, only the way you can get an element by its unique key.
This interface represents a collection with a pair of key and value. Where key must be unique. In Java 8 some additional methods were added (getOrDefault(Object key, V defaultValue), putIfAbsent(K key, V value)).
This collection was implemented before the JCF created. As a heritage it was included. The structure of it is synchronised.
The TreeMap it’s an implementation of red-black tree in Java. This map contains a lot of methods provided in order to navigate on this map.
Let’s see some sample how we can find elements less or greater then current key.
TreeMap<Integer, String> map = new TreeMap<>(); map.put(3, "val"); map.put(2, "val"); map.put(1, "val"); map.put(5, "val"); map.put(4, "val"); Integer highestKey = map.lastKey(); Integer lowestKey = map.firstKey(); Set<Integer> keysLessThan3 = map.headMap(3).keySet(); Set<Integer> keysGreaterThanEqTo3 = map.tailMap(3).keySet(); System.out.println(highestKey); System.out.println(lowestKey); System.out.println(keysLessThan3.toString()); System.out.println(keysGreaterThanEqTo3.toString());
5 1 [1, 2] [3, 4, 5]
It’s sequential collection. All data which is added has the same order. This effect can be achieved by two side links provided in the implementation. But, meanwhile, the negative aspect is that the size of used memory is increased due to additional service data.
This collection is very similar to HashTable, only one difference is that this is not synchronized. The order of elements depends on the hash function. You cannot relay that the order of elements will be sequential.
This collection is implemented with weak references. The GC will remove from collection elements where there are no links to the object.
These types of collections present a sequential sequence of elements. You can get elements by index and find them by value. You can store the duplicate values.
This collection is based on an array. Like an array, you can get elements by index. It works faster for collections where is not much data.
This list has linked elements. The best practice to use it became when you need to remove/add inside of a list a lot of data. Links provide the possibility to do that O(1).
This is a synchronized list inherited from Java 1.0. If you do not use multithreading there is no need to use it.
This collection extends Vector and it was added in Java 1.0. After Java 1.6 there is ArrayDeque which is more appropriate to use.
This interface presents not sequential collection where all elements are unique.
This collection has the same functionality like HashMap. Only one difference is that the value is empty Object value.
Like LinkedHashMap this collection has the same order when we added new elements. It’s provided by links. As a result we need more memory to manage this collection.
This collection presents red-black tree. By using Comparator we can manage order in collection.
This interface presents queue with LIFO and FIFO access, where we can manage priority of elements.
This class implements interface Queue. In this class you can manage order of elements in the collection. For this purpose you can change Comparator.
The same like PriorityQueue but in basis of it we have array. It has better impact on speed when we retrieve elements.
Collections class short overview
This class contains a lot of static method in order to provide additional functionality for collections.
There are some of them:
synchronizedCollection(Collection<T> c) sort(List<T> list, Comparator<? super T> c) unmodifiableCollection(Collection<? extends T> c) checkedCollection(Collection<E> c, Class<E> type)
In this article we could see general overview of collections in JCF.