Join Two Lists in Java

I need to combine two lists in Java. I have a list that has a VO that has a name and description. I have another list that has the same VO type that has a name and address. The "name" is the same. I need to create a List with this VO that has a name, address and description. VO structure

public class PersonDetails{
    private String name;
    private String description;
    private String address;
//getters and setters
}

Can someone please suggest me a better way to implement it?

+3
source share
5 answers

Put all the elements of the first list on the map, then merge the contents of the second list into it:

final List<PersonDetails> listWithAddress =
    new ArrayList<PersonDetails>();
final List<PersonDetails> listWithDescription =
    new ArrayList<PersonDetails>();
// fill both lists with data

final Map<String, PersonDetails> map =
    // map sorted by name, change to HashMap otherwise
    // (or to LinkHashMap if you need to preserve the order)
    new TreeMap<String, PersonDetails>();

for(final PersonDetails detailsWithAddress : listWithAddress){
    map.put(detailsWithAddress.getName(), detailsWithAddress);
}
for(final PersonDetails detailsWithDescription : listWithDescription){
    final PersonDetails retrieved =
        map.get(detailsWithDescription.getName());
    if(retrieved == null){
        map.put(detailsWithDescription.getName(),
            detailsWithDescription);
    } else{
        retrieved.setDescription(detailsWithDescription.getDescription());
    }
}
+2
source

It depends:

, , .

, . , VO . .

public List<Vo> merge(List<Vo> list1, List<Vo> list2) {

    Map<String, Vo> tempMap = new HashMap<String, Vo>();

    for (Vo v : list1) {
        tempMap.put(v.name, v);
    }

    for (Vo vv : list2) {

        //The if is in case the 2 lists aren't filled with the same objects            
        if (tempMap.containsKey(vv.name)) {
            tempMap.get(vv.name).description = vv.description;
        } else {
            tempMap.put(vv.name, vv);
        }
    }

    return new ArrayList<Vo>(tempMap.values());

}

EXACT VO ( ), .

public List<Vo> merge(List<Vo> list1, List<Vo> list2) {

    Collections.sort(list1, new Comparator<Vo>() {

        public int compare(Vo o1, Vo o2) {
            return o1.name.compareTo(o2.name);
        }
    });

    Collections.sort(list2, new Comparator<Vo>() {

        public int compare(Vo o1, Vo o2) {
            return o1.name.compareTo(o2.name);
        }
    });

    for(int i = 0; i < list1.size(); i++){
        list1.get(i).description = list2.get(i).description;
    }

    return list1;

}
+5

VO , , VO .

VO VO, VOD VO , VOA VO :

List<VOD> descriptions = ...;
List<VOA> addresses = ...;
Map<String,String> description ByName = new HashMap<String,String>();
for (VOD description : descriptions) {
  descriptionByName.put(description.name, description.description);
}
Map<String,String> addressByName = new HashMap<String,String>();
for (VOA address: addresses ) {
  addressByName.put(address.name, address.address);
}

Set<String> allNames = new HashSet<String>();
allNames.addAll(descriptionByName.keySet());
allNames.addAll(addressByName.keySet());

List<VO> result = new ArrayList<VO>();
for (String name : allNames) {
  VO one = new VO();
  one.name = name;
  one.address = addressByName.get(name)
  one.description = descriptionByName.get(name)
  result.add(one);
}
+2

HashMap, . , .

+1
List<VO> list = new ArrayList<VO>();
for (VO vo1 : vo1List) {
   for (VO vo2 : vo2List) {
    if (vo1.getName().equals(vo2.getName())) {
        VO newVo = new VO();
        newVO.setName(vo1.getName());
        newVO.setDescription(vo1.getDescription());
        newVO.setAddress(vo2.getAddress);
        list.add(newVO);
        break;
    }
}

}

It is best that you sort both lists by name in advance, it speeds up the double iteration.

+1
source

Source: https://habr.com/ru/post/1763024/


All Articles