How to get the last value from TreeMap in Java

2 Answers

0 votes
import java.util.TreeMap;
  
public class MyClass {
    public static void main(String args[]) {
        TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
         
        treemap.put(4, "java");
        treemap.put(2, "c++");
        treemap.put(5, "python");
        treemap.put(3, "c#");
         
        if (!treemap.isEmpty()){
            System.out.println(treemap.lastEntry().getValue());
        }        
    }
}
   
   
   
   
/*
run:
   
python
  
*/

 



answered Mar 14, 2021 by avibootz
0 votes
import java.util.TreeMap;
  
public class MyClass {
    public static void main(String args[]) {
        TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
         
        treemap.put(4, "java");
        treemap.put(2, "c++");
        treemap.put(5, "python");
        treemap.put(3, "c#");
         
        if (!treemap.isEmpty()){
            System.out.println(treemap.get(treemap.lastKey()));
        }        
    }
}
   
   
   
   
/*
run:
   
python
  
*/

 



answered Mar 14, 2021 by avibootz

Related questions

1 answer 159 views
1 answer 172 views
2 answers 215 views
1 answer 176 views
1 answer 175 views
1 answer 144 views
1 answer 164 views
...