Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 655295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:35:17+00:00 2026-05-13T22:35:17+00:00

Okay at first I thought this would be pretty straightforward. But I can’t think

  • 0

Okay at first I thought this would be pretty straightforward. But I can’t think of an efficient way to solve this. I figured a brute force way to solve this but that’s not very elegant. I have an ArrayList. Contacts is a VO class that has multiple members – name, regions, id. There are duplicates in ArrayList because different regions appear multiple times. The list is sorted by ID. Here is an example:

Entry 0 – Name: John Smith; Region: N; ID: 1
Entry 1 – Name: John Smith; Region: MW; ID: 1
Entry 2 – Name: John Smith; Region: S; ID: 1
Entry 3 – Name: Jane Doe; Region: NULL; ID: 2
Entry 4 – Name: Jack Black; Region: N; ID: 3
Entry 6 – Name: Jack Black; Region: MW; ID: 3
Entry 7 – Name: Joe Don; Region: NE; ID: 4

I want to transform the list to below by combining duplicate regions together for the same ID. Therefore, the final list should have only 4 distinct elements with the regions combined.

So the output should look like this:-

Entry 0 – Name: John Smith; Region: N,MW,S; ID: 1
Entry 1 – Name: Jane Doe; Region: NULL; ID: 2
Entry 2 – Name: Jack Black; Region: N,MW; ID: 3
Entry 3 – Name: Joe Don; Region: NE; ID: 4

What are your thoughts on the optimal way to solve this? I am not looking for actual code but ideas or tips to go about the best way to get it done.

Thanks for your time!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:35:17+00:00Added an answer on May 13, 2026 at 10:35 pm

    You can iterate them while dumping them (and merging duplicates) into a TreeMap. Then create a list from the sorted view of the TreeMap’s values.

    In the sample code I’m assuming you have an Entry class with id, name and regions fields this last one being a List of Region instances. This could easily be changed to a Set, and Region to Strings or whatever you’re using. The sample copies the entries before inserting them into the map since they will be modified when merged to other entries.

    SortedMap<Integer, Entry> mergedEntriesMap = new TreeMap<Integer, Entry>();
    for (Entry e : entries) {
      if (mergedEntriesMap.contains(e.id)) {
        Entry m = mergedEntriesMap.get(e);
        m.regions.addAll(e.regions);
      } else {
        Entry m = new Entry();
        // copy the entry to keep the original array clean
        m.id = e.id;
        m.name = e.name;
        m.regions = new ArrayList<Region>(e.regions);
        mergedEntriesMap.put(m.id, m);
      }
    }
    
    List<Entry> mergedEntries = new ArrayList<Entry>(mergedEntriesMap.values());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I am creating a ticker of sorts, and I had some pretty
Okay, I'm trying to create a simple app that will draw a shape (I'm
First the relevent schema: A is a table of list entries with ListID, Date,
I have no doubt at all that my own idiocy is responsible for this.
I have approached you today to try and gather your opinions and experience with
I need to make a web page with Wicket that lays out the following
I am not very good with sqls and I am trying to understand an
I'm attempting to create a process that verifies data within a database and notifies
I'm using the FETStationPanels from FlashEff2 with Flex 3. Unfortunately, it is garbling some
I have a 'tabbed' style web page where once the page loads each tab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.