兩個ListMap中同下標的map去重合並

兩個LIst根據某個字段進行取並集

public static void main(String[] args) {
		Map<String, Object> A1 = new HashMap<>();
		A1.put("name", "張三");
		A1.put("age", "25");
		A1.put("sex", "男");
		Map<String, Object> A2 = new HashMap<>();
		A2.put("name", "張四");
		A2.put("age", "23");  
		A2.put("sex", "女");  

		Map<String, Object> B1 = new HashMap<>();
		B1.put("name", "張三");
		B1.put("age", "25");
		B1.put("School", "清華");
		Map<String, Object> B2 = new HashMap<>();
		B2.put("name", "張四");
		B2.put("age", "23");
		B2.put("School", "北大");

		List<Map<String, Object>> listA = new ArrayList<>();
		listA.add(A1);
		listA.add(A2);

		List<Map<String, Object>> listB = new ArrayList<>();
		listB.add(B1);
		listB.add(B2);

		listA.forEach(d1 -> {
			listB.forEach(d2 ->{
				if (d1.get("name").toString().equals(d2.get("name").toString())) {
					d2.putAll(d1);
				}
			});
		});
	}
發佈了35 篇原創文章 · 獲贊 14 · 訪問量 7008
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章