|
|
Program: How to get first key element from TreeMap (Sorted Map)?
Description: |
Below example shows how to get first key from TreeMap based on sorting. To get first element, you have to sort the
TreeMap based on user defined objects by using comparator object. You can include you own
custom sorting logic with compare method. By passing comparator object to the TreeMap, you can sort the keys based on the logic
provided inside the compare method. Once the TreeMap keys are in sorting order, you can call firstKey() method
to get the first key, then you can get the corresponding value object.
|
Code: |
package com.java2novice.treemap;
import java.util.TreeMap;
import java.util.Map.Entry;
public class MyTreeMapFirstElement {
public static void main(String a[]){
//By using salary comparator (int comparison)
TreeMap<Emp,String> trmap = new TreeMap<Emp, String>(new MySalaryCompr());
trmap.put(new Emp("Ram",3000), "RAM");
trmap.put(new Emp("John",6000), "JOHN");
trmap.put(new Emp("Crish",2000), "CRISH");
trmap.put(new Emp("Tom",2400), "TOM");
Emp em = trmap.firstKey();
System.out.println("Highest salary emp: "+em);
Entry<Emp,String> ent = trmap.firstEntry();
System.out.println("Entry set values: ");
System.out.println(ent.getKey()+" ==> "+ent.getValue());
}
}
class MySalaryCompr implements Comparator<Emp>{
@Override
public int compare(Emp e1, Emp e2) {
if(e1.getSalary() < e2.getSalary()){
return 1;
} else {
return -1;
}
}
}
class Emp{
private String name;
private int salary;
public Emp(String n, int s){
this.name = n;
this.salary = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString(){
return "Name: "+this.name+"-- Salary: "+this.salary;
}
}
|
|
Output: |
Highest salary emp: Name: John-- Salary: 6000
Entry set values:
Name: John-- Salary: 6000 ==> JOHN
|
|
|
|
|
List Of All TreeMap Sample Programs:- Basic TreeMap Operations.
- How to iterate through TreeMap?
- How to copy Map content to another TreeMap?
- How to search a key in TreeMap?
- How to search a value in TreeMap?
- How to get all keys from TreeMap?
- How to get entry set from TreeMap?
- How to delete all elements from TreeMap?
- How to sort keys in TreeMap by using Comparator?
- How to sort keys in TreeMap by using Comparator with user define objects?
- How to get sorted sub-map from TreeMap?
- How to get first key element from TreeMap (Sorted Map)?
- How to get last key element from TreeMap (Sorted Map)?
- How to reverse sorted keys in a TreeMap?
|
|
|
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based.
But accessing is difficult with LinkedList. It is slow access. This is
to access any element, you need to navigate through the elements one by
one. But insertion and deletion is much faster with LinkedList, because
if you know the node, just change the pointers before or after nodes.
Insertion and deletion is slow with ArrayList, this is because, during
these operations ArrayList need to adjust the indexes according to
deletion or insetion if you are performing on middle indexes. Means,
an ArrayList having 10 elements, if you are inserting at index 5, then
you need to shift the indexes above 5 to one more.
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln
|