collections Flashcards

2
Q

What is an Iterator ?

A
  • The Iterator interface is used to step through the elements of a Collection.
  • Iterators let you process each element of a Collection.
  • Iterators are a generic way to go through all the elements of a Collection, no matter how it is organized.
  • Iterator is an Interface implemented a different way for every Collection.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you traverse through a collection using its Iterator?

A

To use an iterator to traverse through the contents of a collection, follow these steps:

  1. Obtain an iterator to the start of the collection by calling the collection’s iterator() method.
  2. Set up a loop that makes a call to hasNext().
  3. Have the loop iterate as long as hasNext() returns true.
  4. Within the loop, obtain each element by calling next().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you remove elements during Iteration?

A

Iterator has a remove() method. When it is called, the current element in the iteration is deleted.

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

What is the difference between Enumeration and Iterator?

A

Enumeration

Iterator

Enumeration doesn’t have a remove() method

Iterator has a remove() method

Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects

Can be abstract, final, native, static, or synchronized

Note: So Enumeration is used whenever we want to make Collection objects as Read-only.

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

How is ListIterator?

A

ListIterator is just like Iterator, except it allows us to access the collection in either the forward or backward direction and lets us modify an element

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

What is the List interface?

A

The List interface provides support for ordered collections of objects.

Lists may contain duplicate elements.

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

What are the main implementations of the List interface ?

A

The main implementations of the List interface are as follows :

  • ArrayList : Resizable-array implementation of the List interface. The best all-around implementation of the List interface.
  • Vector : Synchronized resizable-array implementation of the List interface with additional “legacy methods.”
  • LinkedList : Doubly-linked list implementation of the List interface. May provide better performance than the ArrayList implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues (deques).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the advantages of ArrayList over arrays ?

A

Some of the advantages ArrayList has over arrays are:

  • It can grow dynamically
  • It provides more powerful insertion and search mechanisms than arrays.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Difference between ArrayList and Vector ?

A

ArrayList

Vector

ArrayList is NOT synchronized by default.

Vector List is synchronized by default.

ArrayList can use only Iterator to access the elements.

Vector list can use Iterator and Enumeration Interface to access the elements.

The ArrayList increases its array size by 50 percent if it runs out of room.

A Vector defaults to doubling the size of its array if it runs out of room

ArrayList has no default size.

While vector has a default size of 10.

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

How to obtain Array from an ArrayList ?

A

Array can be obtained from an ArrayList using toArray() method on ArrayList.

List arrayList = new ArrayList(); Object a[] = arrayList.toArray();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why insertion and deletion in ArrayList is slow compared to LinkedList ?

A

ArrayList internally uses and array to store the elements, when that array gets filled by inserting elements a new array of roughly 1.5 times the size of the original array is created and all the data of old array is copied to new array.

During deletion, all elements present in the array after the deleted elements have to be moved one step back to fill the space created by deletion.

In LinkedList, data is stored in nodes that have reference to the previous node and the next node so adding element is simple as creating the node an updating the next pointer on the last node and the previous pointer on the new node.

Deletion in linked list is fast because it involves only updatingthe next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.

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

Why are Iterators returned by ArrayList called Fail Fast ?

A

Because, if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException.

Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

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

How do you decide when to use ArrayList and when to use LinkedList?

A

If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList offers the optimal collection.

If, however, you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList offers the better implementation.

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

What is the Set interface ?

A

The Set interface provides methods for accessing the elements of a finite mathematical set

  • Sets do not allow duplicate elements
  • Contains no methods other than those inherited from Collection
  • It adds the restriction that duplicate elements are prohibited
  • Two Set objects are equal if they contain the same elements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the main Implementations of the Set interface ?

A

The main implementations of the List interface are as follows:

  • HashSet
  • TreeSet
  • LinkedHashSet
  • EnumSet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a HashSet ?

A

A HashSet is an unsorted, unordered Set.

It uses the hashcode of the object being inserted (so the more efficient your hashcode() implementation the better access performance you’ll get).

Use this class when you want a collection with no duplicates and you don’t care about order when you iterate through it.

18
Q

What is a TreeSet ?

A

TreeSet is a Set implementation that keeps the elements in sorted order.

The elements are sorted according to the natural order of elements or by the comparator provided at creation time.

