Monday, January 5, 2015

Collection Interface Methods Demo

1. Create a java project.
2. create the package com.demo under src folder.
3. copy & paste the following class code.

 package com.demo;  
 import java.util.ArrayList;  
 import java.util.Iterator;  
 import java.util.List;  
 public class CollectionIterfaceMethodDemo {  
      public static void main(String[] args) {  
           List<String> demoList1 = new ArrayList<String>();  
           demoList1.add("A");  
           demoList1.add("B");  
           demoList1.add("C");  
           demoList1.add("D");  
           System.out.println("-- Printing elements of List1 using iterator with WHILE --");  
           Iterator<String> itr = demoList1.iterator();  
           while (itr.hasNext()) {  
                Object element = itr.next();  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("-- Printing elements of List1 using iterator with FOR --");  
           for (Iterator<String> flavoursIter = demoList1.iterator(); flavoursIter.hasNext();){  
                 Object element = flavoursIter.next();  
                 System.out.print(element + " ");  
             }  
           List<String> demoList2 = new ArrayList<String>();  
           demoList2.add("E");  
           demoList2.add("F");  
           demoList2.add("G");  
           demoList2.add("H");  
           System.out.println();  
           System.out.println("-- Prinitng elements of List2 with For-Each loop --");  
           for (String element : demoList2) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("--------------------------------------------");  
           System.out.println("-- start of Collection interface methods --");  
           System.out.println("--------------------------------------------");  
           List<String> demoList3 = new ArrayList<String>();  
           System.out.println("* demoList3.isEmpty() => " + demoList3.isEmpty());  
           System.out.println("* demoList1.contains('B') => "  
                     + demoList1.contains("B"));  
           System.out.println("* demoList1.contains('E') => "  
                     + demoList1.contains("E"));  
           System.out.println("* demoList2.size() => " + demoList2.size());  
           System.out.println("-- adding elements to List3 --");  
           demoList3.add("I");  
           demoList3.add("J");  
           demoList3.add("G");  
           for (String element : demoList3) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("------------------------------------------------");  
           System.out.println("-- start of Collection interface bulk methods --");  
           System.out.println("------------------------------------------------");  
           System.out.println("* demoList1.containsAll(demoList3) => "  
                     + demoList1.containsAll(demoList3));  
           System.out.println("* demoList2.containsAll(demoList3) => "  
                     + demoList2.containsAll(demoList3));  
           System.out.println("* demoList2.addAll(demoList3) => "  
                     + demoList2.addAll(demoList3));  
           System.out.println("-- Printing demoList2 after addAll() --");  
           for (String element : demoList2) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("* demoList2.retainAll(demoList3) => "  
                     + demoList2.retainAll(demoList3));  
           System.out.println("-- Printing demoList2 after retainAll() --");  
           for (String element : demoList2) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("* demoList2.removeAll(demoList3) => "  
                     + demoList2.removeAll(demoList3));  
           System.out.println("-- demoList2 size after demoList2.removeAll() => "  
                     + demoList2.size());  
           demoList3.clear();  
           System.out.println();  
           System.out.println("* demoList3.clear() => " + demoList3.size());  
           System.out.println("----------------------------------------------------------");  
           System.out.println(" == removing element 'C' from demoList1 using iterator ==");  
           System.out.println("----------------------------------------------------------");  
           Iterator<String> itr2 = demoList1.iterator();  
           while (itr2.hasNext()) {  
                if (itr2.next().equals("C")) {  
                     itr2.remove();  
                }  
           }  
           for (String element : demoList1) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("----------------------------------------------");  
           System.out.println("== converting list1 to array with toArray() ==");  
           System.out.println("----------------------------------------------");  
           Object[] arr = demoList1.toArray();  
           for (Object element : arr) {  
                System.out.println("Element = " + element);  
           }  
           System.out.println("---------------------------------------------------------");  
           System.out.println("== converting list1 to array again with toArray(T[] a) ==");  
           System.out.println("---------------------------------------------------------");  
           String[] strArr = demoList1.toArray(new String[demoList1.size()] );  
           for (Object element : strArr) {  
                System.out.println("Element = " + element);  
           }  
      }  
 }  

Employee Class -
 package com.demo.model;  
 import java.util.Comparator;  
 public class Employee implements Comparable<Employee> {  
      private String name;  
      private int age;  
      private double salary;  
      public Employee(String name, int age, double salary) {  
           super();  
           this.name = name;  
           this.age = age;  
           this.salary = salary;  
      }  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public int getAge() {  
           return age;  
      }  
      public void setAge(int age) {  
           this.age = age;  
      }  
      public double getSalary() {  
           return salary;  
      }  
      public void setSalary(double salary) {  
           this.salary = salary;  
      }  
      @Override  
      public int compareTo(Employee emp) {  
           // TODO Auto-generated method stub  
           return (this.age - emp.age);  
      }  
      @Override  
      public String toString() {  
           return "Employee [name=" + name + ", age=" + age + ", salary=" +salary +"]";  
      }  
      public static Comparator<Employee> NameComparator = new Comparator<Employee>() {  
     @Override  
     public int compare(Employee e1, Employee e2) {  
       return e1.getName().compareTo(e2.getName());  
     }  
   };  
   public static Comparator<Employee> SalaryComparator = new Comparator<Employee>() {  
     @Override  
     public int compare(Employee e1, Employee e2) {  
       return (int)(e1.getSalary() - e2.getSalary());  
     }  
   };  
 }  
OUTPUT 

-- Printing elements of List1 using iterator with WHILE --
A B C D
-- Printing elements of List1 using iterator with FOR --
A B C D
-- Prinitng elements of List2 with For-Each loop --
E F G H
--------------------------------------------
-- start of Collection interface methods --
--------------------------------------------
* demoList3.isEmpty() => true
* demoList1.contains('B') => true
* demoList1.contains('E') => false
* demoList2.size() => 4
-- adding elements to List3 --
I J G
------------------------------------------------
-- start of Collection interface bulk methods --
------------------------------------------------
* demoList1.containsAll(demoList3) => false
* demoList2.containsAll(demoList3) => false
* demoList2.addAll(demoList3) => true
-- Printing demoList2 after addAll() --
E F G H I J G
* demoList2.retainAll(demoList3) => true
-- Printing demoList2 after retainAll() --
G I J G
* demoList2.removeAll(demoList3) => true
-- demoList2 size after demoList2.removeAll() => 0

* demoList3.clear() => 0
----------------------------------------------------------
 == removing element 'C' from demoList1 using iterator ==
----------------------------------------------------------
A B D
----------------------------------------------
== converting list1 to array with toArray() ==
----------------------------------------------
Element = A
Element = B
Element = D
---------------------------------------------------------
== converting list1 to array again with toArray(T[] a) ==
---------------------------------------------------------
Element = A
Element = B
Element = D

No comments: