title: Grouping elements by object attribute
body: <p>I really need to solve a problem i've been stuck into for 3 days.  Any help would be appreciated.</p>
  
  <p>I have a <code>Car</code> class with <strong>brand</strong>, <strong>model</strong> and <strong>color</strong> instance variables.<br>
  I have a <code>Preferences</code> class with <strong>name</strong>, <strong>brand</strong>, <strong>model</strong> and <strong>color</strong> instance variables.<br>
  I have a <code>preferences</code> (Key = name, value = Car) HashMap  that contains people's names as keys and Car they would like to drive as values.<br>
  Finally i have a <code>names</code> ArrayList that contains all the names in the system.  </p>
  
  <p>I want somehow to group the names of the people according to the car they would like to drive.<br>
  For example i want the output to be something like:  </p>
  
  <p>Ford Focus, 2006, Blue : Nick,Bill,Jim<br>
  Opel Astra, 2008, Black : Joe, George<br>
  Mercedes CLK, 2009, Black : Steve, Chris, John</p>
  
  <p>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.</p>
  
  <pre><code>Preferences[] p = new Preferences[preferences.size()];  
  
  for (int i = 0; i &lt; 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);
      }
  }
  </code></pre>
  
  <p>Is there anything i can do?</p>