lambda表达式—foreach的使用

使用lambada对list和map的遍历

lambada表达式遍历简洁方便,在此记录。

list的示例

1
2
3
4
5
6
7
8
9
10
11
public void testUpdateUser(){

int id = 2;
AyUser user = new AyUser();
user.setName("IornMan");
user.setPassword("886688668866");
int a = ayUserDao.updateUser(user, id);

List<AyUser> userList = ayUserDao.findById(2);
userList.forEach(x -> System.out.println(x.getName() + x.getPassword()));
}

map的示例

1
2
3
4
5
6
7
8
9
public void testUpdateUser(){
Map<String, String> map = new HashMap<String, String>() {{
put("key1", "value1");
put("key2", "value2");
put("key3", "value3");
}};
map.forEach((k,v)->System.out.println("Key:" + k + "-----Value:" + v));

}