Java Compare Two Lists

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

For example

**List 1**            **Lists 2**
milan                 hafil
dingo                 iga
iga                   dingo
elpha                 binga
hafil                 mike
meat                  dingo
milan
elpha
meat
iga
neeta.peeta

I'd like a method that returns how many elements are same. For this example it should be 3 and it should return me similar values of both list and different values too.

Should I use hashmap if yes then what method to get my result?

Please help

P.S: It is not a school assignment :) So if you just guide me it will be enough

450217 次浏览

假设 hash1hash2

List< String > sames = whatever
List< String > diffs = whatever


int count = 0;
for( String key : hash1.keySet() )
{
if( hash2.containsKey( key ) )
{
sames.add( key );
}
else
{
diffs.add( key );
}
}


//sames.size() contains the number of similar elements.

这些真的是 lists(有序的,有重复的) ,还是 设置(无序的,没有重复的) ?

因为如果是后者,那么您可以使用,比如说,java.util.HashSet<E>,并使用方便的 retainAll在预期的线性时间内完成此操作。

    List<String> list1 = Arrays.asList(
"milan", "milan", "iga", "dingo", "milan"
);
List<String> list2 = Arrays.asList(
"hafil", "milan", "dingo", "meat"
);


// intersection as set
Set<String> intersect = new HashSet<String>(list1);
intersect.retainAll(list2);
System.out.println(intersect.size()); // prints "2"
System.out.println(intersect); // prints "[milan, dingo]"


// intersection/union as list
List<String> intersectList = new ArrayList<String>();
intersectList.addAll(list1);
intersectList.addAll(list2);
intersectList.retainAll(intersect);
System.out.println(intersectList);
// prints "[milan, milan, dingo, milan, milan, dingo]"


// original lists are structurally unmodified
System.out.println(list1); // prints "[milan, milan, iga, dingo, milan]"
System.out.println(list2); // prints "[hafil, milan, dingo, meat]"

EDIT

这里有两个版本,一个使用 ArrayList,另一个使用 HashSet

比较它们并从中创建 own版本,直到您得到您需要的。

这应足以涵盖:

PS: 这不是学校布置的作业:)所以只要你指导我就够了

你问题的一部分。

continuing with the original answer:

你可以使用 java.util.Collection和/或 java.util.ArrayList

retainAll方法具有以下功能:

只保留此集合中包含在指定集合中的元素

看看这个例子:

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;


public class Repeated {
public static void main( String  [] args ) {
Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));


listOne.retainAll( listTwo );
System.out.println( listOne );
}
}

剪辑

对于第二部分(类似的值) ,您可以使用 删除所有方法:

移除此集合中也包含在指定集合中的所有元素。

第二个版本还提供了重复的类似值和句柄(通过丢弃它们)。

这一次,Collection可以是 Set而不是 List(区别在于,Set 不允许重复的值)

import java.util.Collection;
import java.util.HashSet;
import java.util.Arrays;


class Repeated {
public static void main( String  [] args ) {


Collection<String> listOne = Arrays.asList("milan","iga",
"dingo","iga",
"elpha","iga",
"hafil","iga",
"meat","iga",
"neeta.peeta","iga");


Collection<String> listTwo = Arrays.asList("hafil",
"iga",
"binga",
"mike",
"dingo","dingo","dingo");


Collection<String> similar = new HashSet<String>( listOne );
Collection<String> different = new HashSet<String>();
different.addAll( listOne );
different.addAll( listTwo );


similar.retainAll( listTwo );
different.removeAll( similar );


System.out.printf("One:%s%nTwo:%s%nSimilar:%s%nDifferent:%s%n", listOne, listTwo, similar, different);
}
}

产出:

$ java Repeated
One:[milan, iga, dingo, iga, elpha, iga, hafil, iga, meat, iga, neeta.peeta, iga]


Two:[hafil, iga, binga, mike, dingo, dingo, dingo]


Similar:[dingo, iga, hafil]


Different:[mike, binga, milan, meat, elpha, neeta.peeta]

如果它不能完全满足你的需要,它会给你一个良好的开端,这样你就可以从这里处理。

给读者的问题: 如何包括所有重复的值?

您可以尝试 CollectionUtils中的 intersection()subtract()方法。

intersection()方法提供一个包含公共元素的集合,而 subtract()方法提供所有不常见的元素。

他们也应该关注类似的元素

我在 < a href = “ http://www.gitarani.com/classfied.htm? classfiedId = 3012”rel = “ nofollow”> List Compare 找到了一个非常基本的 List 比较例子 此示例首先验证大小,然后检查一个列表中的特定元素在另一个列表中的可用性。

使用 java 8 RemoveIf

public int getSimilarItems(){
List<String> one = Arrays.asList("milan", "dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta");
List<String> 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();
}
public static boolean compareList(List ls1, List ls2){
return ls1.containsAll(ls2) && ls1.size() == ls2.size() ? true :false;
}


public static void main(String[] args) {


ArrayList<String> one = new ArrayList<String>();
one.add("one");
one.add("two");
one.add("six");


ArrayList<String> two = new ArrayList<String>();
two.add("one");
two.add("six");
two.add("two");


System.out.println("Output1 :: " + compareList(one, two));


two.add("ten");


System.out.println("Output2 :: " + compareList(one, two));
}

If you are looking for a handy way to test the equality of two collections, you can use org.apache.commons.collections.CollectionUtils.isEqualCollection, which compares two collections regardless of the ordering.

简单的解决办法:-

    List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "d", "c"));
List<String> list2 = new ArrayList<String>(Arrays.asList("b", "f", "c"));


list.retainAll(list2);
list2.removeAll(list);
System.out.println("similiar " + list);
System.out.println("different " + list2);

Output :-

similiar [b, c]
different [f]

Of all the approaches, I find using org.apache.commons.collections.CollectionUtils#isEqualCollection is the best approach. Here are the reasons -

  • 我不必申报任何额外的名单/设置自己
  • 我没有对输入列表进行变异
  • 它非常有效,它检查 O (N)复杂度中的相等性。

如果不可能将 apache.commons.collections作为一个依赖项,我建议实现它所遵循的算法来检查列表的相等性,因为它的效率很高。