免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 976 | 回复: 1
打印 上一主题 下一主题

About next() of HashMap keySet [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-28 21:04 |只看该作者 |倒序浏览

About next() of HashMap keySet








Note:
If you found all of the a4 values are the same as a1, please change the init value of h4 such as:
   static HashMap<String, String> h4 = new HashMap<String, String>(1);




Java代码
  1. 1.package test;  
  2. 2.  
  3. 3.import java.util.HashMap;  
  4. 4.import java.util.Iterator;  
  5. 5.import java.util.Map.Entry;  
  6. 6.  
  7. 7.public class Testor {  
  8. 8.  static Testor t = new Testor();  
  9. 9.  class T1{  
  10. 10.    String s;  
  11. 11.    T1(String pS){  
  12. 12.      s = pS;  
  13. 13.    }  
  14. 14.    @Override  
  15. 15.    public int hashCode() {  
  16. 16.      return (int)(Math.random()*1000);  
  17. 17.    }  
  18. 18.    @Override  
  19. 19.    public String toString() {  
  20. 20.      return s;  
  21. 21.    }  
  22. 22.  }  
  23. 23.  class T2{  
  24. 24.    String s;  
  25. 25.    T2(String pS){  
  26. 26.      s = pS;  
  27. 27.    }  
  28. 28.    @Override  
  29. 29.    public int hashCode() {  
  30. 30.      return s.hashCode();  
  31. 31.    }  
  32. 32.    @Override  
  33. 33.    public String toString() {  
  34. 34.      return s;  
  35. 35.    }  
  36. 36.  }  
  37. 37.  static HashMap<String, String> h1 = new HashMap<String, String>(50);  
  38. 38.  static {  
  39. 39.    for (int i = 0; i < 20; i++) {  
  40. 40.      h1.put(String.valueOf(i), String.valueOf(i));  
  41. 41.    }  
  42. 42.  }  
  43. 43.  static HashMap<T1, String> h2 = new HashMap<T1, String>();  
  44. 44.  static {  
  45. 45.    Testor t = new Testor();  
  46. 46.    for (int i = 0; i < 20; i++) {  
  47. 47.      h2.put(t.new T1(String.valueOf(i)), String.valueOf(i));  
  48. 48.    }  
  49. 49.  }  
  50. 50.  static HashMap<T2, String> h3 = new HashMap<T2, String>();  
  51. 51.  static {  
  52. 52.    for (int i = 0; i < 20; i++) {  
  53. 53.      h3.put(t.new T2(String.valueOf(i)), String.valueOf(i));  
  54. 54.    }  
  55. 55.  }  
  56. 56.  
  57. 57.  static HashMap<String, String> h4 = new HashMap<String, String>(1);  
  58. 58.  static {  
  59. 59.    for (int i = 0; i < 20; i++) {  
  60. 60.      h4.put(String.valueOf(i), String.valueOf(i));  
  61. 61.    }  
  62. 62.  }  
  63. 63.   
  64. 64.  public static void main(String[] args) {  
  65. 65.    Iterator<Entry<String,String>> it = h1.entrySet().iterator();  
  66. 66.    Iterator<Entry<T2,String>> it3 = h3.entrySet().iterator();  
  67. 67.    Iterator<Entry<T1,String>> it2 = h2.entrySet().iterator();  
  68. 68.    Iterator<Entry<String,String>> it4 = h4.entrySet().iterator();  
  69. 69.    while (it.hasNext()) {  
  70. 70.      String a1 = it.next().getKey();  
  71. 71.      T1 a2 = it2.next().getKey();  
  72. 72.      T2 a3 = it3.next().getKey();  
  73. 73.      String a4 = it4.next().getKey();  
  74. 74.      System.out.print(a1 + "|" + a3 + "|" + a2 + "|" + a4 + " ");  
  75. 75.    }  
  76. 76.  }  
  77. 77.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-28 21:04 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP