TreeMap get获取数据为null
2016-07-16 10:55
267 查看
之前用treeMap进行排序,然后for循环keySet,get(Key)获取到的值是null,后来找到一下方法测了一下,只有没有注释的那个方法是可以获取到值得
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
Map<Special, List<Integer>> items = new TreeMap<Special, List<Integer>>((Comparator<? super Special>) new Comparator<Special>()
{
public int compare(Special special1, Special special2)
{
return special2.getIndexNo() < special1.getIndexNo() ? 1 : -1;
}
});
Special a = new SpecialImpl();
a.setId(1);
a.setIndexNo(0);
List<Integer> b = new ArrayList<Integer>();
b.add(1);
b.add(2);
b.add(3);
items.put(a, b);
a = new SpecialImpl();
a.setId(2);
a.setIndexNo(1);
b = new ArrayList<Integer>();
b.add(4);
b.add(5);
b.add(6);
items.put(a, b);
// Set<Special> keySet = items.keySet(); // keySet获取key
// for (Iterator<Special> it = keySet.iterator(); it.hasNext();)
// {
// Special special = it.next();
// System.out.println(special.getId() + "--" + special.getIndexNo() + ":");
// System.out.println(items.get(special));
// }
// 第二种
Set<Map.Entry<Special, List<Integer>>> entrySet = items.entrySet(); // entrySet获取映射关系
Iterator<Map.Entry<Special, List<Integer>>> iter = entrySet.iterator();
while (iter.hasNext())
{
Map.Entry<Special, List<Integer>> me = iter.next();// Map.Entry是entrySet集合的元素类型.
Special special = me.getKey();
List<Integer> c = me.getValue();
System.out.println(special.getId() + "--" + special.getIndexNo() + ":");
System.out.println(c);
}
}
}
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
Map<Special, List<Integer>> items = new TreeMap<Special, List<Integer>>((Comparator<? super Special>) new Comparator<Special>()
{
public int compare(Special special1, Special special2)
{
return special2.getIndexNo() < special1.getIndexNo() ? 1 : -1;
}
});
Special a = new SpecialImpl();
a.setId(1);
a.setIndexNo(0);
List<Integer> b = new ArrayList<Integer>();
b.add(1);
b.add(2);
b.add(3);
items.put(a, b);
a = new SpecialImpl();
a.setId(2);
a.setIndexNo(1);
b = new ArrayList<Integer>();
b.add(4);
b.add(5);
b.add(6);
items.put(a, b);
// Set<Special> keySet = items.keySet(); // keySet获取key
// for (Iterator<Special> it = keySet.iterator(); it.hasNext();)
// {
// Special special = it.next();
// System.out.println(special.getId() + "--" + special.getIndexNo() + ":");
// System.out.println(items.get(special));
// }
// 第二种
Set<Map.Entry<Special, List<Integer>>> entrySet = items.entrySet(); // entrySet获取映射关系
Iterator<Map.Entry<Special, List<Integer>>> iter = entrySet.iterator();
while (iter.hasNext())
{
Map.Entry<Special, List<Integer>> me = iter.next();// Map.Entry是entrySet集合的元素类型.
Special special = me.getKey();
List<Integer> c = me.getValue();
System.out.println(special.getId() + "--" + special.getIndexNo() + ":");
System.out.println(c);
}
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树