package array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class Collection_try {
public static void main(String[] args) {
Collection_try ct =new Collection_try();
//ct.learn_list();
//ct.ArrayList();
//ct.hash_set();
//ct.linked_hash_set();
//ct.tree_set();
//ct.remove_duplicate_from_array_using_LinkedHashSet();
//ct.take_duplicate_element();
//ct.remove_duplicate_elements_from_ArrayList();
//ct.arraylist_to_array();
//ct.to_get_specific_datatype_in_collection();
//ct.some_methods_of_sortedset();
//ct.iterator();
//ct.real_Scenario();
//ct.map();
ct.map_question();
}
private void map_question() {
// TODO Auto-generated method stub
String input="today is thrusday";
HashMap<Character,Integer> charfreq=new HashMap<Character,Integer> ();
char[] arr=input.toCharArray();
for(char ch:arr) {
if(!charfreq.containsKey(ch)) {
charfreq.put(ch,1);
}
else {
int value=charfreq.get(ch);
charfreq.put(ch,value+1);
}
}
System.out.println(charfreq);//{ =2, a=2, r=1, s=2, t=2, d=2, u=1, h=1, y=2, i=1, o=1}
int highestCount = Collections.max(charfreq.values());
System.out.println("The highest count of repeated character is : " + highestCount );//2
System.out.println("The highest repeated character is: ");
for (Entry<Character, Integer> entry : charfreq.entrySet()) {//This loop iterates the entrySet() of charfreq,Entry<Character, Integer> to specify the type
if (entry.getValue() == highestCount) {
System.out.print("'" + entry.getKey() + "' ");//The highest repeated character is: ' ' 'a' 's' 't' 'd' 'y'
}
}
}
private void map() {
// TODO Auto-generated method stub
/*HashMap hm= new HashMap();
hm.put(56.6f, "test");//it accept different datatype in key
hm.put(67, 87.9f);
hm.put("chicken briyani ", 145);
hm.put("mutton briyani ", 298);
hm.put("veg briyani ", 125);
hm.put("veg fried rice ", 110);
hm.put("meals ", 110);
System.out.println("hash map "+hm);//{veg fried rice =110, mutton briyani =298, veg briyani =125, chicken briyani =145, meals =110}
hm.put("mutton briyani ", 300);//it update value because using same key
System.out.println("hashmap "+hm);//{veg fried rice =110, mutton briyani =300, veg briyani =125, chicken briyani =145, meals =110}
System.out.println("hashmap "+hm.keySet());//-to take only key[veg fried rice , mutton briyani , veg briyani , chicken briyani , meals ]
System.out.println("hashmap "+hm.values());//[110, 300, 125, 145, 110]*/
/*hash map {67=87.9, 56.6=test, veg fried rice =110, mutton briyani =298, veg briyani =125, chicken briyani =145, meals =110}
hashmap {67=87.9, 56.6=test, veg fried rice =110, mutton briyani =300, veg briyani =125, chicken briyani =145, meals =110}
hashmap [67, 56.6, veg fried rice , mutton briyani , veg briyani , chicken briyani , meals ]
hashmap [87.9, test, 110, 300, 125, 145, 110]*/
LinkedHashMap hm=new LinkedHashMap();
// hm.put(456, "test");//it accept different datatype in key
hm.put("chicken briyani ", 145);
hm.put("mutton briyani ", 298);
hm.put("veg briyani ", 125);
hm.put("veg fried rice ", 110);
hm.put("meals ", 110);
System.out.println("linkedhashmap "+hm);//linkedhashmap {chicken briyani =145, mutton briyani =298, veg briyani =125, veg fried rice =110, meals =110}
hm.put("mutton briyani ", 300);//it update value because using same key
System.out.println("linkedhashmap "+hm);//linkedhashmap {chicken briyani =145, mutton briyani =300, veg briyani =125, veg fried rice =110, meals =110}
System.out.println("linkedhashmap "+hm.keySet());//-to take only key linkedhashmap [chicken briyani , mutton briyani , veg briyani , veg fried rice , meals ]
System.out.println("linkedhashmap "+hm.values());//linkedhashmap [145, 300, 125, 110, 110]
System.out.println("entry set "+hm.entrySet());//print in [] because it is set -entry set [456=test, chicken briyani =145, mutton briyani =300, veg briyani =125, veg fried rice =110, meals =110]
System.out.println("contain key "+hm.containsKey(426));//it check contain or not return boolean type
System.out.println("contain value "+hm.containsValue(110));//true
Set s=hm.entrySet();
Iterator it=s.iterator();
boolean present=it.hasNext();
while(present==true) {
/*System.out.println(it.next());
o/p- print as object ie., before entry typecast:456=test
chicken briyani =145
mutton briyani =300
veg briyani =125
veg fried rice =110
meals =110*/
Entry entry =(Entry) it.next();//collection of entries is map,Entry is inner interface of the map(map is also interface).it typecast because if typecast we can seperate values by getkey and getvalues function
/* System.out.println(entry.getKey()+"==>"+entry.getValue());
456==>test
chicken briyani ==>145
mutton briyani ==>300
veg briyani ==>125
veg fried rice ==>110
meals ==>110
*/
Integer price=(Integer) entry.getValue();//it take value from entry typecast for if condition
if(price>=100&& price<=200) {
System.out.println(entry.getKey());
/*chicken briyani
veg briyani
veg fried rice
meals*/
}
present=it.hasNext();
}
/*TreeMap hm=new TreeMap();//it not accept different datatype
hm.put("chicken briyani ", 145);
hm.put("mutton briyani ", 298);
hm.put("veg briyani ", 125);
hm.put("veg fried rice ", 110);
hm.put("meals ", 110);
System.out.println("treemap "+hm);// treemap {chicken briyani =145, meals =110, mutton briyani =300, veg briyani =125, veg fried rice =110}
hm.put("mutton briyani ", 300);//it update value because using same key
System.out.println("treemap "+hm);//treemap treemap {chicken briyani =145, meals =110, mutton briyani =300, veg briyani =125, veg fried rice =110}
System.out.println("treemap "+hm.keySet());//-to take only key linkedhashmap [chicken briyani , mutton briyani , veg briyani , veg fried rice , meals ]
System.out.println("treemap "+hm.values());//treemap [145, 110, 300, 125, 110]*/
}
private void real_Scenario() {
// TODO Auto-generated method stub
Mobiles m1=new Mobiles("vivo",45000,6);
Mobiles m2=new Mobiles("oneplus",30000,5);
Mobiles m3=new Mobiles("nokia",65000,7);
Mobiles m4=new Mobiles("apple",95000,8);
ArrayList al=new ArrayList();
al.add(m1);
al.add(m2);
al.add(m3);
al.add(m4);
System.out.println(al);//[samsung 45000 6 , oneplus 30000 5 , nokia 65000 7 , apple 95000 8 ]
Iterator it=al.iterator();
boolean bo=it.hasNext();
while(bo!=false) {
Object o=it.next();
Mobiles mobile=(Mobiles) o;
String brand=mobile.model;
// if(brand.contains("vivo")&&mobile.price==30000)
if(brand.contains("vivo")) {
System.out.println(mobile);}
bo=it.hasNext();
//below using price
/*Integer brand=mobile.price;
if(brand>=65000) {
System.out.println(mobile);}
bo=it.hasNext(); */
}
Collections.sort(al);//m1 m2 m3 m4
//this object -m2
//m2.CompareTo(m1) -->
System.out.println("Sort "+al);//Sort [apple 95000 8 , nokia 65000 7 , vivo 45000 6 , oneplus 30000 5 ]
//using-(-,+)Sort [oneplus 30000 5 , vivo 45000 6 , nokia 65000 7 , apple 95000 8 ]
}
private void iterator() {
// TODO Auto-generated method stub
//LinkedList al = new LinkedList();
HashSet al=new HashSet(); // available in set and list
al.add(4);
al.add(10);
// al.add(5.5f);
al.add(50);
al.add(60);
Iterator it=al.iterator();
//it.hasnext()-do you have next element
//it.next();give me the next value
boolean bo=it.hasNext();
/*while(bo!=false) {
System.out.println(it.next());
bo=it.hasNext();
}*/
while(bo!=false) {
Object o=it.next();//in collection are all object
Integer i=(Integer) o;//first we typecast that class object
if(i%10==0) {
System.out.println(i);}
bo=it.hasNext();
}
}
private void some_methods_of_sortedset() {
// TODO Auto-generated method stub
TreeSet lhs = new TreeSet();
lhs.add(10);
lhs.add(20);
lhs.add(30);
lhs.add(40);
lhs.add(50);
lhs.add(60);
//this methods are only in tree set because accending order and same type of element
System.out.println(lhs);//[10, 20, 30, 40, 50, 60]
System.out.println("headset "+lhs.headSet(30));//[10, 20]-it return greaterthan values of given value
System.out.println("tailset "+lhs.tailSet(40));//[40, 50, 60]-it return smallerthan values of given value
System.out.println("first "+lhs.first());//10-it return first value
System.out.println("last "+lhs.last());//60-it return last value
System.out.println("subset "+lhs.subSet(20, 50));//[20, 30, 40]-it return in between value from given values
System.out.println("comparator "+lhs.comparator());//null
System.out.println("ceiling "+lhs.ceiling(15));//20-it return given matching value or nearest greater given value
System.out.println("higher "+lhs.higher(20));//30-it return given only nearest greater given value
System.out.println("size "+lhs.size());//size 6
System.out.println("descending "+lhs.descendingSet());//[60, 50, 40, 30, 20, 10]
System.out.println("pollfirst "+lhs.pollFirst());//pollfirst 10-it shows first element and removed 1st element in that set
System.out.println("after poll first "+lhs);//[20, 30, 40, 50, 60]
System.out.println("polllast "+lhs.pollLast());//polllast 60-it shows last element and removed last element in that set
System.out.println("after poll last "+lhs);//after poll last [20, 30, 40, 50]
System.out.println("is empty "+lhs.isEmpty());//is empty false-it check contains element or not,if contain return false otherwise return false
String s="1234";
Integer.parseInt(s);
System.out.println(s);//1234
//enum-collection of final variable
}
private void to_get_specific_datatype_in_collection() {
// TODO Auto-generated method stub
ArrayList al= new ArrayList();
al.add("sona");
al.add(567);
al.add("way");
al.add(78.9f);
al.add("way");
al.add(5.89f);
al.add(true);
al.add(67.5f);
for(Object s:al) {
try {
Float f=(Float)s;
System.out.println(f);//78.9 5.89 67.5
}
catch(ClassCastException o) {
//System.out.println("its not float");
}
}
}
private void arraylist_to_array() {
// TODO Auto-generated method stub
ArrayList al= new ArrayList();
al.add("sona");
al.add(567);
al.add("way");
al.add(78.9f);
al.add("way");
Object[] ar=al.toArray();
for(int i=0;i<ar.length;i++) {
System.out.println(ar[i]);
}
// System.out.println(Arrays.toString(ar));//[sona, 567, way, 78.9, way]
System.out.println(al);
LinkedHashSet lhs = new LinkedHashSet(al);
System.out.println(lhs);//[sona, 567, way, 78.9]
}
private void remove_duplicate_elements_from_ArrayList() {
// TODO Auto-generated method stub
ArrayList al= new ArrayList();
al.add(5);
al.add(10);
al.add(12);
al.add(5);
al.add(10);
System.out.println(al);//[5, 10, 12, 5, 10]
LinkedHashSet lhs = new LinkedHashSet(al);
System.out.println(lhs);//[5, 10, 12]
}
private void take_duplicate_element() {
// TODO Auto-generated method stub
int arr[]= {10,20,30,10,20,30,10,20,40,50};
LinkedHashSet lhs = new LinkedHashSet();
for(int i=0;i<arr.length;i++) {
if(lhs.add(arr[i])==false) {
System.out.println(arr[i]);//10 20 30 10 20
}
}
System.out.println(lhs);//[10, 20, 30, 40, 50]
}
private void remove_duplicate_from_array_using_LinkedHashSet() {
// TODO Auto-generated method stub
int arr[]= {10,20,30,10,20,30,10,20,30,40,50};
LinkedHashSet lhs = new LinkedHashSet();
for(int i=0;i<arr.length;i++) {
lhs.add(arr[i]);
}
System.out.println(lhs);//[10, 20, 30, 40, 50]
}
private void tree_set() {
// TODO Auto-generated method stub
TreeSet lhs = new TreeSet();
System.out.println( lhs.add("anand"));
lhs.add("karthick");
System.out.println(lhs.add("anand"));
lhs.add("swathi");
lhs.add("banana");
lhs.add("swathi");
System.out.println(lhs);//[anand, banana, karthick, swathi]
}
private void linked_hash_set() {
// TODO Auto-generated method stub
LinkedHashSet lhs = new LinkedHashSet();
lhs.add("anand");
lhs.add("askin");
lhs.add("karthick");
lhs.add("anand");
lhs.add(true);
lhs.add(5.5f);
lhs.add(100);
System.out.println(lhs);//[askin, karthick, anand, true, 5.5, 100]
}
private void hash_set() {
// TODO Auto-generated method stub
HashSet al2 = new HashSet();
//Dynamic Array
al2.add("Raj");
al2.add("Raj");
al2.add("Raja");
al2.add(456);
System.out.println(al2);//[Raja, 456, Raj]
HashSet al = new HashSet();
al.add("world");//all are saved by objects
al.add(true);//Boolean wraper class
al.add(5.5f);//Float
al.add(88);//Integer
System.out.println(al);
al2.addAll(al);
System.out.println(al2);//[Raja, 5.5, world, 456, 88, Raj, true]
System.out.println(al2.contains("van"));//false
/*al2.add(3, "ramya");//No Indexing
System.out.println(al2);*/
al2.removeAll(al);
System.out.println(al2);//[Raja, 456, Raj]
al2.addAll(al);
System.out.println(al2);//[Raja, 5.5, world, 456, 88, Raj, true]
al2.retainAll(al);
System.out.println(al2);//[5.5, world, 88, true]
System.out.println(al.add("duplicate1"));//true
System.out.println(al.add("duplicate1"));//false
//System.out.println(al.get(3));
// Iterator:
}
private void ArrayList() {
// TODO Auto-generated method stub
ArrayList al= new ArrayList();
al.add("world");//all are saved by objects
al.add(true);//Boolean wraper class
al.add(5.5f);//Float
al.add(88);//Integer
System.out.println(al);//[world, true, 5.5, 88]
ArrayList al2= new ArrayList();
al2.add("hi");
al2.add("hello");
al2.add(345);
System.out.println(al2);//[hi, hello, 345]
al2.addAll(al);//it included al into al2
System.out.println(al2);//[hi, hello, 345, world, true, 5.5, 88]
System.out.println(al2.contains(348));//false-it checks given contains are not
al2.add(2,"san");
System.out.println(al2);// it join using index-before al2[hi, hello, 345, world, true, 5.5, 88]
//after index 2 place-[hi, hello, san, 345, world, true, 5.5, 88]
al2.addAll(1,al);
System.out.println(al2);//before-[hi, hello, san, 345, world, true, 5.5, 88]
//after-[hi, world, true, 5.5, 88, hello, san, 345, world, true, 5.5, 88]
al2.removeAll(al);
System.out.println(al2);//before remove-[hi, world, true, 5.5, 88, hello, san, 345, world, true, 5.5, 88]
//after remove-[hi, hello, san, 345]
al2.addAll(al);//it included al into al2
System.out.println(al2);//[hi, hello, san, 345, world, true, 5.5, 88]
al2.retainAll(al);//it takes al from al2-[world, true, 5.5, 88]
System.out.println(al2);
System.out.println(al2.get(2));//it gives given index value-5.5
}
private void learn_list() {
// TODO Auto-generated method stub
/*Scanner sc = new Scanner(System.in);
System.out.println("Enter Name");
String name = sc.next();
boolean b = sc.nextBoolean();
LinkedList al = new LinkedList();
al.add(name); //Object
al.add(b); // Boolean b
al.add(5.5f); // Float f
al.add(567); // Integer i
System.out.println(al);*/
LinkedList al = new LinkedList();
al.add("san"); //Object
al.add(true); // Boolean b
al.add(5.5f); // Float f
al.add(567); // Integer i
al.add(3,"you");//[san, true, 5.5, you, 567] -->place by index 3
System.out.println(al);//[san, true, 5.5, 567]
LinkedList al2 = new LinkedList();
//Dynamic Array
al2.add("Raj");
al2.add("Raja");
al2.add(456);
System.out.println(al2);//[Raj, Raja, 456]
// al.addAll(al2);
//System.out.println(al);//[san, true, 5.5, 567, Raj, Raja, 456]
al2.addAll(al);
System.out.println(al2);//[Raj, Raja, 456, san, true, 5.5, 567],[Raj, Raja, 456, san, true, 5.5, 567, Raj, Raja, 456]
System.out.println(al2.contains("Edwin"));//false
al2.retainAll(al);//[san, true, 5.5, you, 567]
System.out.println(al2);
al2.removeAll(al); //[Raj, Raja, 456]
System.out.println(al2);
System.out.println(al.get(3));//you
}}
Leave a comment