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

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

问题描述

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



例如

  **列表1 ** **列表2 ** 
milan hafil
dingo iga
iga dingo
elpha binga
hafil mike
meat dingo
milan
elpha
meat
iga
neeta.peeta

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



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



请帮助



PS:这不是一个学校作业:)所以如果你只是指导我,足够

解决方案

EDIT



这里有两个版本。一个使用 ArrayList 和其他使用 HashSet



并从中创建自己的版本,直到您获得所需的内容。



这应该足以覆盖:


这不是一个学校作业:)所以,如果你只是指导我,这将是足够的


您的问题的一部分。



继续使用原始答案



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

EDIT >

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



此第二个版本提供了这个集合的所有元素。



这次 Collection 可以是一个设置而不是列表(区别是,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重复
一个:[milan,iga,dingo,iga,elpha,iga,hafil,iga,meat,iga,neeta.peeta,iga ]

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

类似:[dingo,iga,hafil]
$ b b不同:[mike,binga,milan,meat,elpha,neeta.peeta]

完全按照你的需要,它给你一个好的开始,所以你可以从这里处理。



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


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

解决方案

EDIT

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.

This should be enough to cover the:

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

part of your question.

continuing with the original answer:

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

The retainAll method does the following:

Retains only the elements in this collection that are contained in the specified collection

see this sample:

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

EDIT

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

Removes all of this collection's elements that are also contained in the specified collection.

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

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

Output:

$ 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天全站免登陆