簡體   English   中英

添加多個集 <String> 在Java中

[英]Adding Multiple Set<String> in Java

我有兩個集合:set1和set2我想要結合。

set1包含personID,其位置為:[1-NY,2-CA,3-MD,1-TX,3-VA]

set2包含personName和場所,例如:[John-NY,Bill-CA,Ron-CA,Rick-MD,John-TX,Rick-VA]

我想將這兩個集合結合起來,這樣我將獲得personID,personName和place的輸出,例如:[1-John-NY,2-Bill-CA,2-Ron-CA,3-Rick-MD,1-John -TX,3-Rick-VA]。

基本上,事情是:我想使用“地方”作為結合的錨點。

Set<String> set1 = new LinkedHashSet<String>();
Set<String> set2 = new LinkedHashSet<String>();
Set<String> combination = new LinkedHashSet<String>();

combination.addAll(set1);
combination.addAll(set2);

但是,我無法以預期的方式獲得輸出。 請提出任何建議。 謝謝!

您應該重新考慮一下自己的方法。 為了將這兩個集合結合起來,您應該創建某種查找表。 我將為此使用簡單的HashMap 該代碼確實是不言自明的,但是可以隨意提問)

使用Java 8:

    Set<String> personIds = new LinkedHashSet<>(Arrays.asList("1-NY", "2-CA", "3-MD", "1-TX", "3-VA"));
    Set<String> personNames = new LinkedHashSet<>(Arrays.asList("John-NY", "Bill-CA", "Ron-CA", "Rick-MD", "John-TX", "Rick-VA"));

    Map<String, String> personIdMap = personIds.stream().map(v -> v.split("-"))
            .collect(Collectors.toMap(v -> v[1], v -> v[0]));

    Set<String> combination = new LinkedHashSet<>();
    personNames.forEach(name -> {
        final String[] split = name.split("-");
        final String personId = personIdMap.get(split[1]);
        combination.add(personId + '-' + name);
    });

使用Java 7:

    Set<String> personIds = new LinkedHashSet<>(Arrays.asList("1-NY", "2-CA", "3-MD", "1-TX", "3-VA"));
    Set<String> personNames = new LinkedHashSet<>(Arrays.asList("John-NY", "Bill-CA", "Ron-CA", "Rick-MD", "John-TX", "Rick-VA"));

    Map<String, String> personIdMap = new HashMap<>();
    for (String id : personIds) {
        final String[] split = id.split("-");
        personIdMap.put(split[1], split[0]);
    }

    Set<String> combination = new LinkedHashSet<>();
    for (String name : personNames) {
        final String[] split = name.split("-");
        final String personId = personIdMap.get(split[1]);
        combination.add(personId + '-' + name);
    }

正如用戶chrylis所建議的那樣,您可以將類用於此建議。 首先,創建一個類Person.class來存儲所需的值:人ID /人名/地名。 為了簡化過程,此處使用帶有3個參數的構造函數構造對象,但這不是唯一的選擇。 順便說一句,我強烈建議您為每個人使用唯一的值

public Person(String id, String name, String place) {
    this.id = id;
    this.name = name;
    this.place = place;
}

然后創建一種方法來組合存儲在人員類中的不同信息。

public String getCombination() {
    return String.format("%s-%s-%s", id, name, place);
}

現在,您可以將數據放入集合combinations

Set<Person> people = new LinkedHashSet<>();
people.add(new Person("1", "John", "NY"));
people.add(new Person("2", "Bill", "CA"));
people.add(new Person("2", "Ron", "CA"));
people.add(new Person("3", "Rick", "MD"));
people.add(new Person("1", "John", "TX"));
people.add(new Person("3", "Rick", "VA"));

Set<String> combinations = new LinkedHashSet<>();
for (Person p : people) {
    combinations.add(p.getCombination());
}

這是Person類的完整實現。

public class Person {

    private String id;  // maybe place id ?
    private String name;
    private String place;

    public Person(String id, String name, String place) {
        this.id = id;
        this.name = name;
        this.place = place;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPlace(String place) {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getCombination() {
        return String.format("%s-%s-%s", id, name, place);
    }
}

暫無
暫無

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

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