Java Compare Two Lists

后端 未结 10 596
情深已故
情深已故 2020-11-22 13:25

I have two lists ( not java lists, you can say two columns)

For example

**List 1**            **Lists 2**
  milan                 hafil
  dingo               


        
10条回答
  •  不知归路
    2020-11-22 13:29

    Using java 8 removeIf

    public int getSimilarItems(){
        List one = Arrays.asList("milan", "dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta");
        List two = new ArrayList<>(Arrays.asList("hafil", "iga", "binga", "mike", "dingo")); //Cannot remove directly from array backed collection
        int initial = two.size();
    
        two.removeIf(one::contains);
        return initial - two.size();
    }
    

提交回复
热议问题