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

  • SEARCH
  • Home
  • 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 6365113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:15:07+00:00 2026-05-25T00:15:07+00:00

I saw an earlier post which is trying to do a similar sort of

  • 0

I saw an earlier post which is trying to do a similar sort of thing in Python.

Here is a boiled down example of what I want. Let us say I have List.

public class MyObject {
    private String purchase;
    private Double price;
}

Let us say that a typical List<MyObject> will hold:

Bike 95.00
Clothes 24.99
Clothes 10.76
Food 6.35
Food 91.46

I want all of the items with same purchase value to be combined into a single item with the price summed for that item. For example, clothes would be a single item with price 35.75 (if I have done the addition correctly).

The way I thought about doing it was:

  1. Collections.sort the list by purchase for O(n log n)
  2. Walk the sorted list (it is an ArrayList I am using) as same items will be consecutive and perform a merge on 2 items at a time O(n)

Giving a total runtime of O(n log n).

Now that sounds reasonable to me, however is there a library out there which at the very least beats the pants of my constants? I always support going with a streamlined version if one exists. So are there any existing implementations that I should think about using or improvements over my algorithm?

EDIT

After thinking about my boiled down case as I went home yesterday, yes I easily saw it is a map. All I had to do is reduce it to the simpler problem I posted and it became very obvious. My real structure is

public class MyObject {
   Map bucketOfStuff;
}

In reality, bucketOfStuff is really Map<String, Object> where sometimes the value is a String and sometimes the value is a Double (it can also be an Integer sometimes, but hey I can treat it as double). For all of the objects which are of type String, they would be used to form the key in this problem. So If I had

  • color => Red
  • size => Small
  • texture => Smooth

Then I could encode all into a single String such as Red,Small,Smooth because I know comma will not be a character present in any of the values so I can use it as a delimiter.

For the value for our hypothetical new Map, it would be List because I have to perform (mathematical) vector addition on all of the bucketOfStuff values which are doubles. So the proposed new Map would either be Map<List<String>, List<Double>> or simply Map<String, List<Double>> if I used the delimiter as above.

Another thing which corrupted my thought process is that in the end the collection has to be a List to pass on so I was in a narrow-minded way thinking List all the way. So I have to be able to reconstruct the original object which is a bit involved, but not impossible. Thanks all for the help and nice catch.

EDIT

I have to modify my description slightly because I just recalled that I should maintain the original ordering of the List<MyObject>, therefore my original solution would have been incorrect anyway since I was doing the sort. Because of this, I shall continue to follow the path of assistance offered and use a LinkedHashMap<String, List<Double>>. Coming from Java 6 API “This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order)”.

  • 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-25T00:15:08+00:00Added an answer on May 25, 2026 at 12:15 am

    The question you linked seems to be more interested in fuzzy matching (two strings that are “close enough”) to one another, but from your question it seems like you’re just interested in combining terms that are identical.

    If that’s the case, you can do this in O(n) time by using a standard HashMap. In particular, your algorithm could work like this:

    1. Construct a HashMap from String to Double.
    2. For each element in the list:
      1. If the named object doesn’t exist in the HashMap, insert it with value equal to the price.
      2. If the named object does exist in the HashMap, update the value by adding in the value associated with the current object.
    3. Iterate across the HashMap and for each element, create a new MyObject from the key/value pair.

    Step 1 takes O(1) time. Step 2 takes O(n) time, since you do only constant work for each element. Finally, step 3 takes O(n) time again because you have to visit every element in the HashMap exactly once. Overall this is O(n), which is asymptotically faster than your O(n log n) sorting-based solution. I also think this is much easier to code up, since you don’t need to define a Comparator for your type and can just use standard, off-the-shelf Java components.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw in an earlier post here on stackoverflow a example on many to
I saw some websites that (for example): if you want to view your message
I saw a similar question being posted here, yet it did not help me
I saw this post here , he explains well howto show a highlighted result,
From an earlier post about trying to improve my sites performance I have been
I saw a piece of code earlier this week (which, unfortunately, I am unable
I saw a post earlier today and have been playing around with: http://arashkarimzadeh.com/index.php/jquery/7-editable-jquery-plugin.html What
I saw the docs on the Instagram developers page. http://instagram.com/developer/endpoints/ But I want to
I saw some code when studying the open source project: here . But I
i saw few android apps got the interface similar with itune(as attached), user can

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.