java listmap遍历_Java遍历List、Map的集合方法

使用JDK8新特性,for循环增强,Iterator,while做遍历List集合

public static void main(String[] args) {

List list = new ArrayList<>();

list.add("test1x");

list.add("test2x");

list.add("test3x");

System.out.println(list);

System.out.println("===========");

list.forEach((v)->{

System.out.println(v);

});

System.out.println("===========");

for(String str : list) {

System.out.println(str);

}

System.out.println("===========");

for(int i = 0; i < list.size();i++) {

System.out.println(list.get(i));

}

System.out.println("===========");

Iterator it = list.iterator();

while(it.hasNext()) {

System.out.println(it.next());

}

System.out.println("=========");

int i = 0;

while(i < list.size()){

System.out.println(list.get(i));

i++;

}

}

使用JDK8新特性,for循环增强,Iterator,Map.Entry做遍历Map集合

public static void main(String[] args) {

Map map = new HashMap<>();

map.put(1,"test1x");

map.put(2,"test2x");

map.put(3,"test3x");

System.out.println(map);

System.out.println("==========");

map.forEach((k ,v) -> {

System.out.println(k + " , " + v);

});

System.out.println("========================");

for(Map.Entry entry : map.entrySet()) {

System.out.println(entry.getKey() + " , " + entry.getValue());

}

System.out.println("==========");

for(int key : map.keySet()) {

System.out.println(key + " , " + map.get(key));

}

System.out.println("==========");

Iterator> it = map.entrySet().iterator();

while(it.hasNext()){

Map.Entry entry = it.next();

System.out.println(entry.getKey() + " , " + entry.getValue());

}

System.out.println("==========");

for(String v : map.values()) {

System.out.println(v);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值