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