Tanvir
Tanvir

Reputation: 174

Adding Multiple Set<String> in Java

I have two sets as: set1 and set2 that I want to combine.

set1 contains personID and place as: [1-NY, 2-CA, 3-MD, 1-TX, 3-VA]

set2 contains personName and place as: [John-NY, Bill-CA, Ron-CA, Rick-MD, John-TX, Rick-VA]

I want to combine both the set such that I will get the output of personID, personName and place as: [1-John-NY, 2-Bill-CA, 2-Ron-CA, 3-Rick-MD, 1-John-TX, 3-Rick-VA].

Basically the thing is: I want to use "place" as the anchor to combine.

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

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

But, I am not able to get the output in my expected way. Any suggestion please. Thanks!

Upvotes: 1

Views: 3732

Answers (2)

Mincong Huang
Mincong Huang

Reputation: 5552

As user chrylis suggests, you could use class for this propose. First, create a class Person.class to store the required values: person ID / person name / place name. For simplifying the process, a constructor with 3 parameters is used here to construct the object, but it's not the only choice. By the way, I strongly suggest you to use a unique value for each person.

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

Then create a method to combine the different information stored in the person class.

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

Now you can put the data into the set 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());
}

Here's the full implementation of class 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);
    }
}

Upvotes: 0

Enigo
Enigo

Reputation: 3895

You should rethink your approach a bit. In order to combine these two sets you should create some kind of look-up table. I would use simple HashMap for this. The code is really self-explanatory, but fell free to ask questions)

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

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

Upvotes: 1

Related Questions