list 將map裏面某個字段重複的,從list裏面去掉

對於list<map>須要去掉 map裏面有一個字段重複的map在此寫了一個demo用於測試java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Map> shareList = new ArrayList<Map>();
		for (int a = 0; a < 5; a++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("age", 11 + a);
			shareList.add(map);
		}
		System.out.println("shareList1=" + shareList.toString());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("age", 12);
		shareList.add(map);
		System.out.println("shareList2=" + shareList.toString());
		for (int i = 0; i < shareList.size() - 1; i++) {
			for (int j = shareList.size() - 1; j > i; j--) {
				if (shareList.get(j).get("age").equals(shareList.get(i).get("age"))) {
					shareList.remove(j);
				}
			}
		}
		System.out.println("shareList3=" + shareList.toString());

	}

}
相關文章
相關標籤/搜索