ArrayLists Flashcards
What is the ArrayList class roughly equivalent to?
It is roughly equivalent to Vector, except that Array List is unsynchronized (should be used in an non-threaded environment).
What are the common ways to initialize an ArrayList?
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.
What’s an ArrayList?
Is a resizable array implementation of the list interface. It implements all optional List operations, and permits all elements, including null.
What are the common ways to loop thru an ArrayList?
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()); }
Common ways to sort an ArrayList?
- Collections.sort(ArrayList a)
- Sort in descending order –> Collections.reverseOrder(ArrayList a)
3) Implement the Comparable interface and override the compareTo() in the user-defined object class.
Ways to synchronize an ArrayList?
- Collections.synchronizedList( ArrayList a )
2. CopyOnWriteArrayList
Convert a LinkedList to an ArrayList?
- 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
Synchronization differences between Vector and ArrayList?
ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time.
Resizing differences between Vector and ArrayList?
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.
Performance differences between Vector and ArrayList?
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.
Fail-fast differences between Vector and ArrayList?
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.
Similarities between Vector and ArrayList?
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.
Vector vs ArrayList: Who belongs to collection framework really?
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.
Implementation differences between ArrayList and HashMap?
ArrayList implements List Interface while HashMap is an implementation of Map interface. List and Map are two entirely different collection interfaces.
Memory differences between ArrayList and HashMap?
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");