26/03/25 ---------- Q)Explain about Comparator interface. =>java.util.Comparator is used in sorting of user defined collection objects. =>compare() method of this interface is used to implement sorting criteria. =>Comparator fecilitates multiple criteria for ordering the collection objects. =>user defined objects of the collection need not implement this interface in order for sorting. =>java.util.Collections class has sort method to perform sorting of objects. Collection.sort(list,Comarator) Q)Example program on Comparator. //ComparatorApplication.java import java.util.*; class Employee{ int empno; String name; float salary; Employee(int empno,String name,float salary){ this.empno=empno; this.name=name; this.salary=salary; } public String toString(){ return empno+"\t"+name+"\t"+salary; } } class NameComparator implements Comparator{ public int compare(Employee e1,Employee e2){ return e1.name.compareTo(e2.name); } } class NumberComparator implements Comparator{ public int compare(Employee e1,Employee e2){ return e1.empno-e2.empno; } } class SalaryComparator implements Comparator { public int compare(Employee e1,Employee e2){ return (int) (e1.salary-e2.salary); } } class ComparatorApplication { public static void main(String[] args) { ArrayList al=new ArrayList<>(); al.add(new Employee(102,"Rama",70000)); al.add(new Employee(103,"Rahim",50000)); al.add(new Employee(101,"Salman",60000)); System.out.println("Before sorting...."); for(Employee e:al) System.out.println(e); System.out.println("After sorting...."); Collections.sort(al,new NameComparator()); for(Employee e:al) System.out.println(e); } } Q)What is a Set based based collection? =>A collection object that implements java.util.Set interface is nothing but a Set based collection. =>In a set based collection, insertion order is not preserved, duplicates are not allowed and at max one null value can be inserted. =>java.util.HashSet is the most frequently used Set based collection class. DIAGRAM Note:- A set based collection abstracts a mathematical set. Q)What are the most frequently used methods of Set interface? 1)size() 2)isEmpty() 3)contains() 4)remove() 5)addAll(collection) 6)retainAll(collection) 7)removeAll(collection) 8)clear() Note:- Set interface has not added any new methods.These are from Collection interface only. Q)Example program on set based collection(union,intersection,difference) import java.util.*; class SetOperations{ public static void main(String[] args) { HashSet one=new HashSet<>(); Collections.addAll(one,10,20,30,40,50); HashSet two=new HashSet<>(); Collections.addAll(two,30,40,50,60,70); /*two.retainAll(one);//Intersection System.out.println(two); one.addAll(two);//Union System.out.println(one); */ one.removeAll(two); //difference of two sets System.out.println(one); } } Q)What is a map based collection? =>An object that implements java.util.Map interface is nothing but a map based collection. =>A map based collection object maps keys to values. =>A map can't contain duplicate keys. =>Each key can be mapped to atmost one value. =>Most frequently used map based collection class is HashMap. DIAGRAM =>Most frequently used methods of Map interface put(K,V) get(K) replace(K,NV) remove(key) Set keySet() Collection values() clear() Q)Example program on HashMap. import java.util.*; class HashMapExample{ public static void main(String[] args){ HashMap hm=new HashMap<>(); hm.put(101,"David"); hm.put(102,"Rama"); hm.put(103,"Rahim");//C of CRUD System.out.println("Emp name of 103 Id:"+hm.get(103));//R of CRUD System.out.println(hm); hm.put(103,"Sharif");//U of CRUD System.out.println(hm); hm.remove(102);//D of CRUD System.out.println(hm); Set keys=hm.keySet(); for(Integer key:keys){ System.out.println(key+":"+hm.get(key)); } Collection values=hm.values(); System.out.println(values); hm.clear(); System.out.println(hm); } }