TreeMap get获取数据为null

之前用treeMap进行排序,然后for循环keySet,get(Key)获取到的值是null,后来找到一下方法测了一下,只有没有注释的那个方法是可以获取到值得

public class Test
{

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    Map> items = new TreeMap>((Comparator) new Comparator()
    {
      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 b = new ArrayList();
    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();
    b.add(4);
    b.add(5);
    b.add(6);
    items.put(a, b);

    // Set keySet = items.keySet(); // keySet获取key
    // for (Iterator it = keySet.iterator(); it.hasNext();)
    // {
    // Special special = it.next();
    // System.out.println(special.getId() + "--" + special.getIndexNo() + ":");
    // System.out.println(items.get(special));
    // }
    // 第二种
    Set>> entrySet = items.entrySet(); // entrySet获取映射关系
    Iterator>> iter = entrySet.iterator();
    while (iter.hasNext())
    {
      Map.Entry> me = iter.next();// Map.Entry是entrySet集合的元素类型.
      Special special = me.getKey();
      List c = me.getValue();
      System.out.println(special.getId() + "--" + special.getIndexNo() + ":");
      System.out.println(c);
    }
  }

}


你可能感兴趣的:(java)