当前位置:   article > 正文

把map中的值全部打印出来

map打印出来的值i
  1. /**
  2. * @param h
  3. * @return 实现对map按照value升序排序
  4. */
  5. @SuppressWarnings("unchecked")
  6. public static Map.Entry[] getSortedHashtableByValue(Map h) {
  7. Set set = h.entrySet();
  8. Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set
  9. .size()]);
  10. Arrays.sort(entries, new Comparator() {
  11. public int compare(Object arg0, Object arg1) {
  12. Long key1 = Long.valueOf(((Map.Entry) arg0).getValue()
  13. .toString());
  14. Long key2 = Long.valueOf(((Map.Entry) arg1).getValue()
  15. .toString());
  16. return key1.compareTo(key2);
  17. }
  18. });
  19. return entries;
  20. }
  21. /**
  22. * @param h
  23. * @return 实现对map按照key排序
  24. */
  25. @SuppressWarnings("unchecked")
  26. public static Map.Entry[] getSortedHashtableByKey(Map h) {
  27. Set set = h.entrySet();
  28. Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set
  29. .size()]);
  30. Arrays.sort(entries, new Comparator() {
  31. public int compare(Object arg0, Object arg1) {
  32. Object key1 = ((Map.Entry) arg0).getKey();
  33. Object key2 = ((Map.Entry) arg1).getKey();
  34. return ((Comparable) key1).compareTo(key2);
  35. }
  36. });
  37. return entries;
  38. }

 Map map = new HashMap();
map.set( "a ", "b ");
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
Object key =it.next();
System.out.println( "key is "+key);
System.out.println( "value is "+map.get(key));

}

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/953894
推荐阅读
相关标签
  

闽ICP备14008679号