1

If I need to iterate over a list and try to group elements together, what is the best way to go about this?

Let's say for example we have the following objects to loop through:

People
  |---Jimmy
  |     -Country: Canada
  |---Johnny
  |     -Country: Ireland
  |---Vinny
  |     -Country: Italy
  |---Tommy
  |     -Country: Ireland
  |---Wendy
  |     -Country: Canada

I need to go through each person, and create a group with people in the same country.

For example, it starts with Jimmy, iterates through the list to find anyone in Canada, only Wendy matches the same country so they both get grouped together. When iterating to the next person, we don't want to go through the same process for Wendy (as she is already grouped). So we need to remove her from the original list.

I've read that the iterator is best for removing elements from an active iterating list. However that only removes the current element. Is there any other suggestions? I was thinking a while loop and then a for loop which iterates to match.

For example:

while (!originalPersonList.isEmpty()) {
  ArrayList<Person> newGroup = new ArrayList<Integer>();
  List<Integer> indexToRemove = new ArrayList<Integer>();
  newGroup.add(0);
  indexToRemove.add(0);
  for (int i=1; i < originalPersonList.size(); i++) {
    if (originalPersonList.get(0).getCountry() == originalPersonList.get(i).getCountry()) {
       indexToRemove.add(i);
       newGroup.add(i);
    }
  }
  originalPersonList.removeAll(indexToRemove);
}

This was the direction I was going towards, but I feel there is a more elegant solution out there.

Apologies - I feel I should have highlighted this at the start. I'm restricted to Java 7.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
n1234
  • 155
  • 1
  • 2
  • 9
  • Why don't you just fill a country map while iterating your list? – César Alves Aug 14 '20 at 15:38
  • You can use the stream API for this. Refer to this question (and answer): https://stackoverflow.com/questions/21678430/group-a-list-of-objects-by-an-attribute-java – Abra Aug 14 '20 at 15:39
  • So, the next loop, will be to find a person from Ireland, between Johnny, Vinny, and Tommy? – LoukasPap Aug 14 '20 at 15:44
  • @L.Papadopoulos - yes, essentially I'm wanting to group these people by the same country. The solution I had in mind was looping through each person, try to find if any other person matches that country, and remove them from the list as there is no need to iterate through those people that got matched. The next iteration will go through Johnny, Vinny, and Tommy like you said, according to the example. Then Johnny and Tommy will be matched into another group. The next, and final iteration will have a single person on the list which is Vinny. – n1234 Aug 14 '20 at 15:50
  • So, you just want to find a better (maybe quicker) way to do that? – LoukasPap Aug 14 '20 at 15:53
  • @L.Papadopoulos I think so. I'm just interested if there is a better, or cleaner way of doing this. If I am on the right road, then I'll happily continue on :-) – n1234 Aug 14 '20 at 15:57
  • Sorry, I'm going to have to reask the question. Someone closed this, as they suggested it's a similar question to another one posted before (which it's not). I'll try to give some more details in the new question. :-/ – n1234 Aug 14 '20 at 16:02

0 Answers0