Java中的TreeMap

时间:2025-04-04 09:33:39

import ;

import ;

 

public class LinkedHashMapExample

{

    public static void main(String[] args)

    {

        //Natual ordering by deafult

        TreeMap<Integer, String> pairs = new TreeMap<>();

         

        (2,  "B");

        (1,  "A");

        (3,  "C");

         

        String value = (3);    //get method

         

        (value);

         

        value = (5"oops");  //getOrDefault method

         

        (value);

         

        //Iteration example

        Iterator<Integer> iterator =  ().iterator();

         

        while(()) {

            Integer key = ();

            ("Key: " + key + ", Value: " + (key));

        }

         

        //Remove example

        (3);

        (pairs);

         

        ((1));    //containsKey method  

         

        (("B"));    //containsValue method  

 

        ((2));

    }

}