PRACTICAL 9
Collection API
9-(1)
1. Write a program to demostrate user of ArrayList, LinkedList ,LinkedHashMap,
TreeMap and HashSet Class. And also implement CRUD operation without database
connection using Collection API.
package com.company; import java.util.*; public class Main {public static void main(String args[]){ Vector<Integer> v1=new Vector<Integer>(); v1.add(4);v1.add(6);v1.add(2);v1.add(1); System.out.println("ARRAY LIST");ArrayList<Integer> al=new ArrayList<Integer>(); al.addAll(v1);al.add(3); System.out.println(al); al.clear(); System.out.println(al);System.out.println("LINKED LIST"); LinkedList<Integer> ll = new LinkedList<Integer>(); ll.addAll(v1);ll.add(9);ll.add(7); ll.addLast(3); ll.addFirst(0); ll.add(4, 8); System.out.println(ll); ll.remove(2);ll.remove(3); ll.removeFirst(); ll.removeLast(); if(ll.contains(7)) {System.out.println("linked list contains object 7 of type Integer");}System.out.println(ll); System.out.println("LINKED HASH MAP"); LinkedHashMap<Integer, Integer> lhm= new LinkedHashMap<Integer, Integer>(); lhm.put(1,73);lhm.put(2, 47);lhm.put(3, 45);lhm.put(4, 24); System.out.println(lhm);System.out.println("Getting value for key 1: "+ lhm.get(1)); System.out.println("Size of the map: "+ lhm.size()); System.out.println("Is map empty? " + lhm.isEmpty()); System.out.println("Contains key 2? "+ lhm.containsKey(2));System.out.println("Contains value 24 and 47? "+ lhm.containsValue(47 + 24)); System.out.println("delete element 1: "+ lhm.remove(1)); System.out.println("Mappings of LinkedHashMap : "+ lhm); System.out.println("TREE MAP");TreeMap<String, Integer> num = new TreeMap<>(); num.put("One", 1);num.put("Two", 2);num.put("Three", 3); System.out.println("TreeMap: " + num); int value1 = num.remove("Two");System.out.println("Removed value: " + value1); boolean result1 = num.remove("Three", 3);System.out.println("Is the entry {Three=3} removed? " + result1); System.out.println("Updated TreeMap: " + num); System.out.println("HASHSET");HashSet<Integer> evenNumber = new HashSet<>(); evenNumber.add(2);evenNumber.add(4); evenNumber.add(6);System.out.println("HashSet: " + evenNumber); HashSet<Integer> numbers2 = new HashSet<>(); numbers2.addAll(evenNumber); numbers2.add(5);System.out.println("New HashSet: " + numbers2);}}
CRUD OPERATION:
package com.company; import java.util.*;class Employee{ private int empno;private String ename; private int salary;Employee(int empno, String ename, int salary){ this.empno = empno;this.ename = ename; this.salary = salary;}public int getEmpno(){ return empno;}public int getSalary(){ return salary;}public String getEname(){ return ename;}public String toString(){return empno+" "+ename+" "+salary;}}class CRUDDemo{public static void main(String[] args) {List<Employee> c = new ArrayList<Employee>(); Scanner s = new Scanner(System.in);Scanner s1 = new Scanner(System.in); int ch;do{System.out.println("1.INSERT"); System.out.println("2.DISPLAY"); System.out.println("3.SEARCH"); System.out.println("4.DELETE"); System.out.println("5.UPDATE"); System.out.print("Enter Your Choice : "); ch = s.nextInt();switch(ch){ case 1:System.out.print("Enter Empno : "); int eno = s.nextInt();System.out.print("Enter EmpName : "); String ename = s1.nextLine(); System.out.print("Enter Salary : ");int salary = s.nextInt();c.add(new Employee(eno,ename,salary)); break;case 2:Iterator<Employee> i = c.iterator(); while(i.hasNext()){Employee e = i.next(); System.out.println(e);}break; case 3:boolean found = false; System.out.print("Enter Empno to Search :"); int empno = s.nextInt();i = c.iterator(); while(i.hasNext()){Employee e = i.next(); if(e.getEmpno() == empno) {System.out.println(e); found = true;}}if(!found){System.out.println("Record Not Found");}break;case 4:found = false;System.out.print("Enter Empno to Delete :"); empno = s.nextInt();i = c.iterator(); while(i.hasNext()){Employee e = i.next(); if(e.getEmpno() == empno) {i. remove(); found = true;}}if(!found){System.out.println("Record Not Found");}else{System.out.println("Record is Deleted Successfully...!");}break; case 5:found = false;System.out.print("Enter Empno to Update :"); empno = s.nextInt();ListIterator<Employee>li = c.listIterator(); while(li.hasNext()){Employee e = li.next(); if(e.getEmpno() == empno) {System.out.print("Enter new Name : "); ename = s1.nextLine(); System.out.print("Enter new Salary : "); salary = s.nextInt();li.set(new Employee(empno,ename,salary)); found = true;}}if(!found){System.out.println("Record Not Found");}else{System.out.println("Record is Updated Successfully...!");}break;}}while(ch!=0);}}
9-(2)
Write a program
to Sort Array,ArrayList,String,List,Map and Set.package com.company;import java.util.*; public class Main {public static void main(String[] args) { System.out.println("Array");int[] arr = {55,69,6,73,8};Arrays.sort(arr);System.out.printf("Sorted arr[] = %s",Arrays.toString(arr)); System.out.println("ARRAYLIST");ArrayList<String>list = new ArrayList<String>(); list.add("sorting");list.add("java"); list.add("arraylist"); list.add("in");System.out.println("Unsorted ArrayList: "+ list); Collections.sort(list);System.out.println("Sorted ArrayList "+ "in Ascending order :"+ list); System.out.println("STRING");String inputString = "Hello BROS ";char Array1[] = inputString.toCharArray(); Arrays.sort(Array1);String outputString =new String(Array1); System.out.println("Input String : " + inputString); System.out.println("Output String : " + outputString); System.out.println("LIST");Integer[] digits = new Integer[] {15,57,80,25,24,9,89,69,59}; List<Integer> digitsList = Arrays.asList(digits);System.out.println("Unsorted List :"+digitsList); Collections.sort(digitsList); System.out.println("Sorted List :" +digitsList); System.out.println("Map");HashMap<Integer, String> map = new HashMap<>(); map.put(14, "VEDANT");map.put(2, "GCET");map.put(30, "IT");map.put(5, "12002080501073");TreeMap<Integer, String> treeMap = new TreeMap<>(map); System.out.println(treeMap);System.out.println("SET");HashSet<Integer> numbersSet = new LinkedHashSet<>(Arrays.asList(17,58,89,23,24,8,87,68,39) );List<Integer> numbersList = new ArrayList<Integer>(numbersSet); Collections.sort(numbersList);numbersSet = new LinkedHashSet<>(numbersList); System.out.println(numbersSet);}}
0 Comments