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使用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.