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 7666899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:57:18+00:00 2026-05-31T14:57:18+00:00

I have the following scenario: class Task { int id; Group group; User user;

  • 0

I have the following scenario:

class Task {
    int id;
    Group group;
    User user;
    boolean successful;
}

User is part of Group, and Group-User relationship is many-to-many (user may belong to multiple groups and a group may contain multiple users). Task is specific to a User in a Group.

There is a List<Task> for which I need to summarize the number of successful tasks per User in a Group and send update to the user. So, if a user belongs to several groups I need to update him once for each group he belongs to (with the number of successful tasks the user had for that group).

What would be the best way to achieve that? Our current algorithm is:

First, sort the list by Group ID and then by User ID.
Then: 

int successfulTasks = 0;
Group curGroup = null;
User curUser = null;
for(Task task : tasksByGroupAndUser) {
    if((task.getGroup() != curGroup) || (task.getUser() != curUser) {
        // Going to next user or group, update the previous user
        updateUser(user,group, successfulTasks);
        successfulTasks = 0;        
    }

    if(task.isSuccessful()) {
        successfulTasks++;
    }
}

// Handle last user  
if(curUser != null) {
    updateUser(user,group, successfulTasks); 
}

Is there a better (cleaner) way to do this? The above seems somewhat error-prone, especially that last user check.

  • 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-31T14:57:19+00:00Added an answer on May 31, 2026 at 2:57 pm

    You can create a Pair class which will have User and Group as final fields. Make sure Pair overrides hashCode() and equals().

    Create a HashMap<Pair,Integer>, and maintain it [as a histogram] while iterating the list. [Need only one pass on the list for that].

    Later, iterate the histogram, and send an update for every Pair [which is actually a tuple: (User,Group)] with the key – containing succesful runs.

    Should look something like: [Pseudo-code, there might be some syntatic errors…]:

    Map<Pair,Integer> histogram = new HashMap<Pair,Integer>();
    for(Task task : tasksByGroupAndUser) {
       if (task.isSuccessful() == false) continue; //just skip unseccessful tasks.
       Pair current = new Pair(task.getUser(),task.getGroup());
       Integer value = histogram.get(current);
       histogram.put(current, value == null? 1 : value + 1); //update the histogram
    }
    for (Entry<Pair,Integer> entry : histogram.entrySet()) { 
       updateUser(entry.getKey().getUser(),entry.getKey().getGroup(),entry.getValue());
    }
    

    This solution is asymptotically faster then the suggested solution since it is not requires sorting – so the total runtime is O(n). [while the algorithm suggested in the question is O(nlogn)].

    Also: I find this solution easier to understand, but that might be a matter of opinion…

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

Sidebar

Related Questions

I have the following scenario: class Addition{ public Addition(int a){ a=5; } public static
I have the following scenario in nHibernate: <class name=TestApp.Components.User,TestApp.Components table=Users> <id name=Id column=UserId type=Int32
So I have the following scenario: I have a class part of a view
I have the following scenario: public class Controller { private ModelRepository repository; public int
I have the following scenario: sealed abstract class Type(val inUse: Boolean) case class IntTy(override
I have the following scenario, class foo { ... private: char *_test; }; void
I have the following scenario: class my_base { ... } class my_derived : public
I have the following scenario: public class A { } public class B extends
I have following class hierarchy scenario; Class A has a method and Class B
I have this following scenario namespace A { //... class A { public: static

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.