ArrayLists Flashcards

1
Q

What is the ArrayList class roughly equivalent to?

A

It is roughly equivalent to Vector, except that Array List is unsynchronized (should be used in an non-threaded environment).

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

What are the common ways to initialize an ArrayList?

A

There are four ways to initialize an array list.

1) initialization using Array.asList
2) Anonymous inner class method to initialize ArrayList
3) add(Object o)
4) Collections.ncopies when we need to initialize the ArrayList with the same value for all of its elements.

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

What’s an ArrayList?

A

Is a resizable array implementation of the list interface. It implements all optional List operations, and permits all elements, including null.

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

What are the common ways to loop thru an ArrayList?

A
example:
ArrayList arrlist = new ArrayList();
      arrlist.add(14);
      arrlist.add(7);
      arrlist.add(39);
      arrlist.add(40);
1. For Loop
 for (int counter = 0; counter  count) {
	 System.out.println(arrlist.get(count));
         count++;
      }
4. Iterator
Iterator iter = arrlist.iterator();
      while (iter.hasNext()) {
         System.out.println(iter.next());
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Common ways to sort an ArrayList?

A
  1. Collections.sort(ArrayList a)
  2. Sort in descending order –> Collections.reverseOrder(ArrayList a)
    3) Implement the Comparable interface and override the compareTo() in the user-defined object class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Ways to synchronize an ArrayList?

A
  1. Collections.synchronizedList( ArrayList a )

2. CopyOnWriteArrayList

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

Convert a LinkedList to an ArrayList?

A
  1. When creating the ArrayList, add the name of the LinkedList to initialize it.
example:
LinkedList linkedlist = new LinkedList();
linkedlist.add("Harry");
linkedlist.add("Jack");
linkedlist.add("Tim");
linkedlist.add("Rick");
linkedlist.add("Rock");

List list = new ArrayList(linkedlist);

*this creation is the same for vector –> arraylist and hashset –> arraylist

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

Synchronization differences between Vector and ArrayList?

A

ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time.

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

Resizing differences between Vector and ArrayList?

A

Both ArrayList and Vector can grow and shrink dynamically to maintain the optimal use of storage, however the way they resized is different. ArrayList grow by half of its size when resized while Vector doubles the size of itself by default when grows.

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

Performance differences between Vector and ArrayList?

A

ArrayList gives better performance as it is non-synchronized. Vector operations gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released.

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

Fail-fast differences between Vector and ArrayList?

A

Fail-fast: If the collection (ArrayList, vector etc) gets structurally modified by any means, except the add or remove methods of iterator, after creation of iterator then the iterator will throw ConcurrentModificationException. Structural modification refers to the addition or deletion of elements from the collection.

As per the Vector javadoc the Enumeration returned by Vector is not fail-fast. On the other side the iterator and listIterator returned by ArrayList are fail-fast.

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

Similarities between Vector and ArrayList?

A

There are few similarities between these classes which are as follows:

  • Both Vector and ArrayList use growable array data structure.
  • The iterator and listIterator returned by these classes (Vector and ArrayList) are fail-fast.
  • They both are ordered collection classes as they maintain the elements insertion order.
  • Vector & ArrayList both allows duplicate and null values.
  • They both grows and shrinks automatically when overflow and deletion happens.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Vector vs ArrayList: Who belongs to collection framework really?

A

The vector was not the part of collection framework, it has been included in collections later. It can be considered as Legacy code. There is nothing about Vector which List collection cannot do. Therefore Vector should be avoided. If there is a need of thread-safe operation make ArrayList synchronized as discussed in the next section of this post or use CopyOnWriteArrayList which is a thread-safe variant of ArrayList.

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

Implementation differences between ArrayList and HashMap?

A

ArrayList implements List Interface while HashMap is an implementation of Map interface. List and Map are two entirely different collection interfaces.

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

Memory differences between ArrayList and HashMap?

A

ArrayList stores the element’s value alone and internally maintains the indexes for each element.

 ArrayList arraylist = new ArrayList();
//String value is stored in array list
 arraylist.add("Test String");

HashMap stores key & value pair. For each value there must be a key associated in HashMap. That clearly shows that memory consumption is high in HashMap compared to the ArrayList.

HashMap hmap= new HashMap();
//String value stored along with the key value in hash map
 hmap.put(123, "Test String");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Order differences between ArrayList and HashMap?

A

ArrayList maintains the insertion order while HashMap doesn’t. Which means ArrayList returns the list items in the same order in which they got inserted into the list. On the other side HashMap doesn’t maintain any order, the returned key-values pairs are not sorted in any kind of order.

17
Q

Duplicate differences between ArrayList and HashMap?

A

ArrayList allows duplicate elements but HashMap doesn’t allow duplicate keys (It does allow duplicate values).

18
Q

Null differences between ArrayList and HashMap?

A

ArrayList can have any number of null elements. HashMap allows one null key and any number of null values.

19
Q

get method differences between ArrayList and HashMap?

A

In ArrayList we can get the element by specifying the index of it. In HashMap the elements is being fetched by specifying the corresponding key.

20
Q

Search differences between ArrayList and HashMap?

A

ArrayList search operation is pretty fast compared to the LinkedList search operation. get(int index) in ArrayList gives the performance of O(1) while LinkedList performance is O(n).

Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.

21
Q

Deletion differences between ArrayList and HashMap?

A

LinkedList remove operation gives O(1) performance while ArrayList gives variable performance: O(n) in worst case (while removing first element) and O(1) in best case (While removing last element).

Conclusion: LinkedList element deletion is faster compared to ArrayList.

Reason: LinkedList’s each element maintains two pointers (addresses) which points to the both neighbor elements in the list. Hence removal only requires change in the pointer location in the two neighbor nodes (elements) of the node which is going to be removed. While In ArrayList all the elements need to be shifted to fill out the space created by removed element.

22
Q

Inserts Performance differences between ArrayList and HashMap?

A

LinkedList add method gives O(1) performance while ArrayList gives O(n) in worst case. Reason is same as explained for remove.

23
Q

Memory Overhead differences between ArrayList and HashMap?

A

ArrayList maintains indexes and element data while LinkedList maintains element data and two pointers for neighbor nodes hence the memory consumption is high in LinkedList comparatively.

24
Q

Similarities between ArrayList and HashMap?

A

There are few similarities between these classes which are as follows:

Both ArrayList and LinkedList are implementation of List interface.
They both maintain the elements insertion order which means while displaying ArrayList and LinkedList elements the result set would be having the same order in which the elements got inserted into the List.
Both these classes are non-synchronized and can be made synchronized explicitly by using Collections.synchronizedList method.
The iterator and listIterator returned by these classes are fail-fast (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).