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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:06:38+00:00 2026-06-15T08:06:38+00:00

I really need to solve a problem i’ve been stuck into for 3 days.

  • 0

I really need to solve a problem i’ve been stuck into for 3 days. Any help would be appreciated.

I have a Car class with brand, model and color instance variables.
I have a Preferences class with name, brand, model and color instance variables.
I have a preferences (Key = name, value = Car) HashMap that contains people’s names as keys and Car they would like to drive as values.
Finally i have a names ArrayList that contains all the names in the system.

I want somehow to group the names of the people according to the car they would like to drive.
For example i want the output to be something like:

Ford Focus, 2006, Blue : Nick,Bill,Jim
Opel Astra, 2008, Black : Joe, George
Mercedes CLK, 2009, Black : Steve, Chris, John

The first thing i thought of was, that i could hold all the duplicates in a list and then get the names of that list, but i figured out i can’t form groups of people according to their preferred car. So, i think the following piece of code doesn’t help at all.

Preferences[] p = new Preferences[preferences.size()];  

for (int i = 0; i < preferences.size(); i++) {
    p[i] = (Preferences) preferences.get(names.get(i));
    Car car = new Car(p[i].getBrand(), p[i].getModel(), p[i].getColor());
    if (!set.add(car)) { //duplicate
        System.out.println("Duplicate!");
        listOfDuplicates.add(car);
    }
}

Is there anything i can do?

  • 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-06-15T08:06:40+00:00Added an answer on June 15, 2026 at 8:06 am
    Map<Car, List<String>> namesByCar = new HashMap<Car, List<String>>();
    for (Map.Entry<String, Car> entry : preferences) {
        String name = entry.getKey();
        Car car = entry.getValue();
        List<String> names = namesByCar.get(car);
        if (names == null) {
            names = new ArrayList<String>();
            namesByCar.put(car, names);
        }
        names.add(name);
    }
    

    Here you go…

    It would be even easier with Guava’s Multimap.

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

Sidebar

Related Questions

I have been trying to solve this problem for over 14 hours now...I am
I really need help with this last part of my program. I need to
I really need to limit any unnecessary network traffic and server trips. Solution: common
I Really need help in this ... I'm building an application where I need
I really need help this time - Joomla 2.5 and latest K2. I'm using
I really need help on this one. I am having a simple login form
I really need advice on how to do the following. I have tried several
Here is the problem that I need to solve: This is the table that
I have written a PHP application and have a problem that I cannot solve
Possible Duplicate: Why does 'return self' return None? I've been trying to solve Problem

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.