Collections Flashcards

1
Q

Difference between ArrayList and Vector?

A

1) Vector are Synchronized, due for deprecated, slow, poorly designed, not recommended to use
2)ArrayList are not thread safe
3) thread safe of ArrayList
CopyOnWriteArrayList
List namesList = Collections.synchronizedList(new ArrayList());

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

Character of ArrayList ?

A

1) Backed by a dynamic array
2) random index access is O(1)
3) manipulation is slow compare with linked list, when one element added/removed, need to move all impacted bits in array

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

Character of LinkedList?

A

1) internally uses a doubly linked list to store the elements
2) No ramdom index access, need O(n) to find the element
3) Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory.
4) Can be used as a Queue/Stack because it implements Deque interfaces
5) have memory over head as need to store the pointers

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

How HashMap internally works?

A
  1. Java7: Array + Linkedlist
    Java8: Array+LinkedList+RedBlackTree(when linkedlist size >8)
    Entry: key, value, hashcode, next pointer
    V never used in comparation!

2.
Capacity - array size, doubled when extended
LoadFactor - default 0.75
threshold - of Extend, = capacity*loadFactor - if not will cause hash collision degreed the performance

  1. how get(key)/put(k, v) works
    call key.hashCode(), find the bucket
    call key.equals(otherKey), find the elements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How HashSet internally works?

A

HashMap with all Node with dummy V.

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

Array’s HashCode - need tests

A

1) Since arrays don’t override the Object.hashCode method. Changing the array’s contents thus does not affect its hash code.
2) To get an array’s hash code correctly, you should call Arrays.hashCode(nums).

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

HashSet and TreeSet Difference?

A
  1. HSimplemented by HashTable, TS-> Tree structure
  2. HS elements are unordered, TS elements are sorted
  3. HS add/remove/contans is O(1), TS is O(logN)
  4. HS using equals() check duplication, TS using compareTo()
  5. TS not allow null elements
  6. 2 elements equal() must , compareTo() should return 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

LinkedHashMap internal implementation?

A
  1. HashMap with gauranteed order of insertion

2. Using a DoubleLinkedList to keep the insertion order

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

Interfaces on Collection framework?

A
1. Collection
List->(ArrayList, LinkedList, Vector->Stack)
Set->(HashSet, LinkedHashSet)
->SortedSet->NavigableSet->(TreeSet)
Queue->Deque->(LinkedList)
  1. Map->(HashMap, LinkedHashMap)
    - >SortedMap->NavigableMap->(TreeMap)

3.Iterator->ListIterator

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

Fail Fast Iterator and Fail safe Iterator

A

Fail-safe iterators - will not throw any exception even if the collection is modified while iterating over it.
Fail-fast iterators throw an exception(ConcurrentModificationException) if the collection is modified while iterating over it.

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

Iterator Vs ListIterator

A

Iterator

  1. can be used on Map, set ,list
  2. can only traverse forward
  3. can not modify elements
  4. can not return index

ListIterator

  1. can only used on List
  2. can traverse forward and backword: next(), previous()
  3. can modify elements add(), remove(), set()
  4. can return index nextIndex(), previousIndex()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Collection Vs Collections

A

Collection - super Interface of List, Set

Collections - Utility class for Collection - because previous java8 version does not allow default methods
addAll(other)
emptyList/Map/Set() - parameterized type safe
EMPTY_SET/LIST/MAP - return raw type
reverse(List> list)
max/min(Collections c), max/min(Collections c, comparator c)

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

Coding 1. Implement HashMap using array

A
  1. entryclass

2. mymap class, put, get

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

Coding 2. Implement ArrayList using array

A

todo

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