Java Collections

1.1 Collections in Java

is a framework. provides an architecture to store and manipulate the group of objects. operations - search,sort,insert,delete,manipulation Interfaces - Set,List,Queue,Deque Classes - ArrayList,LinkedList,Vector,PriorityQueue,HashSet,LinkedHashSet,TreeSet java.util package contains all the classes and interfaces

1.2 Methods of Collection Interface

1.3 Iterator Interface

Methods


    import java.util.*;

    class JavaCollectionDemo {
       public static void main(String args[]) {
          ArrayList<String> list = new ArrayList<String>();
          list.add("abc");
          list.add("abcb");
          Iterator itr = list.iterator();
          while (itr.hasNext()) {
             System.out.println(itr.next());
          }
       }
    }

2.1 ArrayList


    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class ArrayListInteger {
    
      public static void main(String[] args) {
    
        // ArrayList<Integer> list_first = new ArrayList<Integer>();
        List<Integer> list_first = new ArrayList<Integer>();
        list_first.add(1);
        list_first.add(2);
        list_first.add(3);
    
        // array print
        System.out.println("List : " + list_first.toString());
    
        // array size
        System.out.println("Array size : " + list_first.size());
    
        // contain ?
        if (list_first.contains(3)) {
          System.out.println("List has the value");
        } else {
          System.out.print("List does not  have the value");
        }
    
        // addAll
        ArrayList<Integer> list_second = new ArrayList<Integer>();
        list_second.add(4);
        list_second.add(5);
        list_second.add(6);
        System.out.println("second list" + list_second.toString());
    
        list_first.addAll(list_second);
        System.out.println("updated first list" + list_first.toString());
    
        // remove
        list_first.remove(4);
        System.out.println("updated first list (after remove)" + list_first.toString());
    
        // iterating
        for (int i = 0; i < list_first.size(); i++) {
          System.out.println(list_first.get(i));
        }
    
        // Iterating
        Iterator itr = list_first.iterator();
        while (itr.hasNext()) {
          System.out.println(itr.next());
        }
    
      }
    }
    

2.2 LinkedList


    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    public class LinkedListDemo {
    
      public static void main(String[] args) {
    
        Object objects[] = new Object[1000000];
    
        for (int i = 0; i < objects.length; i++) {
          objects[i] = new Object();
        }
        
        // Arra
        LinkedList<Object> list = new LinkedList();
        long start = System.currentTimeMillis();
        for (Object object : objects) {
          list.add(objects);
        }
        long end = System.currentTimeMillis();
        System.out.println("Time taken : " + (end - start));
        
        // insert
        start = System.currentTimeMillis();
        list.set(2,new Object());
        end = System.currentTimeMillis();
        System.out.println("Time taken : " + (end - start));
    
      }
    
    }
    

2.3 Vector



2.4 Set

3.1 Generics


class Container<T> {
        T data;

        void showType() {
                System.out.println(data.getClass().getName());
        }
}

class Problem {

        public static void main(String[] args) {
                Container<Integer> obj_1 = new Container<>();
                obj_1.data = 1;
                obj_1.showType();

                Container<Double> obj_2 = new Container<>();
                obj_2.data = 1.1;
                obj_2.showType();
        }
}