Collections Flashcards

1
Q

Collection

USED FOR:

A

it is used to store multiples of the same item or type in the same place (collection)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Array

DEFINE & RULES

A
  • 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Duplication - Nulls - Order

LIST INTERFACE

A
  • 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Duplication - Nulls - Order

MAP INTERFACE

A
  • 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Duplication - Nulls - Order

SET INTERFACE

A
  • 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

ArrayList vs Vector

A

Vector is the thread-safe version of ArrayList. Thread-safe means synchronized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

ArrayList vs LinkedList

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

HashSet vs LinkedHastSet

A

The only difference between them is LinkedHashSet keeps insertion order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

HashSet vs TreeSet

A

TreeSet -> NO nulls

HashSet -> a single null element.

TreeSet -> sorts elements implicitly.
Numbers: ascending
Strings: lexicographically

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

HashSet vs TreeSet

A

TreeSet -> NO nulls

HashSet -> a single null element.

TreeSet -> sorts elements implicitly.
Numbers: ascending
Strings: lexicographically

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

HashMap vs LinkedHashMap

A

The only difference is LinkedHashMap keeps the insertion order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

HashMap vs TreeMap

A

HashMap -> one null key & many null values

TreeMap -> NO null keys & many null values. TreeMap -> sorts keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

HashMap vs HashTable

A

HashMap -> 1 null key & many null values.

HashTable -> NO null keys or values.
HashTable -> thread-safe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Hash vs Linked vs Tree

A

Hash -> allows nulls

Linked -> keeps insertion order

Tree -> No nulls & sorts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Iterator

EXAMPLE

A

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());
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly