簡體   English   中英

Java:比較兩個字符串數組並刪除兩個數組中存在的元素

[英]Java: Comparing two string arrays and removing elements that exist in both arrays

這主要是一個表現問題。 我有一個String數組AllUids中存在的所有用戶的主列表。 我還有一個String數組EndUids中存在的所有最終用戶的列表。

我在Java工作,我的目標是從主列表AllUids中刪除最終日期數組中存在的任何用戶。 我知道PHP有一個名為array_diff的函數。

我很好奇Java是否有任何可以比較兩個數組並刪除兩者相似的元素。 我的目標是這里的表現,這就是我詢問內置功能的原因。 我不想添加任何特殊包。

我想過寫一個遞歸函數,但看起來效率很低。 兩個列表中都有數千個用戶。 要存在於結束日期列表中,您必須存在於AllUids列表中,直到被刪除。

例:

String[] AllUids = {"Joe", "Tom", "Dan", "Bill", "Hector", "Ron"};

String[] EndUids = {"Dan", "Hector", "Ron"};

我正在尋找的功能:

String[] ActiveUids = AllUids.RemoveSimilar(EndUids);

ActiveUids看起來像這樣:

{"Joe", "Tom", "Bill"}

謝謝大家,顯然我可以提出循環等但我不相信它會有效率。 這是每天在生產機器上運行的東西。

Commons Collections有一個名為CollectionUtils的類和一個名為removeAll的靜態方法,它接受一個初始列表和一個要從該列表中刪除的東西列表:

Collection removeAll(Collection collection,
                     Collection remove)

如果你使用用戶列表而不是數組,那應該做你想要的。 您可以使用Arrays.asList()將數組轉換為一個列表,以便...

Collection ActiveUids = CollectionUtils.removeAll(Arrays.asList(AllUids), 
                                                  Arrays.asList(EndUids))

編輯:我也對Commons Collections進行了一些挖掘,並在Commons Collections中找到了ListUtils的以下解決方案:

List diff = ListUtils.subtract(Arrays.asList(AllUids), Arrays.asList(EndUids));

很簡約...

您無法從數組中“刪除”元素。 您可以將它們設置為null,但數組的大小是固定的。

可以使用java.util.SetremoveAll從一個遠離另一個,但我更喜歡使用Google Collections Library

Set<String> allUids = Sets.newHashSet("Joe", "Tom", "Dan",
                                      "Bill", "Hector", "Ron");
Set<String> endUids = Sets.newHashSet("Dan", "Hector", "Ron");
Set<String> activeUids = Sets.difference(allUids, endUids);

這有一個更實用的感覺。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Bireswhar
 */
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

        String[] s1 = {"ram", "raju", "seetha"};
        String[] s2 = {"ram"};
        List<String> s1List = new ArrayList(Arrays.asList(s1));
        for (String s : s2) {
            if (s1List.contains(s)) {
                s1List.remove(s);
            } else {
                s1List.add(s);
            }
             System.out.println("intersect on " + s1List);
        }
    }
}

最簡單的解決方案可能是將所有元素放入Set中,然后使用removeAll。 您可以從這樣的數組轉換為Set:

Set<String> activeUids = new HashSet<String>(Arrays.asList(activeUidsArray));

雖然你應該盡量避免使用數組和青睞集合。

不要為此使用數組,使用Collection和removeAll()方法。 至於性能:除非你做一些導致O(n ^ 2)運行時的愚蠢行為,否則就算了。 這是不成熟的優化,無用/有害的。 “成千上萬的用戶”並不算什么,除非你每秒都做數千次。

順便說一句,PHP“數組”實際上是哈希映射。

您可以將這些字符串放入Collection中 ,然后使用removeAll方法。

    String s1 = "a,b,c,d";
    String s2 = "x,y,z,a,b,c";
    Set<String> set1 = new HashSet<String>();
    Set<String> set2 = new HashSet<String>();

    Set<String> set11 = new HashSet<String>();

    String[] splitS1 = s1.split(",");
    String[] splitS2 = s2.split(",");

    for(String s3:splitS1){
        set1.add(s3);
        set11.add(s3);
    }

    for(String s4:splitS2){
        set2.add(s4);
    }
    set1.removeAll(set2);
    set2.removeAll(set11);
    set1.addAll(set2);
    System.out.println(set1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM