简体   繁体   中英

TreeMap keys does not display in ascending order

I am using Treemap to get key value pair in ascending order.

I have key value pair as

key  |  Value

 2      Rooney 
 7      Ronaldo
 20     Persie
 1      Pele
 10     Ramires

I have used Map.Entry and also advanced for loop to iterate key value.

Problem is that the output is not in proper ascending order as i want.

Output 

key  |  Value

 1      Pele
 10     Ramires     
 2      Rooney
 20     Persie 
 7      Ronaldo

Actual output as I want to display is shown below.

Output 

    key  |  Value

     1      Pele          
     2      Rooney
     7      Ronaldo
     10     Ramires
     20     Persie

Thanks in advance for your answers.

Change key to int. If you use string, the map will be sorted in the lexicographical order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM