00:00
cl1.png /* O10 - set */ import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class O10 { public static void main(String[] args) { Set<Integer> s1 = new HashSet<>(); Set<Integer> s2 = new HashSet<>(); Set<Integer> s3 = new HashSet<>(); /* No index */ s1.add(100); s1.add(200); System.out.println(s1); // [100, 200] /* Duplicate : One */ s2.add(10); s2.add(10); System.out.println(s2); // [10] /* Null : One */ s3.add(10); s3.add(null); s3.add(30); s3.add(null); System.out.println(s3); // [null, 10, 30] /* Iterator m/t */ Iterator<Integer> itr = s3.iterator(); while(itr.hasNext()){ System.out.println(itr.next());// print one by one } } }
cl2.png /* O11 - Iterator / List Iterator has + hasnext m/t */ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class O11 { public static void main(String[] args) { List<Object> l1 = new ArrayList<>(); l1.add(10); l1.add("deepak"); l1.add("rahul"); System.out.println(l1);// [10, deepak, rahul] /* Iterator */ Iterator<Object> itr1 = l1.iterator(); while(itr1.hasNext()){ System.out.println(itr1.next()); // one by one } /* ListIterator */ ListIterator<Object> li1 = l1.listIterator(); while(li1.hasNext()){ System.out.println(li1.next()); // one by one }
/* has previous mt */ List<Object> l2 = new ArrayList<>(); l2.add(10); l2.add("deepak"); l2.add("rahul"); ListIterator<Object> li2 = l2.listIterator(); /* put cursor at end */ while(li2.hasNext()){ System.out.println(li2.next()); // top to botton } /* shift cursor from end */ while(li2.hasPrevious()){ System.out.println(li2.previous()); // bottom to top } /* second way */ li2.next(); li2.next(); while(li2.hasPrevious()){ System.out.println(li2.previous()); // deepak to top }
/* List remove, add, set m/t */ List<Object> l3 = new ArrayList<>(); l3.add(10); l3.add("deepak"); l3.add("rahul"); l3.add(100); l3.add(223); l3.add('c'); l3.add("book"); System.out.println("add = " +l3);// add = [10, deepak, rahul, 100, 223, c, book] /* object */ l3.remove("rahul"); // sop.l3 : [10, deepak, 100, 223, c, book] /* index number */ l3.remove(2);// sop.l3 : [10, deepak, 223, c, book] /* set ,replace , return previous object */ System.out.println(l3.set(0, "copy")); // sop.l3 : [copy, deepak, 223, c, book]
/* List more methods */ /* at default 0 */ List<Integer> l4 = new ArrayList<>(); l4.add(10); /* add + index */ l4.add(1,20);//sop.l4 : [10, 20] List<Integer> l5 = new ArrayList<>(); /* add index 1, must, 0 index val.*/ l5.add(1,30);// java.lang.IndexOutOfBoundsException: List<Integer> l6 = new ArrayList<>(); /* 25, at index 0 and then 35 */ l6.add(25); l6.add(35);// sop.l6 : // [25, 35] System.out.println("zz="+l6); List<Integer> l7 = new ArrayList<>(); /* Duplicate */ l7.add(10); l7.add(10); // sop.l7 : [10, 10] List<Integer> l8 = new ArrayList<>(); /* Null */ l8.add(10); l8.add(null); l8.add(null);// sop.l8 : [10, null, null] } }
cl4.png /* O18 - ArrayList m/t */ import java.util.ArrayList; import java.util.Iterator; public class O18 { public static void main(String[] args) { /* wrong index */ ArrayList<Object> al1 = new ArrayList<>(); al1.add(1,200); // e/X outOfBound ArrayList<Object> al2 = new ArrayList<>(); al2.add(0,"a1"); al2.add(1,"a2"); al2.add(2,"a3"); /* index add */ al2.add("c1"); al2.add("c2"); al2.add(1,"c4"); al2.add("c3"); al2.add("c4"); /* get */ al2.get(0); /* hetrogeneous */ al2.add(100); al2.add("cup"); al2.add('D'); /* duplicate */ al2.add(500); al2.add(500); /* multiple null */ al2.add(null); al2.add(null); /* Sorting No*/ al2.add(20); al2.add(30); al2.add(10); al2.add(40); al2.add(50); System.out.println(al2); // [a1, c4, a2, a3, c1, c2, c3, c4, 100, cup, D, 500, 500, null, null, 20, 30, 10, 40, 50]
/* add, addAll, remove, clear - - - - - - - - - - - - - - - - - - - - - */ ArrayList<Integer> al3 = new ArrayList<>(); al3.add(100); al3.add(200); al3.add(300);// sop : [100, 200, 300] ArrayList<Object> al4 = new ArrayList<>(); al4.add('a'); al4.add('b'); al4.add('c'); al4.add("rajat"); // sop : [a, b, c, rajat] /* addAll */ al4.addAll(al3); //sop.al4 : [a, b, c, rajat, 100, 200, 300] /* object remove */ al4.remove("rajat");// sop.al4 : [a, b, c, 100, 200, 300] /* index remove, */ al4.remove(2); //sop.al4 : [a, b, 100, 200, 300] /* removeAll, comman obj. return */ al4.removeAll(al3); //sop.al4 : [a, b] /* clear, Del all obj. */ al4.clear();//sop.al4 : []
/* contain, containall, size, get, set, indexof, iterator- - - - - - - - - - - - - */ ArrayList<Object> al5 = new ArrayList<>(); al5.add(100); al5.add(200); al5.add(300); ArrayList<Object> al6 = new ArrayList<>(); al6.add('a'); al6.add('b'); al6.add('c'); al6.add("rajat"); /* add all */ al6.addAll(al5);//sop.al4 : [a, b, c, rajat, 100, 200, 300] /* contains */ al6.contains("rajat");//sop : true /* containAll */ al6.containsAll(al5);//sop : true /* size */ al6.size();// sop : 7 /* get */ al6.get(3);// sop : rajat /* set , return previous element on this place */ al6.set(3,66);//sop : rajat /* indexOf */ al6.indexOf(66);//sop : 3 /* Iterator */ Iterator<Object> itr = al6.iterator(); while(itr.hasNext()){ System.out.println(itr.next());// one by onw }
/* c/t capacity = 10, size = 0 */ ArrayList<Integer> al7 = new ArrayList<>(); al7.add(100); al7.add(200); al7.add(300); al7.add(400); /*c/t capacity = 6, size = 0 */ ArrayList<Integer> al8 = new ArrayList<>(6); /* c/t copy obj. one to other */ ArrayList<Integer> al9 = new ArrayList<>(al7); // sop.al9 : [100, 200, 300, 400]
/* another code - - - - - - - */ ArrayList<Object> al10 = new ArrayList<>(); al10.add(100); al10.add("Depak"); al10.add('C');// sop : [100, Depak, C] /* add */ ArrayList<Object> al11 = new ArrayList<>(); al11.add("aaa"); al11.add("bbb"); // sop : [aaa, bbb] /* add return TRUE*/ al11.add("ddd"); //sop : true /* add */ ArrayList<Integer> al12 = new ArrayList<>(); al12.add(100); al12.add(200); al12.add(300); // sop : [100, 200, 300] /* addAll */ al11.addAll(al12);// sop.al11 : [aaa, bbb, ccc, ddd, 100, 200, 300] /* isEmpty */ al11.isEmpty(); // sop : false /* remove() */ //al11.remove(100); // sop : outofboundexcepiton al11.remove(0); //sop : aaa al11.remove("ccc"); //sop : false /* removeAll(), common */ al11.removeAll(al12); //sop : true System.out.println("12= "+al11); // [bbb, ddd] } }
cl5.png cl6.png /* O20 - LinkedList m/t, Contains Collection List and Deque m/t */ import java.util.LinkedList; public class O20 { public static void main(String[] args) { LinkedList<Object> l1 = new LinkedList<>(); /* hetrogeneous */ l1.add("Cub"); l1.add("Cat"); l1.add(200); /* duplicate */ l1.add("Den"); l1.add("Dot"); /* multiple null */ l1.add(null); l1.add(null); /* sorting order : NO */ l1.add(1); l1.add(4); l1.add(3); l1.add(2); /* follow insertion order */ System.out.println(l1); // [Cub, Cat, 200, Den, Dot, null, null, 1, 4, 3, 2] LinkedList<String> l2 = new LinkedList<>(); l2.add("cup"); l2.add("tea"); l2.add("usa"); l2.add("bal"); l2.add("cab"); l2.add("dad"); l2.add("mad");// sop.l2 : [cup, tea, usa, bal, cab, dad, mad] /* add first */ l2.addFirst("arm");// sop.l2 : [arm, cup, tea, usa, bal, cab, dad, mad] /* add last */ l2.addLast("ink");// sop.l2 : [arm, cup, tea, usa, bal, cab, dad, mad, ink] /* remove first */ l2.removeFirst();// sop.l2 : [cup, tea, usa, bal, cab, dad, mad, ink] /* remove last */ l2.removeLast(); // sop.l2 : [cup, tea, usa, bal, cab, dad, mad] /* get first */ l2.getFirst(); // sop = cup /* get last */ l2.getLast(); // sop = mad /* c/t - only one */ LinkedList<String> l3 = new LinkedList<>(l2); System.out.println(l3);// [cup, tea, usa, bal, cab, dad, mad] } }
cl7.png /* O22 - Legacy classes || vector + stack m/t*/ import java.util.Enumeration; import java.util.Stack; import java.util.Vector; public class O22 { public static void main(String[] args) { Vector<Object> v1 = new Vector<>(); /* hetrogeneous */ v1.addElement("cup"); v1.add(200); /* Duplicate */ v1.add("hat"); v1.add("hat"); /* Multiple Null */ v1.add(null); v1.add(null); /*Sorting No */ v1.add(1); v1.add(4); v1.add(3); v1.add(2);// sop.v1 : [cup, 200, hat, hat, null, null, 1, 4, 3, 2] /* add first */ v1.addFirst("mat");// sop.v1 : [mat, cup, 200, hat, hat, null, null, 1, 4, 3, 2] /* add last */ v1.addLast("bus"); // sop.v1 : [mat, cup, 200, hat, hat, null, null, 1, 4, 3, 2, bus] /* first element ,last element */ v1.firstElement();// sop : mat v1.lastElement();// sop : bus /* set element At */ v1.capacity(); // sop : 20 default v1.setElementAt("cat",3); //sop.v1 : [mat, cup, 200, cat, hat, null, null, 1, 4, 3, 2, bus] /* remove */ v1.removeElement("mat"); // sop.v1 : [cup, 200, cat, hat, null, null, 1, 4, 3, 2, bus] v1.removeElementAt(2); // sop.v1 : [cup, 200, hat, null, null, 1, 4, 3, 2, bus] v1.removeAllElements(); // sop.v1 : [] /* c/t capacity 10 */ Vector<Object> v2 = new Vector<>(); // create 10 capacity vector /* ct capacity 6 */ Vector<Object> v3 = new Vector<>(6); // capacity user defined v3.capacity(); // sop 6 /* ct (initial, increment) */ Vector<Object> v4 = new Vector<>(20,2); // capacity user defined v4.capacity(); // sop 20 Vector<Object> v5 = new Vector<>(); v5.addElement("kapli"); v5.addElement("sharma"); /* c/t Capacity 6*/ v5.capacity();// sop 10 /* add element */ v5.addElement(1); v5.addElement(2); v5.addElement(3); v5.addElement(4); v5.addElement(5); v5.addElement(6); v5.addElement(7); v5.addElement(8); v5.addElement(9); v5.addElement(10); /* new capacity after adding 10 object*/ v5.capacity(); //sop 20 /* ct- copy one to another */ Vector<Object> v6 = new Vector<>(v5); // copy v5 object to v6 System.out.println(v6);// [kapli, sharma, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Vector<Object> v7 = new Vector<>(); v7.addElement("kaplisharma"); v7.capacity(); // sop 10
cl14.png /* stack mt and ct - - - - - -- - - */ Stack<String> s = new Stack<>(); s.push("Delhi"); s.push("Mumbai"); s.push("Bihar"); s.push("MP"); s.push("UP"); s.push("AP"); s.push("HR"); /* mt*/ System.out.println(s.pop()); //HR System.out.println(s.peek());// AP System.out.println(s.search("MP"));//3 System.out.println(s.search("bhopal")); //-1 System.out.println(s.empty());// false
cl3.png /* Enumeration ( cursors ) LC */ Vector<Object> v8 = new Vector<>(); v8.add(10); v8.add("deepak"); v8.add(15.2); System.out.println(v8); // [10, deepak, 15.2] /* Enumeration ( cursors ) */ Enumeration<Object> e = v8.elements(); while(e.hasMoreElements()){ System.out.println(e.nextElement()); // one by one } } }
cl8.png /* O25 - Hashset - code */ import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; public class O25 { public static void main(String[] args) { /* Insertion No */ HashSet<Object> hst = new HashSet<>(); hst.add(10); /* all element will add into MAP */ hst.add("deepak"); hst.add(100.55); hst.add("xyz"); hst.add(true); //sop.hst : [deepak, xyz, 10, 100.55, true] /* Null One */ HashSet<Object> hst1 = new HashSet<>(); hst1.add(null); hst1.add(null); //sop.hst1 : [null] /* Duplicate One */ HashSet<Object> hst2 = new HashSet<>(); hst2.add("rajat"); hst2.add("rajat");// sop.hst2 : [rajat] /* Sorting No */ HashSet<Object> hst3 = new HashSet<>(); hst3.add(20); hst3.add(30); hst3.add(40); hst3.add(10); // sop.hst3 : [20, 40, 10, 30] /* Iterator m/t */ Iterator<Object> itr = hst.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); // one by one , using hashcode } /* add All */ ArrayList<Object> al = new ArrayList<>(); al.add("apple"); al.add("mango"); al.add("banana"); al.add("orange"); al.add("grapes"); HashSet<Object> hst4 = new HashSet<>(); hst4.addAll(al); System.out.println("AFTER ADDALL = "+hst4);//sop : [banana, orange, apple, mango, grapes] /* contains */ hst4.contains("manoj"); // sop : false /* contains All */ hst4.containsAll(al); // sop : true /* remove */ hst4.remove("grapes"); //sop : true /* clear */ hst4.clear(); System.out.println("AFTER-CLEAR = "+hst4); // sop : [] } }
cl12.png /* O26 - TreeSet - code , 4 = c/t */ import java.util.TreeSet; public class O26 { public static void main(String[] args) { TreeSet<Object> ts = new TreeSet<>(); /* add numbers */ ts.add(10); ts.add(20); /* add text */ TreeSet<Object> ts1 = new TreeSet<>(); ts1.add("deepak"); ts1.add("amit"); /* add text + numbers */ TreeSet<Object> ts2 = new TreeSet<>(); ts2.add("deepesh"); ts2.add(10); //java.lang.ClassCastException /* add number + text */ TreeSet<Object> ts3 = new TreeSet<>(); ts3.add(10);//java.lang.ClassCastException ts3.add("deepak"); /* Null No*/ TreeSet<Object> ts4 = new TreeSet<>(); ts4.add(null); //java.lang.NullPointerException TreeSet<Object> ts5 = new TreeSet<>(); ts5.add("apple"); ts5.add("guava"); ts5.add("potato"); /* remove */ ts5.remove("potato"); /* clear */ ts5.clear(); System.out.println("CLEAR = "+ts5); } }
cl9.png /* O27 - Map theory */ import java.util.HashMap; import java.util.Map; public class O27 { public static void main(String[] args) { Map<Integer,String> mp = new HashMap<>(); /* add */ mp.put(101, "rajat"); mp.put(102, "manoj"); mp.put(103, "kamal"); mp.put(105, "deepak"); System.out.println("01 = "+mp);//01 ={101=rajat, 102=manoj, 103=kamal, 105=deepak} System.out.println("02 = "+mp.containsKey(102));//true System.out.println("03 = "+mp.containsValue("kamal"));//true System.out.println("04 = "+mp.get(102));//manoj System.out.println("05 = "+mp.isEmpty());//false /* remove */ mp.remove(104); System.out.println("06 = "+mp);//06 = {101=rajat, 102=manoj, 103=kamal, 105=deepak} /* remove key+value */ mp.remove(103, "kamal"); System.out.println("07 = "+mp);//07 = {101=rajat, 102=manoj, 105=deepak} /* replace(x,x,x) */ mp.replace(105, "deepak", "mango"); System.out.println(mp);//{101=rajat, 102=manoj, 105=mango} /* size */ System.out.println("08 = "+mp.size());//3 Map<Integer,String> mp1 = new HashMap<>(); mp1.put(101, "rajat"); System.out.println("09 = "+mp1);//09 = {101=rajat} Map<Object,Object> mp2 = new HashMap<>(); /* Null key, one value many */ mp2.put(null, null); mp2.put(null, "aaa"); System.out.println("10 = "+mp2);//10 = {null=aaa} Map<Integer,String> mp3 = new HashMap<>(); mp3.put(108, "bbb"); mp3.put(105, "ccc"); System.out.println("11 = "+mp3);//11 = {105=ccc, 108=bbb} } }
cl10.png /* O28 - HashMap - m/t c/t(4) */ import java.util.Set; import java.util.Iterator; import java.util.Map; import java.util.HashMap; public class O28 { public static void main(String[] args) { HashMap<Integer,String> hm = new HashMap<>(); hm.put(101, "deepak"); hm.put(102, "amit"); hm.put(103, "rahul"); hm.put(104, "kamal"); System.out.println("USING PUT M/T = "+hm); Set<Map.Entry<Integer, String>> set = hm.entrySet(); System.out.println("USING SET M/T = "+set); /* ITERATOR */ Iterator<Map.Entry<Integer, String>> itr = set.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } /* getkey get value , while loop */ Iterator<Map.Entry<Integer, String>> itr1 = set.iterator(); while(itr1.hasNext()){ Map.Entry<Integer,String> entry = itr1.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } /*getkey get value , for each */ HashMap <Integer,String>hm1 = new HashMap<>(); hm1.put(101, "deepak"); hm1.put(102, "amit"); hm1.put(103, "rahul"); hm1.put(104, "kamal"); for(Map.Entry<Integer,String> me : hm1.entrySet()){ System.out.println(me.getKey()+" "+me.getValue()); } /* add (put) */ System.out.println("BEFORE USING PUT M/T = "+hm1); hm1.put(105,"rajat"); System.out.println("AFTER USING PUT M/T = "+hm1); /* same key with different value */ HashMap<Integer,String> hm2 = new HashMap<>(); hm2.put(101, "deepak"); hm2.put(101, "amit"); hm2.put(103, "rahul"); System.out.println("SAME KEY WITH DIFFERENT VALUES = "+hm2); /* null value OK */ HashMap<Integer,String> hm3 = new HashMap<>(); hm3.put(101, null); hm3.put(102, "amit"); System.out.println(hm3); System.out.println("ONE NULL VALUES = "+hm2); /* null keyS ONE */ HashMap<Object,String> hm4 = new HashMap<>(); hm4.put(null, "aaa"); hm4.put(null, "ccc"); System.out.println("MULTIPLE NULL KEY = "+hm4); /* null valueS OK */ HashMap<Integer,String> hm5 = new HashMap<>(); hm5.put(101, null); hm5.put(102, null); System.out.println("MULTIPLE NULL VALUES = "+hm5); /* remove */ HashMap<Integer,String> hm6 = new HashMap<>(); hm6.put(101, "deepak"); hm6.put(102, "amit"); hm6.put(103, "rahul"); hm6.put(104, "kamal"); hm6.put(105,"mangoo"); System.out.println("REMOVE M/T = "+hm6.remove(103)); /* contains, */ System.out.println("CONTAINSKEY = " +hm6.containsKey(103)); System.out.println("CONTAINSVALUE = "+hm6.containsValue("amit")); System.out.println("GET M/T = "+hm6.get(101)); System.out.println("get the key which is not present = "+hm6.get(107)); System.out.println("isEmpty = "+hm6.isEmpty()); System.out.println("replace m/t = "+hm6.replace(105,"mangoo")); System.out.println("size m/t = "+hm6.size()); } }
cl11.png /* 030 - TreeMap m/t c/t = 4 */ import java.util.TreeMap; public class O30 { public static void main(String[] args) { TreeMap<Integer,String> tm = new TreeMap<>(); tm.put(101, "deepak"); tm.put(102, "amit"); tm.put(103, "rahul"); /* SORTING : yes */ System.out.println(tm);// {101=deepak, 102=amit, 103=rahul} TreeMap<String,Integer> tm1 = new TreeMap<>(); tm1.put( "kamal",104); tm1.put( "mango",105); tm1.put( "apple",106); tm1.put( "mobile",107); tm1.put( "mouse",108); /* CAN SORT TEXT ALSO */ System.out.println(tm1);// {apple=106, kamal=104, mango=105, mobile=107, mouse=108} TreeMap<Integer,String> tm2 = new TreeMap<>(); tm2.put(101, "deepak"); tm2.put(102, "amit"); tm2.put(103, "rahul"); tm2.put(104, "kamal"); tm2.put(106, "mango"); tm2.put(107, "apple"); tm2.put(108, "mobile"); tm2.put(109, "mouse"); tm2.put(110, "watch"); tm2.put(111, "book"); tm2.put(112, "chair"); tm2.put(113, "computer"); tm2.put(114, "chain"); tm2.put(115, "pad"); tm2.ceilingEntry(103);// sop.tm2 : 103=rahul tm2.floorEntry(108); //sop.tm2 : 108=mobile tm2.ceilingKey(103);// sop.tm2 : 103 /* if key not present */ tm2.ceilingEntry(105); // sop.tm2 : 106=mango tm2.floorEntry(105); // sop.tm2 : 104=kamal tm2.ceilingKey(105);// sop.tm2 : 106 tm2.containsKey(106);// true tm2.containsValue("mobile");// true tm2.firstEntry(); // 101=deepak tm2.get(103); // rahul tm2.get(100);// null tm2.headMap(102); // {101=deepak} /* higher if key not present */ tm2.higherKey(103);//104 tm2.higherKey(100);//101 tm2.higherKey(1000);//null tm2.keySet();//[101, 102, 103, 104, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115] tm2.pollFirstEntry();// sop : 101=deepak System.out.println("RETRUN REST ENTRY = "+tm2); // RETRUN REST ENTRY = {102=amit, 103=rahul, 104=kamal, 106=mango, 107=apple, // 108=mobile, 109=mouse, 110=watch, 111=book, 112=chair, 113=computer, 114=chain} tm2.pollLastEntry();// sop : 115=pad System.out.println("RETRUN REeST ENTRY = "+tm2); // {102=amit, 103=rahul, 104=kamal, 106=mango, 107=apple, 108=mobile, 109=mouse, // 110=watch, 111=book, 112=chair, 113=computer, 114=chain} tm2.replace(102,"eee");// sop : amit tm2.size(); // sop : 12 tm2.subMap(102,106);// sop : {102=eee, 103=rahul, 104=kamal} tm2.remove(109);// sop : mouse // tm2.comparator(); } }
Home PREVIOUS NEXT