我有两个ArrayList列表,我要比较它们将它们合并到ArrayList的最终列表中。
示例我有数据库调用,我将这些项目作为
List<List<String>> data1 = dao1.getall();
List<List<String>> data2 = dao2.getall();
data1看起来像
的结果集"results": [
[
"India",
"Idea",
"30"
],
[
"USA",
"Idea",
"10"
],
[
"irland",
"Idea",
"10"
]
data2看起来像
的结果集"results": [
[
"India",
"Idea",
"50"
],
[
"usa",
"Idea",
"30"
],
[
"sweden",
"Idea",
"10"
]
我想通过比较Country和Operator字段来合并这两个列表列表,如下所示。
"results": [
[
"India",
"Idea",
"30",
"50",
"80" ====== sum of the above two values
],
[
"usa",
"Idea",
"10",
"30",
"40"
],
[
"irland",
"Idea",
"10"
"0"
"10"
]
[
"sweden",
"Idea",
"0"
"10" ==== order is very important here
"10"
]
有人可以帮我吗?提前谢谢。
我试过了,但根本没有为我工作。
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Public class Hello{
public static void main(String[] args) {
List<List<String>> list1 = new ArrayList<List<String>>();
List<List<String>> list2 = new ArrayList<List<String>>();
// add data
List<String> datalist1 = new ArrayList<String>();
datalist1.add("India");
datalist1.add("vodafone");
datalist1.add("23");
list1.add(datalist1);
System.out.println(list1);
List<String> datalist2 = new ArrayList<String>();
datalist2.add("India");
datalist2.add("vodafone");
datalist2.add("20");
list2.add(datalist2);
System.out.println(list2);
Collection<List<Country>> list3 = Stream.concat(list1.get(0).stream(), list2.get(0).stream())
.collect(Collectors.toMap(Country::getCountryName, Country::getOperator, Country::merge)).values();
}
private static final class Country {
private final String countryName;
private final String operator;
private final List<String> value;
public Country(String countryName, String name, List<String> values) {
this.countryName = countryName;
this.operator = name;
this.value = values;
}
public String getCountryName() {
return countryName;
}
public String getOperator() {
return operator;
}
public List<String> getValue() {
return value;
}
/*
* This method is accepting both Country and merging data that you need.
*/
public static Country merge(Country country1, Country country2) {
if (country1.getCountryName().equalsIgnoreCase(country2.getCountryName().toLowerCase())
&& country1.getOperator().equalsIgnoreCase(country2.getOperator().toLowerCase())) {
List<String> newValue = country1.getValue();
newValue.add(country2.getValue().get(0));
Integer Total = Integer.parseInt(country1.getValue().get(0)) + Integer.parseInt(country2.getValue().get(0));
newValue.add(Total.toString());
return new Country(country1.getCountryName(), country1.getOperator(), newValue);
}
return new Country(country1.getCountryName(), country1.getOperator(), country1.getValue());
}
}
}
答案 0 :(得分:1)
如果您使用的是java8,可以在此处使用Map和merge选项。您只需要实现自己的合并方法,它将连接两个元素的数据。
在下面的示例中,我调用了您的班级Country
。
@Test
public void should_create_plain_text_tag_for_booking_init_rq() {
List<Country> data1 = new ArrayList<>();
List<Country> data2 = new ArrayList<>();
Collection<Country> results = Stream.concat(data1.stream(), data2.stream())
.collect(Collectors.toMap(
Country::getCountryName, // I asume that you want to merge Data with same country name.
Function.identity(),
Country::merge)
).values();
}
static class Country {
private final String countryName;
private final String name;
private final List<Integer> values;
public Country(String countryName, String name, List<Integer> values) {
this.countryName = countryName;
this.name = name;
this.values = values;
}
/* This method is accepting both Country and merging data that you need. */
public static Country merge(Country country1, Country country2) {
List<Integer> newValue = country1.getValues();
newValue.addAll(country2.getValues());
// compute sum here;
return new Country(country1.getCountryName(), country1.getName(), newValue);
}
// getters
}