在点1和点2下面是我的输入,在下面的代码中,我试图比较Arraylist以获取匹配和不匹配的元素,但是我仅获取不匹配的记录。
arraylist1 = [检查文档等于,文档日期如等于,基金编号如等于,帐号如等于,锡如等于,公司编号如等于,经销商编号如等于,会员编号如等于, ,组ID像等于,帐户类型像等于,文档类型代码像等于,打印等于,测试两个]
arraylist2 = [文档日期类似 等于 两者之间,基金数量相等 喜欢,帐号喜欢 等于 在,锡等于 像,公司编号等于 喜欢,经销商编号喜欢 等于]
3。当我试图从空间“ arrayList2”中删除多余内容时 [文档日期LikeEqualsBetween,资金编号EqualsLike,帐号LikeEqualsIn,锡EqualsLike,
我的代码从这里开始
HashSet<String> hs1 = new HashSet<String>();
HashSet<String> hs2 = new HashSet<String>();
for (String match : arraylist2 ) {
hs1.add(match);
}
for (String match : arraylist1) {
hs2.add(match);
}
for (String match : hs2) {
boolean b = hs1.add(match);
if (b == false) {
matchingFields = match;
System.out.println("-----------------matching Elements--------------------- = " +
matchingFields);
} else {
nonMatchingFields = match;
System.out.println("-----------------Not matching elements--------------------- = " +
nonMatchingFields);
}
}
4。下面是我得到的输出,您可以看到我只得到不匹配的值,没有匹配的值。
--------Not matching elements--------------------- = Document Type Code Like Equals In
---------Not matching elements--------------------- = Test two
--------Not matching elements--------------------- = Document Date Like Equals Between
-----------------Not matching elements--------------------- = Check Document Equals
-----------------Not matching elements--------------------- = Member Id Like Equals In
-----------------Not matching elements--------------------- = Dealer Number Like Equals In
-----------------Not matching elements--------------------- = Print Equals
-----------------Not matching elements--------------------- = Group Id Like Equals In
-----------------Not matching elements--------------------- = Fund Number Like Equals
-----------------Not matching elements--------------------- = Tin Like Equals
-----------------Not matching elements--------------------- = Account Type Like Equals In
-----------------不匹配的元素--------------------- =帐号等于 -----------------不匹配元素--------------------- =公司编号等于
答案 0 :(得分:0)
我用一个简单的main方法设置了它,这似乎对我有用。您可能想通过像我这样的简单示例进行尝试。
package test;
import java.util.ArrayList;
import java.util.HashSet;
class Test{
public static void main(String[] args) {
ArrayList<String> a1 = new ArrayList<String>();
a1.add("a");
a1.add("b");
ArrayList<String> a2 = new ArrayList<String>();
a2.add("c");
a2.add("a");
checkMatch(a1, a2);
}
public static void checkMatch(ArrayList<String> arraylist1, ArrayList<String> arraylist2 ) {
HashSet<String> hs1 = new HashSet<String>();
HashSet<String> hs2 = new HashSet<String>();
for (String match : arraylist2 ) {
hs1.add(match);
}
for (String match : arraylist1) {
hs2.add(match);
}
for (String match : hs2) {
boolean b = hs1.add(match);
if (b == false) {
String matchingFields = match;
System.out.println("-----------------matching Elements--------------------- = " +
matchingFields);
} else {
String nonMatchingFields = match;
System.out.println("-----------------Not matching elements--------------------- = " +
nonMatchingFields);
}
}
}
}
输出:
-----------------匹配元素--------------------- = a
-----------------不匹配的元素--------------------- = b
答案 1 :(得分:0)
您可以通过使用单个哈希图来达到相同的结果。
import java.util.*;
public class Practice {
public static void main(String[] args) {
ArrayList<String> arraylist1 = new ArrayList<>(Arrays.asList("Check Document Equals" , "Document Date Like Equals Between", "Fund Number Like Equals" , "Account Number Like Equals In", "Tin Like Equals" , "Company Number Like Equals" , "Dealer Number Like Equals In", "Member Id Like Equals In", "Group Id Like Equals In", "Account Type Like Equals In", "Document Type Code Like Equals In", "Print Equals" , "Test two" ));
ArrayList<String> arraylist2 = new ArrayList<>(Arrays.asList("Document Date Like Equals Between", "Fund Number Equals Like", "Account Number Like Equals In", "Tin Equals Like", "Company Number Equals Like", "Dealer Number Like Equals"));
HashSet<String> hs = new HashSet<String>(arraylist1);
for (String match : arraylist2) {
if (hs.contains(match)) {
System.out.println("-----------------matching Elements--------------------- = " + match);
}
else {
System.out.println("-----------------Not matching elements--------------------- = " + match);
}
}
}
}