19
Q

What is an EnumSet ?

A

An EnumSet is a specialized set for use with enum types.

All of the elements in the EnumSet type that is specified, explicitly or implicitly, when the set is created.

20
Q

Difference between HashSet and TreeSet ?

A

HashSet

TreeSet

HashSet is under set interface i.e. it does not guarantee for either sorted order or sequence order.

TreeSet is under set i.e. it provides elements in a sorted order (acceding order).

We can add any type of elements to hash set.

We can add only similar types of elements to tree set.

21
Q

What is a Map ?

A

A map is an object that stores associations between keys and values (key/value pairs).

  • Given a key, you can find its value.
  • Both keys and values are objects.
  • The keys must be unique, but the values may be duplicated.
  • Some maps can accept a null key and null values, others cannot.
22
Q

What are the main Implementations of the Map interface ?

A

The main implementations of the Map interface are as follows:

  • HashMap
  • HashTable
  • TreeMap
  • EnumMap
23
Q

What is a TreeMap ?

A

TreeMap actually implements the SortedMap interface which extends the Map interface.

In a TreeMap the data will be sorted in ascending order of keys according to the natural order for the key’s class, or by the comparator provided at creation time.

TreeMap is based on the Red-Black tree data structure.

24
Q

How do you decide when to use HashMap and when to use TreeMap ?

A

For inserting, deleting, and locating elements in a Map, the HashMap offers the best alternative.

If, however, you need to traverse the keys in a sorted order, then TreeMap is your better alternative.

Depending upon the size of your collection, it may be faster to add elements to a HashMap, then convert the map to a TreeMap for sorted key traversal.

25
Q

Difference between HashMap and Hashtable ?

A

HashMap

HashTable

HashMap lets you have null values as well as one null key.

HashTable does not allows null values as key and value.

The iterator in the HashMap is fail-safe (If you change the map while iterating, you’ll know).

The enumerator for the Hashtable is not fail-safe.

HashMap is unsynchronized.

Hashtable is synchronized.

26
Q

How does a Hashtable internally maintain the key-value pairs?

A

The Hashtable class uses an internal (private) class named Entry to hold the key-value pairs.

All entries of the Hashtable are stored in an array of Entry objects with the hash value of the key serving as the index.

If two or more different keys have the same hash value these entries are stored as a linked list under the same index.

27
Q

What are the different Collection Views that Maps provide?

A

Maps Provide 3 Collection Views:

  • Key Set - allow a map’s contents to be viewed as a set of keys.
  • Values Collection - allow a map’s contents to be viewed as a set of values.
  • Entry Set - allow a map’s contents to be viewed as a set of key-value mappings.
28
Q

What is a KeySet View ?

A

KeySet is a set returned by the keySet() method of the Map Interface.

It is a set that contains all the keys present in the Map.

29
Q

What is a Values Collection View ?

A

Values Collection View is a collection returned by the values() method of the Map Interface.

It contains all the objects present as values in the map.

30
Q

What is an EntrySet View ?

A

Entry Set view is a set that is returned by the entrySet() method in the map and contains Objects of type Map.

Entry each of which has both Key and Value.

31
Q

How do you sort an ArrayList (or any list) of user-defined objects ?

A

Create an implementation of the java.lang.Comparable interface that knows how to order your objects and pass it to java.util.Collections.sort(List, Comparator).

32
Q

What is the Comparable interface ?

A

The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively.

The objects of the class implementing the Comparable interface can be ordered.

The Comparable interface in the generic form is written as follows:

interface Comparable

where T is the name of the type parameter.

All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer.

The signature of the compareTo() method is as follows:

int i = object1.compareTo(object2)

If object1 < object2: The value of i returned will be negative.

If object1 > object2: The value of i returned will be positive.

If object1 = object2: The value of i returned will be zero.

33
Q

What are the differences between the Comparable and Comparator interfaces ?

A

Comparable

Comparator

It uses the compareTo() method.
int objectOne.compareTo(objectTwo).

It uses the compare() method.

int compare(ObjOne, ObjTwo)

It is necessary to modify the class whose instance is going to be sorted.

A separate class can be created in order to sort the instances.

Only one sort sequence can be created.

Only one sort sequence can be created.

It is frequently used by the API classes.

It used by third-party classes to sort instances.