简体   繁体   English

如何比较两个列表以获取匹配元素和不匹配元素

[英]How to compare two list to get matching elements and non matching elements

Below point 1 and point 2 are my input, in below code i'm trying to compare both the Arraylist to get matching and nonmatching elements, But i'm geting only non matching records.第 1 点和第 2 点下方是我的输入,在下面的代码中,我试图比较 Arraylist 以获得匹配和不匹配的元素,但我只得到不匹配的记录。

  1. arraylist1 = [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 ] arraylist1 = [检查文件等于,文件日期等于之间,基金编号等于,账户编号等于在,锡等于等于,公司编号等于等于,经销商编号等于等于,成员ID等于等于,组ID等于等于, Account Type Like Equals In, Document Type Code Like Equals In, Print Equals, Test two ]

  2. arraylist2 = [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] arraylist2 = [文档日期等于之间,基金编号等于,帐号等于输入,锡等于等于,公司编号等于等于,经销商编号等于]

3.when i'm trying to remove extra from space "arrayList2" i'm getting below [Document Date LikeEqualsBetween, Fund Number EqualsLike, Account Number LikeEqualsIn, Tin EqualsLike, 3.当我试图从空间“arrayList2”中删除额外内容时,我得到低于 [文档日期 LikeEqualsBetween、资金编号 EqualsLike、帐号 LikeEqualsIn、Tin EqualsLike、

  1. My code starts from here我的代码从这里开始

     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.below is the out put i'm getting, You can see i'm getting only nonmatching value there is no matching value. 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

-----------------Not matching elements--------------------- = Account Number Like Equals In -----------------Not matching elements--------------------- = Company Number Like Equals -----------------不匹配的元素--------------------- = 帐号等于在 --- --------------不匹配的元素--------- = 公司编号等于

I set this up with a simple main method and it seems to be working for me.我用一个简单的主要方法设置它,它似乎对我有用。 You may want to try it with a simple example like mine.您可能想用像我这样的简单示例进行尝试。

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);
            }
        }
    }
}

Output: Output:

-----------------matching Elements--------------------- = a -----------------匹配元素--------------------- = a

-----------------Not matching elements--------------------- = b -----------------不匹配的元素--------------------- = b

You can achieve the same result by using a single hashmap.您可以通过使用单个 hashmap 来获得相同的结果。

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);
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java8 Streams中通过多个过滤谓词比较两个Map列表以识别匹配和不匹配的记录 - How to compare Two List of Map to identify the matching and non matching records with multiple filter predicates in Java8 Streams 链接列表中的匹配元素 - Matching elements in Linked List 如何在数组列表中搜索匹配的元素,然后使用这些元素创建列表 - how to search an array list for matching elements then create a list with those elements 如果所有条件都在谓词中给出,则获取所有匹配的非null元素 - get all matching non null elements if all the conditions are given in predicate 如何从列表中获取匹配和不匹配的字符串 - How to get matching and non matching string from list 如何只比较前三个字符并删除匹配的元素? - How to compare only first three characters and remove matching elements? 如何比较两个字符串并用 - 替换不匹配的字母? - How to compare two strings and replace non-matching letters with -? 如何根据包含的元素比较两个列表? - How to compare two list based on elements it contains? 如何在两个数组之间的不同位置打印匹配元素? (爪哇) - How to print matching elements in different positions between two arrays? (java) 如何使用java从列表中删除重复项和匹配的原始元素 - How to remove duplicates and matching original elements from list using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM