Java 比较两个ArrayList以匹配后处理条件
我有两个数组列表Java 比较两个ArrayList以匹配后处理条件,java,arraylist,Java,Arraylist,我有两个数组列表 ArrayList A ArrayList B London 001 Berlin 001 Frankfurt 450 Rome 001 Geneva 230 Lille 620 我要打印的内容如下: 如果arraylist中的代码不相等,则向其添加单独的XML标记。如果他们是平等的,那么俱乐部在一个单一的标签他们 例如 伦敦柏林法兰克福罗马日内瓦里尔 下面是我使用的逻辑 List<String> ne
ArrayList A ArrayList B
London 001
Berlin 001
Frankfurt 450
Rome 001
Geneva 230
Lille 620
我要打印的内容如下:
如果arraylist中的代码不相等,则向其添加单独的XML标记。如果他们是平等的,那么俱乐部在一个单一的标签他们
例如
伦敦柏林法兰克福罗马日内瓦里尔
下面是我使用的逻辑
List<String> newList = new ArrayList<String>();
for(int i= 0; i< ListA.size(); i++){
if(i >=1){
String temp = ListB.get(i-1);
if(temp.contentEquals(ListB.get(i)))
{
newList.add(ListA.get(i));
}
else{
newList.add("<"+ ListB.get(i) +"> " + ListA.get(i) + " </"+ ListB.get(i) +">" );
}
}
else{
/*if i=0*/
newList.add("<"+ ListB.get(i) +"> " + ListA.get(i) + " </"+ ListB.get(i) +">" );
}
}
StringJoiner outputText = new StringJoiner(" ");
for(int i=0; i< newList.size();i++){
outputText.add(newList.get(i));
}
System.out.println(outputText.toString());
}
List newList=newarraylist();
对于(inti=0;i=1){
String temp=ListB.get(i-1);
if(temp.contentEquals(ListB.get(i)))
{
newList.add(ListA.get(i));
}
否则{
newList.add(“+ListA.get(i)+”);
}
}
否则{
/*如果i=0*/
newList.add(“+ListA.get(i)+”);
}
}
StringJoiner outputText=新的StringJoiner(“”);
对于(int i=0;i
我知道逻辑有问题。只是在循环中迷失了方向。见下文:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MultipleLists {
public static void main(String[] args) {
// London 001
// Berlin 001
// Frankfurt 450
// Rome 001
// Geneva 230
// Lille 620
List<String> cities = Arrays.asList("London", "Berlin", "Frankfurt", "Rome", "Geneva", "Lille");
List<String> codes = Arrays.asList("001", "001", "450", "001", "230", "620");
List<CityCode> cityCodes = new ArrayList<>();
for (int i = 0; i < cities.size(); i++) {
cityCodes.add(new CityCode(cities.get(i), codes.get(i)));
}
StringBuffer stringBuffer = new StringBuffer();
Collections.sort(codes);
Set<String> codesSet = new HashSet<>(codes);
for (String code : codesSet) {
stringBuffer.append("<" + code + ">");
for (CityCode cityCode : cityCodes) {
if (cityCode.getCode().compareTo(code) == 0) {
stringBuffer.append(cityCode.getName());
stringBuffer.append(" ");
}
}
stringBuffer.append("</" + code + ">");
}
System.out.println(stringBuffer); // <001>London Berlin Rome </001><620>Lille </620><230>Geneva </230><450>Frankfurt </450>
}
}
class CityCode {
private String name;
private String code;
public CityCode(String name, String code) {
this.name = name;
this.code = code;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Set;
公共类多重列表{
公共静态void main(字符串[]args){
//伦敦001
//柏林001
//法兰克福450
//罗马001
//日内瓦230
//里尔620
列出城市=数组。asList(“伦敦”、“柏林”、“法兰克福”、“罗马”、“日内瓦”、“里尔”);
列表代码=数组。asList(“001”、“001”、“450”、“001”、“230”、“620”);
List citycode=new ArrayList();
对于(int i=0;i
见下文:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MultipleLists {
public static void main(String[] args) {
// London 001
// Berlin 001
// Frankfurt 450
// Rome 001
// Geneva 230
// Lille 620
List<String> cities = Arrays.asList("London", "Berlin", "Frankfurt", "Rome", "Geneva", "Lille");
List<String> codes = Arrays.asList("001", "001", "450", "001", "230", "620");
List<CityCode> cityCodes = new ArrayList<>();
for (int i = 0; i < cities.size(); i++) {
cityCodes.add(new CityCode(cities.get(i), codes.get(i)));
}
StringBuffer stringBuffer = new StringBuffer();
Collections.sort(codes);
Set<String> codesSet = new HashSet<>(codes);
for (String code : codesSet) {
stringBuffer.append("<" + code + ">");
for (CityCode cityCode : cityCodes) {
if (cityCode.getCode().compareTo(code) == 0) {
stringBuffer.append(cityCode.getName());
stringBuffer.append(" ");
}
}
stringBuffer.append("</" + code + ">");
}
System.out.println(stringBuffer); // <001>London Berlin Rome </001><620>Lille </620><230>Geneva </230><450>Frankfurt </450>
}
}
class CityCode {
private String name;
private String code;
public CityCode(String name, String code) {
this.name = name;
this.code = code;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Set;
公共类多重列表{
公共静态void main(字符串[]args){
//伦敦001
//柏林001
//法兰克福450
//罗马001
//日内瓦230
//里尔620
列出城市=数组。asList(“伦敦”、“柏林”、“法兰克福”、“罗马”、“日内瓦”、“里尔”);
列表代码=数组。asList(“001”、“001”、“450”、“001”、“230”、“620”);
List citycode=new ArrayList();
对于(int i=0;i
您的逻辑错误,请尝试以下方法:
for (int i = 0; i < ListA.size(); i++) {
if (i == 0) {
newList.add("<" + ListB.get(i) + "> " + ListA.get(i) + " ");
}
if (i >= 1) {
String temp = ListB.get(i - 1);
if (temp.equals(ListB.get(i))) {
newList.add(ListA.get(i));
} else {
newList.add("</" + ListB.get(i - 1) + ">" + " <" + ListB.get(i) + "> " + ListA.get(i) + " ");
}
}
if (i == ListA.size() - 1) {
newList.add("</" + ListB.get(i) + ">");
}
}
for(int i=0;i=1){
String temp=ListB.get(i-1);
if(temp.equals(ListB.get(i))){
newList.add(ListA.get(i));
}否则{
newList.add(“+”+ListA.get(i)+”);
}
}
如果(i==ListA.size()-1){
新列表。添加(“”);
}
}
通过使用此逻辑,您将获得准确的所需输出如果您的逻辑错误,请尝试此逻辑:
for (int i = 0; i < ListA.size(); i++) {
if (i == 0) {
newList.add("<" + ListB.get(i) + "> " + ListA.get(i) + " ");
}
if (i >= 1) {
String temp = ListB.get(i - 1);
if (temp.equals(ListB.get(i))) {
newList.add(ListA.get(i));
} else {
newList.add("</" + ListB.get(i - 1) + ">" + " <" + ListB.get(i) + "> " + ListA.get(i) + " ");
}
}
if (i == ListA.size() - 1) {
newList.add("</" + ListB.get(i) + ">");
}
}
for(int i=0;i=1){
String temp=ListB.get(i-1);
if(temp.equals(ListB.get(i))){
newList.add(ListA.get(i));
}否则{
newList.add(“+”+ListA.get(i)+”);
}
}
如果(i==ListA.size()-1){
新列表。添加(“”);
}
}
通过使用此逻辑,您将准确地获得所需的输出类似的内容
public static void main(String[] args) {
List<String> cities = Arrays.asList("London", "Berlin", "Frankfurt", "Rome", "Geneva", "Lille");
List<String> codes = Arrays.asList("001", "001", "450", "001", "230", "620");
Map<String, List<String>> result = new HashMap<>();
for (int i = 0; i < cities.size(); i++) {
String city = cities.get(i);
String code = codes.get(i);
if (result.containsKey(code)) {
List<String> list = result.get(code);
list.add(city);
} else {
ArrayList<String> mappedCities = new ArrayList<>();
mappedCities.add(city);
result.put(code, mappedCities);
}
}
String fullXml = result.entrySet().stream().parallel().map(entry -> {
String tag = entry.getKey();
List<String> vals = entry.getValue();
String citiesSeparated = vals.stream().collect(Collectors.joining(" "));
return xmlStart(tag) + citiesSeparated + xmlEnd(tag);
}).collect(Collectors.joining(" "));
System.out.println(fullXml);
}
private static String xmlEnd(String s) {
return "<" + s + "/>";
}
private static String xmlStart(String s) {
return "<" + s + ">";
}
publicstaticvoidmain(字符串[]args){
List cities=array.asList(“伦敦”、“柏林”、“法兰克福”、“罗马”、“日内瓦”、”
Map<String,String> codeCityMap = new TreeMap<String,String>();
for (int i=0;i <arrayListA.size();i++)
{
String cities = codeCityMap.get(arrayListB.get(i));
cities = null == cities?arrayListA.get(i):cities + " " + arrayListA.get(i);
codeCityMap.put(arrayListB.get(i), cities);
}
List<String> newList = new ArrayList<String>();
for (String code: codeCityMap.keySet())
{
newList.add("<" + code + ">" + codeCityMap.get(code) + "</" + code + ">");
}