1. 程式人生 > >Java幾種集合的遍歷方法

Java幾種集合的遍歷方法

(一):Map

long num1 = 100,num2 = 200;
String str1="hello",str2="world";
Map<Long, String> map = new HashMap<>();
map.put(num1,str1);
map.put(num2,str2);
for(Map.Entry<Long, String> entry:map.entrySet()){
    System.out.println("key= "+entry.getKey()+" value= "+entry.getValue());
}

(二):List

List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
1):
for(Iterator it = list.iterator();it.hasNext();){
    System.out.println(it.next());
}

2):
for(String str:list){
    System.out.println(str);
}

3):
for(int i = 0;i<list.size();i++){
    System.out.println(list
.get(i)); }

…更新中