How to get the first 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.get(treemap.firstKey()));
        }        
    }
}
   
   
   
/*
run:
   
c++
  
*/

 



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.firstEntry().getValue());
        }        
    }
}
   
   
   
/*
run:
   
c++
  
*/

 



answered Mar 14, 2021 by avibootz

Related questions

1 answer 180 views
1 answer 169 views
2 answers 192 views
1 answer 162 views
1 answer 179 views
1 answer 147 views
1 answer 178 views
...