1

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!

5
  • 3
    Don't. This is exactly what classes are for. Commented Jan 9, 2017 at 3:51
  • 1
    What if you have two people in the same place? Commented Jan 9, 2017 at 3:53
  • 1
    apart from you should use class to represent your data, there are other uncertainties. For example, what do you want if there is more than 1 entry with same "place"? what if certain place exists in one set but not the other? Commented Jan 9, 2017 at 3:54
  • 2-Bill-CA and 2-Ron-CA: you have to people with the same personID? Bad juju. Commented Jan 9, 2017 at 4:00
  • Might be an X-Y-problem. So, better than talking about your actual question, would you tell us what you are trying to achieve in the big picture? Including why person IDs are paired with cities in the first place? Commented Jan 9, 2017 at 10:24

2 Answers 2

1

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);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Clearly explained! Thanks!
0

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

1 Comment

Well explained! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.