gpt4 book ai didi

java - 当我们有 itreator 时为什么要引入 for 循环 With Using Same Map Entry Function

转载 作者:行者123 更新时间:2023-11-29 05:09:08 24 4
gpt4 key购买 nike

<分区>

当我们有 for lop 那么为什么我们使用迭代器下面的代码是一些作为迭代器给出的结果工作

public class HashMapExample {
public static void main(String[] args) {
HashMap<String, String> hasmap = new HashMap<String, String>();
hasmap.put("1", "Java");
hasmap.put("2", ".Net");
hasmap.put("3", "C++");
hasmap.put("4", "Pearl");
hasmap.put("5", "PHP");

for (Entry<String, String> entry : hasmap.entrySet()) {

System.out.println("For Loop Get Key"+entry.getKey());
System.out.println("For Loop Get Value"+entry.getValue());

}
Set set = hasmap.entrySet();
Iterator itr = set.iterator();
while(itr.hasNext()){
Map.Entry map = (Entry) itr.next();
System.out.println(map.getKey());
System.out.println(map.getValue());
}


}

}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com