Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.w3c.dom.Element; public class Main { /** */ public static ArrayList<Element> mergeByAttrib(ArrayList<Element> master, ArrayList<Element> slave, String attrib) throws Exception { Map<String, Element> masterMap = mapFromArrayListByAttribute(master, attrib); Map<String, Element> slaveMap = mapFromArrayListByAttribute(slave, attrib); slaveMap.putAll(masterMap); ArrayList<Element> res = new ArrayList<Element>(); Collection<Element> collection = slaveMap.values(); for (Element e : collection) { res.add(e); } return res; } public static Map<String, Element> mapFromArrayListByAttribute(ArrayList<Element> list, String attribName) { Map<String, Element> map = new HashMap<String, Element>(); for (Element e : list) { map.put(e.getAttribute(attribName), e); } return map; } }