2017-06-04 56 views
-2

如何比較java中的兩個哈希集?我的第一個哈希集如下所示。如何比較java中的兩個哈希集?

static Set<String> nounPhrases = new HashSet<>(); 

上面的哈希集包含像這樣的元素。

列表名詞解析的:【JAVA,JSP,書]

第二散置

static Set<String> nounPhrases2 = new HashSet<>(); 

名詞解析名單:網頁,PHP,JAVA,書]

注意 - 我需要檢查兩套中是否有相同的名詞。如果他們有類似的名詞,那麼我需要做另一項任務

+1

什麼樣的結果你想? – Sweeper

+4

定義「比較」。你在找交集嗎?或者兩套具有完全相同的元素?另外,你到目前爲止嘗試過什麼? – Sudicode

+1

'seta.equals(setb)'有什麼問題? – laune

回答

0
public class SetUtils { 

    public static boolean equals(Set<?> set1, Set<?> set2){ 

     if(set1 == null || set2 ==null){ 
      return false; 
     } 

     if(set1.size()!=set2.size()){ 
      return false; 
     } 

     return set1.containsAll(set2); 

    } 
} 
+1

這基本上是Set.equals(),因爲它已經存在。 – Durandal

0

所以你的意思是這樣的?

public static void main(String[] args) { 

    final Set<String> nounPhrases = new HashSet<>(); 
    nounPhrases.add("java"); 
    nounPhrases.add("jsp"); 
    nounPhrases.add("book"); 

    final Set<String> nounPhrases2 = new HashSet<>(); 
    nounPhrases2.add("web"); 
    nounPhrases2.add("php"); 
    nounPhrases2.add("java"); 
    nounPhrases2.add("book"); 

    // Checking for every element in first set 
    for (final String element : nounPhrases) { 

     // if second set has the current element 
     if (nounPhrases2.contains(element)) { 
      System.out.println("They have " + element); 
     } 
    } 
} 

我的輸出:

They have java 
They have book 

編輯:基於您的評論 ,如果我理解正確的,如果你想在這兩組共同要素,只是存儲的值並返回它們:

public static void main(String[] args) { 

    final Set<String> nounPhrases = new HashSet<>(); 
    nounPhrases.add("java"); 
    nounPhrases.add("jsp"); 
    nounPhrases.add("book"); 

    final Set<String> nounPhrases2 = new HashSet<>(); 
    nounPhrases2.add("web"); 
    nounPhrases2.add("php"); 
    nounPhrases2.add("java"); 
    nounPhrases2.add("book"); 

    System.out.println(getCommon(nounPhrases, nounPhrases2)); 
} 

public final static Set<String> getCommon(Set<String> setA, Set<String> setB) { 

    final Set<String> result = new HashSet<>(); 
    for (final String element : setA) { 
     if (setB.contains(element)) { 
      result.add(element); 
     } 
    } 
    return result; 
} 

你可以使用仿製藥,對其他元素不是字符串的方法工作:

public final static <T> Set<T> getCommon(Set<T> setA, Set<T> setB) { 

    final Set<T> result = new HashSet<>(); 
    for (final T element : setA) { 
     if (setB.contains(element)) { 
      result.add(element); 
     } 
    } 
    return result; 
} 

如果性能很重要,則應首先檢查大小,然後僅遍歷較小集合的元素。如果你有1個元素和1個元素,那麼從較小的一開始就爲你提供一次迭代,而從較大的開始會給你100個檢查,其中只有1個可能在兩個集合中。

+0

你好,我得到低於輸出。我怎麼能顯示所有相等的元素名詞解析名單:【JAVA,JSP,書]名詞解析的 名單:[Java的意識中,網頁,JSP,書] 他們有 - 書 – user8048032

+0

我不明白你的題。你會得到什麼輸出?用我的代碼,book和java都打印出來。 – Dennux

+0

這是我的代碼https://paste.ofcode.org/X7NVBEdhBA2ux4KGjPfmTv – user8048032

0

如果妳希望找到共同的元素,然後用收集(Collectors.toList()),而不是數量,如果妳只是想找到多少集有使用普通的Java元素8

long count = nounPhrases.stream().filter(tempstring -> { 
      return nounPhrases2.stream().anyMatch(tempstring2 -> { 
       return tempstring.equals(tempstring2); 
      }); 
     }).count(); 
     if (count > 0) 
      System.out.println("has common elements-"+count); 
     else 
      System.out.println("not common"); 
+0

而不是篩選和使用count(),你可以使用anyMatch()。它會停止在第一場比賽,因此會更快(更清晰)。 –

+0

已更新@JB Nizet – UchihaObito

+0

您離開了count()。 anyMatch()返回一個布爾值。 –

0

由使用Java apache.commons.collections包我們可以實現

package com.StackoverFlow; 

import java.util.Collection; 
import java.util.HashSet; 
import java.util.Set; 
import org.apache.commons.collections.CollectionUtils; 
public class MainClass { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 


     Set hs_1 = new HashSet(); 
     hs_1.add("A"); 
     hs_1.add("B"); 
     hs_1.add("C"); 
     hs_1.add("D"); 

     Set hs_2 = new HashSet(); 
     hs_2.add("A"); 
     hs_2.add("B"); 
     hs_2.add("C"); 
     hs_2.add("D"); 

     Collection result = CollectionUtils.subtract(hs_1, hs_2); 
     System.out.println(result); 
     if(result.isEmpty()){ 
      System.out.println("perform Task-->>Value maches "); 

     }else{ 
      System.out.println("perform Task-->>Value not maches "); 
     } 

    } 

} 
0

這是一個已經發明的車輪。

Set#equals()的方式相比集,你會期望:

set1.equals(set2) 

如果你想兩個Set變量都是空是「平等」,然後使用:

Objects.equals(set1, set2)