map通过Key获取Value值

List arrayList = new ArrayList();
		for (int i = 1; i < 5; i++) {
			Student student = new Student();
			student.setId(i);
			arrayList.add(student);
		}
		System.err.println(arrayList);
		Map map = new HashMap();
		map.put(3, "小王");
		map.put(4, "小宋");
		map.put(1, "小白");
		map.put(2, "小陈");
		for (Student student : arrayList) {
			student.setName(map.get(student.getId()));
		}
		System.out.println(arrayList);

打印结果:
[Student [id=1, name=], Student [id=2, name=], Student [id=3, name=], Student [id=4, name=]]
[Student [id=1, name=小白], Student [id=2, name=小陈], Student [id=3, name=小王], Student [id=4, name=小宋]]

你可能感兴趣的:(java)