TreeMap get获取数据为null

之前用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);
    }
  }

}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值