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>([b]1[/b]);


package test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class Testor {
static Testor t = new Testor();
class T1{
String s;
T1(String pS){
s = pS;
}
@Override
public int hashCode() {
return (int)(Math.random()*1000);
}
@Override
public String toString() {
return s;
}
}
class T2{
String s;
T2(String pS){
s = pS;
}
@Override
public int hashCode() {
return s.hashCode();
}
@Override
public String toString() {
return s;
}
}
static HashMap<String, String> h1 = new HashMap<String, String>(50);
static {
for (int i = 0; i < 20; i++) {
h1.put(String.valueOf(i), String.valueOf(i));
}
}
static HashMap<T1, String> h2 = new HashMap<T1, String>();
static {
Testor t = new Testor();
for (int i = 0; i < 20; i++) {
h2.put(t.new T1(String.valueOf(i)), String.valueOf(i));
}
}
static HashMap<T2, String> h3 = new HashMap<T2, String>();
static {
for (int i = 0; i < 20; i++) {
h3.put(t.new T2(String.valueOf(i)), String.valueOf(i));
}
}

static HashMap<String, String> h4 = new HashMap<String, String>(1);
static {
for (int i = 0; i < 20; i++) {
h4.put(String.valueOf(i), String.valueOf(i));
}
}

public static void main(String[] args) {
Iterator<Entry<String,String>> it = h1.entrySet().iterator();
Iterator<Entry<T2,String>> it3 = h3.entrySet().iterator();
Iterator<Entry<T1,String>> it2 = h2.entrySet().iterator();
Iterator<Entry<String,String>> it4 = h4.entrySet().iterator();
while (it.hasNext()) {
String a1 = it.next().getKey();
T1 a2 = it2.next().getKey();
T2 a3 = it3.next().getKey();
String a4 = it4.next().getKey();
System.out.print(a1 + "|" + a3 + "|" + a2 + "|" + a4 + " ");
}
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值