0
두 개의 ArrayList 목록이 있습니다. ArrayList의 최종 목록을 병합 비교할 위치가 있습니다.를 비교하고 병합합니다. 최종 목록을 생성하는 Java의 목록 2 개를 병합합니다.
예를 들어 내가
List<List<String>> data1 = dao1.getall();
List<List<String>> data2 = dao2.getall();
데이터 1 이러한 항목을 얻을 데이터베이스 통화를하는
"results": [
[
"India",
"Idea",
"30"
],
[
"USA",
"Idea",
"10"
],
[
"irland",
"Idea",
"10"
]
데이터 2의 결과 세트처럼 보인다는
"results": [
[
"India",
"Idea",
"50"
],
[
"usa",
"Idea",
"30"
],
[
"sweden",
"Idea",
"10"
]
의 결과 세트처럼 보이는 나는 것 Country와 Operator 필드를 비교하여 아래와 같이 List of Lists를 병합하고 싶습니다.
"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"
]
누구든지 나를 도와 줄 수 있습니까? 사전에 감사드립니다.
나는 이것을 시도했지만 전혀 나를 위해 일하지 않았다. 당신이 java8를 사용하는 경우
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());
}
}
}
당신이 원하는 것을 이루기 위해 이미 코드를 가지고 있습니까? – LuisFerrolho