Set
- Set prevents duplication.The common use of set is to check for duplication
- Since its is helpful for lookups for duplicate value HashSet provides an optimized implementation
- If you want the result to be sorted use TreeSet instead of HashSet
Set
Why ArrayList Faster than LinkedList during Random Access?
ArrayList has direct references to every element in the list, so it can get the n-th element in constant time. LinkedList has to traverse the list from the beginning to get to the n-th element.
Why LinkedList faster than ArrayList during Insertion/Deletion?
ArrayList is slower because it needs to copy part of the array in order to remove the slot that has become free. If the deletion is done using the ListIterator.remove() API, LinkedList just has to manipulate a couple of references; if the deletion is done by value or by index, LinkedList has to potentially scan the entire list first to find the element(s) to be deleted.
When should I use LinkedList?
When should I use ArrayList?
| Operation | Linked List | Array List |
|---|---|---|
| Access | O(n) | O(1) |
| Insertion | Access time + O(1) | Access time + O(n) |
| Deletion | Access time + O(1) | Access time + O(n) |
When you are simple moving through List but you are not modifying the List object foreach is more efficient.In case you want to perform operations on each element of list individually taking out the element in such case use Iterator.
List<Fruits> arrFruits = new ArrayList<Fruits>();
Iterator<Fruits> itFrt = arrFruits.iterator();
while(itFrt.hasNext())
{
Fruits frt = itFrt.next();
System.out.println(frt);
}
ListIterator
List<fruits> arrFruits = fruits.arrayList(5);
ListIterator<fruits> itFrt = arrFruits.listIterator();
while(itFrt.hasNext())
{
System.out.println(it.next());
System.out.println(it.nextIndex());
System.out.println(it.previousIndex());
}
Operations in ArrayList
class Fruits
{
String Name = "Fruit";
}
class Apple extends Fruits
{
public Apple()
{
Name = "Apple";
}
}
class Orange extends Fruits
{
public Orange()
{
Name = "Orange";
}
}
class Mango extends Fruits
{
public Mango()
{
Name = "Mango";
}
}
Adding Element – List.add()
List<Fruits> arrFruits = new ArrayList<Fruits>(); Fruits objApple = new Apple(); Fruits objMango = new Mango(); arrFruits.add(objApple); arrFruits.add(objMango);
Removing Element – List.remove()
arrFruits.remove(objApple);
Removing by Index – List.remove()
arrFruits.remove(1);
Index of Element – List.indexOf()
arrFruits.indexOf(objMango);
Index of Element – List.indexOf()
arrFruits.indexOf(objMango);
SubList – List.subList(StartPOS, EndPOS)
List<Fruits> arrNewFruits = arrFruits.subList(0,2);
containsAll – NewList.containsAll(OrgList)
arrNewFruits.containsAll(arrFruits);
set Value at Index
arrNewFruits.set(1, new Orange());
removeAll
arrFruits.removeAll(arrFruits);
addAll
arrFruits.addAll(arrNewFruits);
retainAll – set Intersection Operation
retainAll performs set Interscetion Operation by taking Two ArrayList. It Retains Boolean true if intersection finds element false otherwise.
List<Integer> arrNums1 = new ArrayList<Integer>(); arrNums1.add(1); arrNums1.add(2); arrNums1.add(3); List<Integer> arrNums2 = new ArrayList<Integer>(); arrNums2.add(1); System.out.println(arrNums1.retainAll(arrNums2)); System.out.println(arrNums1);