|
| 1 | +packagecoreConcepts; |
| 2 | + |
| 3 | +importjava.util.stream.Collectors; |
| 4 | + |
| 5 | +importjava.util.*; |
| 6 | +importjava.util.ArrayList; |
| 7 | +importjava.util.HashSet; |
| 8 | +importcoreConcepts.Overloading; |
| 9 | +importcoreConcepts.Pojo; |
| 10 | + |
| 11 | +publicclassCollectionssample { |
| 12 | + |
| 13 | +publicvoidcalc() { |
| 14 | +inti=0; |
| 15 | +intc =0; |
| 16 | +intkey=0; |
| 17 | +Stringvalue =null; |
| 18 | +Mapm=newHashMap<>(); |
| 19 | +Scannersc=newScanner(System.in); |
| 20 | +System.out.println("enter the count"); |
| 21 | +try { |
| 22 | +c=sc.nextInt(); |
| 23 | +} |
| 24 | +catch(Exceptione) { |
| 25 | +System.out.println("plz type Integer only" +e); |
| 26 | +calc(); |
| 27 | +} |
| 28 | +while(i<c) { |
| 29 | +System.out.println("Enter the Key"); |
| 30 | +try { |
| 31 | +key=sc.nextInt(); |
| 32 | +} |
| 33 | +catch(Exceptione) { |
| 34 | +System.out.println("Only Integer is allowed" +e); |
| 35 | +calc(); |
| 36 | +} |
| 37 | + |
| 38 | +System.out.println("Enter the value"); |
| 39 | +try { |
| 40 | +if(sc.hasNext("[A-Za-z]*")) { |
| 41 | +value =sc.next(); |
| 42 | +System.out.println("You entered a string value "+value); |
| 43 | + } |
| 44 | + |
| 45 | +} |
| 46 | +catch(Exceptione) { |
| 47 | +System.out.println("Enter only String" +e); |
| 48 | +calc(); |
| 49 | +} |
| 50 | +m.put(key,value); |
| 51 | +i++; |
| 52 | +} |
| 53 | +System.out.println(m); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +publicstaticvoidmain(String[]args) { |
| 58 | +ArrayListar=newArrayList(); |
| 59 | +ar.add(1); |
| 60 | +ar.add("Ishaq"); |
| 61 | +ar.add("Ahamed"); |
| 62 | +ar.add(null); |
| 63 | +ar.add(19); |
| 64 | +ar.add(21); |
| 65 | + |
| 66 | +Iteratorra=ar.iterator(); |
| 67 | +while(ra.hasNext()) { |
| 68 | +System.out.println(ra.next()); |
| 69 | +} |
| 70 | + |
| 71 | +System.out.println(ar); |
| 72 | + |
| 73 | +List<Integer>l=newLinkedList<>(); |
| 74 | +l.add(5); |
| 75 | +l.add(1); |
| 76 | +l.add(10); |
| 77 | +System.out.println(l.indexOf(7)); |
| 78 | +System.out.println(l.indexOf(10)); |
| 79 | +System.out.println(l.get(2)); |
| 80 | +Collections.sort(l); |
| 81 | +System.out.println(l); |
| 82 | + |
| 83 | +HashSet<Integer>s=newHashSet<>(); |
| 84 | +s.add(10); |
| 85 | +s.add(10); |
| 86 | +s.add(15); |
| 87 | +s.add(25); |
| 88 | +s.add(17); |
| 89 | +s.add(28); |
| 90 | +Iteratorr=s.iterator(); |
| 91 | +while(r.hasNext()) { |
| 92 | +System.out.println(r.next()); |
| 93 | +} |
| 94 | +HashSet<Integer>s1=(HashSet<Integer>)s.stream().filter(x->x%2==1).collect(Collectors.toSet()); |
| 95 | +System.out.println(s1); |
| 96 | +s.addAll(ar); |
| 97 | +System.out.println(s.contains(29)); |
| 98 | +System.out.println(s); |
| 99 | +Stringh=Overloading.getFullName(); |
| 100 | +System.out.println(h); |
| 101 | + |
| 102 | +TreeSet<Integer>t=newTreeSet<>(); |
| 103 | +t.add(15); |
| 104 | +t.add(14); |
| 105 | +t.add(25); |
| 106 | +t.add(10); |
| 107 | +t.add(9);//does not allow null values |
| 108 | +System.out.println(t); |
| 109 | +System.out.println(t.pollFirst()); |
| 110 | +System.out.println(t.pollLast()); |
| 111 | +System.out.println(t.first()); |
| 112 | +System.out.println(t.floor(25));//returns the greatest no in the set which is <= given no |
| 113 | +System.out.println(t.ceiling(12));// returns the least no in the set which is >=given no |
| 114 | + |
| 115 | + |
| 116 | +Collectionssamplecs=newCollectionssample(); |
| 117 | +cs.calc(); |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +} |