Collections Flashcards
Collection
USED FOR:
it is used to store multiples of the same item or type in the same place (collection)
Array
DEFINE & RULES
- It is used to store both primitives and reference types as collections.
- fixed sized. Once the size is declared, you cannot add or remove elements (size cannot be changed).
- Array does not provide methods to manipulate the data.
- Array allows duplicates
- Array allows null objects
NOTE: Except arrays, all other collections are used to store objects only (not primitives).
Duplication - Nulls - Order
LIST INTERFACE
- Allows duplication
- Allows null objects (data)
-Keeps insertion order.
NOTE: Provides all the methods with indexes.
-List implementation classes are Vector, ArrayList and LinkedList
(Iterable)
Duplication - Nulls - Order
MAP INTERFACE
- Map always stores data with key-value pairs
- Each key-value pair is also known as entry
- Keys -> NO duplicates
- Values -> duplicate
- put() method is used to add key-value pairs to the collection
-get(key) method is used to get value from the collection
NOTE: Maps do not work with methods that requires index.
Map implementation classes are HashMap, LinkedHashMap, HashTable, and TreeMap
Duplication - Nulls - Order
SET INTERFACE
- No duplicates
- One null object (data). BUT, TreeSet does not allow any null elements because it sorts.
-Set does not preserve insertion order.
NOTE: Set does not provide any methods that use index as arguments.
Set implementation classes are HashSet, LinkedHashSet, and TreeSet
(Iterable)
ArrayList vs Vector
Vector is the thread-safe version of ArrayList. Thread-safe means synchronized.
ArrayList vs LinkedList
LinkedList stores data with nodes (previous - itself - next) and this makes it faster when you remove or add elements. On the other hand, ArrayList stores data with indexes which takes less memory compared to LinkedList but slower when adding or removing elements.
LinkedList implements Queue and Dequeue which allows LinkedList to have more methods
HashSet vs LinkedHastSet
The only difference between them is LinkedHashSet keeps insertion order.
HashSet vs TreeSet
TreeSet -> NO nulls
HashSet -> a single null element.
TreeSet -> sorts elements implicitly.
Numbers: ascending
Strings: lexicographically
HashSet vs TreeSet
TreeSet -> NO nulls
HashSet -> a single null element.
TreeSet -> sorts elements implicitly.
Numbers: ascending
Strings: lexicographically
HashMap vs LinkedHashMap
The only difference is LinkedHashMap keeps the insertion order
HashMap vs TreeMap
HashMap -> one null key & many null values
TreeMap -> NO null keys & many null values. TreeMap -> sorts keys.
HashMap vs HashTable
HashMap -> 1 null key & many null values.
HashTable -> NO null keys or values.
HashTable -> thread-safe.
Hash vs Linked vs Tree
Hash -> allows nulls
Linked -> keeps insertion order
Tree -> No nulls & sorts
Iterator
EXAMPLE
Collections a = new ArrayList<>();
a. add(5);
a. add(7);
a. add(9);
Iterator b = a.iterator();
while(b.hasNext()){
System.out.println(b.next());
}