Map Interface
Map Interface
get(Object key) This method returns the object that contains the
value associated with the key.
Set keySet() It returns the Set view containing all the keys.
class MapExample{
public static void main(String args[]){
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(100,"Amit");
map.put(101,"Vijay");
map.put(102,"Rahul");
//Elements can traverse in any order
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output:
• 102 Rahul
• 100 Amit
• 101 Vijay
exampl2
• import java.util.*;
• class HashMap1{
• public static void main(String args[]){
• HashMap<Integer,String> hm=new HashMap<Integer,String>();
• System.out.println("Initial list of elements: "+hm);
• hm.put(100,"Amit");
• hm.put(101,"Vijay");
• hm.put(102,"Rahul");
•
• System.out.println("After invoking put() method ");
• for(Map.Entry m:hm.entrySet()){
• System.out.println(m.getKey()+" "+m.getValue());
• }
•
• hm.putIfAbsent(103, "Gaurav");
• System.out.println("After invoking putIfAbsent() method ");
• for(Map.Entry m:hm.entrySet()){
• System.out.println(m.getKey()+" "+m.getValue());
• }
output
• Initial list of elements: {}
• After invoking put() method
• 100 Amit
• 101 Vijay
• 102 Rahul
• After invoking putIfAbsent() method
• 100 Amit
• 101 Vijay
• 102 Rahul
• 103 Gaurav
Java LinkedHashSet class
Constructor Description
Output:
Ravi
Vijay
Ajay
java TreeMap class
onstructor Description
Method Description
HashMap TreeMap
1) HashMap can contain one null TreeMap cannot contain any null
key. key.
2) HashMap maintains no order. TreeMap maintains ascending
order.
Java Comparator interface
Method Description
public int compare(Object obj1, Object obj2) It compares the first object with the second
object.
public boolean equals(Object obj) It is used to compare the current object with
the specified object.
public boolean equals(Object obj) It is used to compare the current object with
the specified object.
• Student.java
• class Student{
• int rollno;
• String name;
• int age;
• Student(int rollno,String name,int age){
• this.rollno=rollno;
• this.name=name;
• this.age=age;
• }
• }
• Simple.javaIn this class, we are printing the values of the object by sorting on the basis of name and age.
• import java.util.*;
• import java.io.*;
• class Simple{
• public static void main(String args[]){
•
• ArrayList<Student> al=new ArrayList<Student>();
• al.add(new Student(101,"Vijay",23));
• al.add(new Student(106,"Ajay",27));
• al.add(new Student(105,"Jai",21));
•
• System.out.println("Sorting by Name");
•
• Collections.sort(al,new NameComparator());
• for(Student st: al){
• System.out.println(st.rollno+" "+st.name+" "+st.age);
• }
•
• System.out.println("Sorting by age");
•
• Collections.sort(al,new AgeComparator());
• for(Student st: al){
• System.out.println(st.rollno+" "+st.name+" "+st.age);
• }
• }
• }
• Sorting by Name 106 Ajay 27 105 Jai 21 101 Vijay 23 Sorting by age 105 Jai 21 101 Vijay 23 106 Ajay 27
• import java.util.*;
• class AgeComparator implements Comparator<Student>{
• public int compare(Student s1,Student s2){
• if(s1.age==s2.age)
• return 0;
• else if(s1.age>s2.age)
• return 1;
• else
• return -1;
• }
• }
• Simple.javaIn this class, we are printing the values of the object by sorting on the basis of name and age.
• import java.util.*;
• import java.io.*;
• class Simple{
• public static void main(String args[]){
•
• ArrayList<Student> al=new ArrayList<Student>();
• al.add(new Student(101,"Vijay",23));
• al.add(new Student(106,"Ajay",27));
• al.add(new Student(105,"Jai",21));
•
• System.out.println("Sorting by Name");
•
• Collections.sort(al,new NameComparator());
• for(Student st: al){
• System.out.println(st.rollno+" "+st.name+" "+st.age);
• }
•
• System.out.println("Sorting by age");
•
• Collections.sort(al,new AgeComparator());
• for(Student st: al){
• System.out.println(st.rollno+" "+st.name+" "+st.age);
• }
• }
• }
• Sorting by Name 106 Ajay 27 105 Jai 21 101
Vijay 23 Sorting by age 105 Jai 21 101 Vijay 23
106 Ajay 27