Java Collections | collections in java October 7, 2020October 7, 2020 lifebix COllection: If we want represent a group of individual objects in a single Entity then shoud go for collection Collection is a interface it most common methods which can be applicable for all objects Collection Methods: 1. boolean add(Object o);// Ex: al.add(“Hi”); 2. boolean addAll(Collection c); 3. boolean remove(Object o); 4. boolean removeAll(collection c); 5. boolean retainAll(collection c); 6. void clear(); 7. boolean isEmpty(); 8. int size(); 9. boolean contains(Object o); 10. boolean containsAll(Collection c); 11. Iterator iterator(); ———————————————————— List 1. List is the child interface of collection 2. if you want represent group of indual objects 3. List allows the duplicate objects 4. List insertion order preserved based on index List defined Methods: 1. boolean add(int index,Object o) 2. boolean addAll(int index,Collection c); 3. Object remove(int index) 4. Object get(int index); 5. Object(old) Set(int index,Object o(new))); 6. int LastIndex(Object o); 7. int indexOf(Object o); 8. ListIterator listIterator(); List interface Contained Classes: 1. ArrayList(1.2) 2. LinkedList(1.2) 3.Vector(1.0) 4. Stack(1.0) ———————————————————————————————– ArrayList Class: 1. ArrayList underlying datastructor is Resizable array or Growable 2. ArrayListinsertion order preserved 3. ArrayList allows the duplicate values 4. ArrayList allows heterogeneous datatype 5. Inside the null value insertion possible ArrayList Constructors: 1. ArrayList al=new ArrayList(); intial capcity=10; current capacity=(ic*3/2)+1;==>10*3/2=16==>(16*3/2)+1==25 2. ArrayList al=new ArrayList(int intialCapacity); 3. ArrayList al=new ArrayList(Collection c); —————————————————————————————— LinkedList: 1. LinkedList underlying datastructor doubleLinkedList 2. insertion order preserved 3. duplicate values 4. null values insertion Constructors LinkedList l=new LinkedList(); LinkedList l=new LinkedList(collection c); LinkedList methods: 1. void addFirst(Object o); 2. void addLast(Object o); 3. Object removeFirst(); 4. Object removeLast(); 5. Object getFirst(); 6. Object getLast(); ——————————————————————————————– Vector: 1. Vector underlying datastructor is Resizable array or Growable array 2. insertion order preserved 3. heterogeneous objects are allowed 4. null insertion possible 5. duplicate objects are allowed Constructors: Vector v=new Vector(); default intial capacity=10; new capacity=cc*2; Vector v=new Vector(int intialCapacity); Vector v=new Vector(int intialCapacity,int incrementcapacity); Vector v=new Vector(collection c); Vector Methods: addElement(Object o); removeElement(Object o); removeElementAt(int index); removeAllElements(); firstElement(); lastElement(); elementAt(int index); other Methods: 1. size() 2. capacity 3. Enumeration elements();