package sxt.gaoqi.container;
import java.util.Map;
import java.util.TreeMap;
/*
* 测试TreeMap的使用
*/
public class TestTreeMap
{
public static void main(String[] args)
{
Map<Integer, String> treeMap = new TreeMap<>();
treeMap.put(3, "森");
treeMap.put(2, "莹");
treeMap.put(1, "宝");
for(Integer key : treeMap.keySet())
{
System.out.println(key + "----" + treeMap.get(key));
}
Map<Emp, String> treeMap2 = new TreeMap<>();
treeMap2.put(new Emp(2, 9000.0), "小明");
treeMap2.put(new Emp(6, 6000.0), "小明");
treeMap2.put(new Emp(4, 7000.0), "小明");
treeMap2.put(new Emp(5, 6000.0), "小明");
for(Emp key : treeMap2.keySet())
{
System.out.println(key);
}
}
}
class Emp implements Comparable<Emp>
{
int id;
double salary;
public Emp(int id, double salary)
{
this.id = id;
this.salary = salary;
}
@Override
public int compareTo(Emp o)
{
if(this.salary < o.salary)
{
return -1;
}
else if(this.salary > o.salary)
{
return 1;
}
else
{
if(this.id < o.id)
{
return 1;
}
else if(this.id > o.id)
{
return -1;
}
else
{
return 0;
}
}
}
@Override
public String toString()
{
return "id: " + id + "salary: " + salary;
}
}
TreeMap use
Guess you like
Origin blog.csdn.net/gjs935219/article/details/105498153
Recommended
Ranking