Collection Flashcards

1
Q

Give a short description of the interface: Iterable

A

a class implementing this interface can be used for iterating with a foreach statement

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

Give a short description of the interface : Collection

A

Common base interface for classes in the collection hierarchy. When you want to write methods that are very general, you can pass Collection interface.

Most important methods:
boolean add(Element elem)
void clear()
boolean isEmpty()
Iterator iterator()
boolean remove(Object obj)
int size()
Object[] toArray()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give a short description of the interface : List

A
  • Base interface for containers that store a sequence of elements.
  • You can access the elements using an index.
    Maintains insertion order
  • Can store duplicates
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give a short description of the interface : Set, SortedSet, NavigableSet

A
  • interfaces for containers that dont allow duplicates
  • SortedSet maintains the set elements in a sorted order
  • NavigableSet allows searching the set for the closest match
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give a short description of the interface : Map, SortedMap, NavigableMap

A

interfaces for containers that map keys to values. In sortedMap, the keys are sorted in order

  • NavigableMap allows you to search and return the colest match for given search
  • Map hierarchy does NOT extend Collection interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give a short description of the interface: Queue, Deque

A

Queue interface is a base interface for containers that hold a sequence of elements for processing.
For ex. the classes implementing Queue can be LIFO or FIFO
* In a deque you can insert or remove elements from both ends

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

Give a short description of the interface: Iteratir, ListIterator

A
  • You can transverse over the container in the foward direction if a class implements the Iterator interface
  • You can traverse in both foward and reverse direction if a class implements the ListIterator interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which are the import concrete classes in the Collection Framework?

A
  • ArrayList
  • LinkedList
  • HashSet
  • TreeSet
  • HashMap
  • TreeMap
  • PriorityQueue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give a brief description of the concrete class ArrayList

A
  • Implemented as a resizable array
  • Fast to serach
  • Slow to delete/insert
  • allow dups
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give a brief description of the concrete class LinkedList

A
  • Doubly linked list data strut
  • Fast to insert or delete elements
  • slow for searching elements
  • can be used when you need stack (LIFO) or Queue(FIFO)
  • allow dups
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give a brief description of the concrete class HashSet

A
  • hash table data structure
  • does not allow dups
  • fast for earching and retrieving elements
  • does NOT maintain any order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give a brief description of the concrete class TreeSet

A
  • red-black tree data structure
  • does not allow dup
  • store elements in a sorted order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give a brief description of the concrete class HashMap

A
  • hash-table data structure
  • key values pair
  • uses hashing for finding a place to search or store a pair
  • searching or inserting is very fast
  • not store elements in order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Give a brief description of the concrete class TreeMap

A
  • red-black tree data structure

- store elements in a sorted order

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

Give a brief description of the concrete class PriorityQueue

A
  • heap data structure
  • retrieving based on priority
  • irrespective of the order in which you insert
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Given a list:

ArrayList{String} languageList = new ArrayList{}()

how to iterate over the list using iterator?

A

for(Iterator{String} languageIter = languageList.iterator(); languageIter.hasNext();) {
String language = languageIter.next();
System.out.println(language);
}

17
Q

What is the difference between ArrayList and ArrayDeque?

A

The difference is that you can add an element anywhere in a array list using an index; however, you can add an element only either at the front or end of the array deque.

That makes insertion in array deque more efficient than array list; however, navigation in an array deque becomes more expensive than in array list

18
Q

What is the method the Comparable interface has?

A

int compareTo(Element that)

19
Q

Regards to comparings its, what should the int value be?

A

1 if current object more than passed object
0 if equal
-1 current object less than passed object

20
Q

Of all the java.util package classes, which ones are thread-safe?

A

Only Vector and Hashtable

 java.util.Collections class contains a synchronizedCollection method that creates thread-safe instances based on collections which are not. For example: 
Set s = Collections.synchronizedSet(new HashSet());