Java 比较两个列表 [英] Java Compare Two Lists

查看:46
本文介绍了Java 比较两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表(不是java列表,你可以说是两列)

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

例如

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

我想要一个返回多少相同元素的方法.对于这个例子,它应该是3 并且它也应该返回列表和不同值的相似值.

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.

如果是,我应该使用 hashmap 那么什么方法可以得到我的结果?

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

请帮忙

P.S: 这不是学校作业:) 所以如果你只是指导我就足够了

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

推荐答案

EDIT

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

Here are two versions. One using ArrayList and other using HashSet

比较它们并从中创建您自己的版本,直到您获得所需的内容.

Compare them and create your own version from this, until you get what you need.

这应该足以涵盖:

P.S:这不是学校作业:) 所以如果你只是指导我就足够了

你问题的一部分.

继续原来的答案:

您可以为此使用 java.util.Collection 和/或 java.util.ArrayList.

You may use a java.util.Collection and/or java.util.ArrayList for that.

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

编辑

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

For the second part ( similar values ) you may use the removeAll method:

删除该集合中所有包含在指定集合中的元素.

第二个版本也为您提供了相似的值和重复处理(通过丢弃它们).

This second version gives you also the similar values and handles repeated ( by discarding them).

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

This time the Collection could be a Set instead of a List ( the difference is, the Set doesn't allow repeated values )

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]

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

If it doesn't do exactly what you need, it gives you a good start so you can handle from here.

读者问题:您将如何包含所有重复值?

Question for the reader: How would you include all the repeated values?

这篇关于Java 比较两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