Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent duplicate entries in arraylist

Say I create some object class like so

public class thing {
        private String name; 
        private Integer num;

        public oDetails (String a, Integer b) {
            name = a;
            num = b;
        }
...gets/ sets/ etc

Now I want to create an arraylist to hold a number of this object class like so.

ArrayList<thing> myList = new ArrayList<thing>;
thing first = new thing("Star Wars", 3);
thing second = new thing("Star Wars", 1);
myList.add(first);
myList.add(second);

I would like to include some sort of logic so that in this case...when we try and add object "second" rather than add a new object to the arrayList, we add second.getNum() to first.getNum(). So if you were to iterate through the ArrayList it would be

"Star Wars", 4

I am having trouble coming up with an elegant way of handling this. And as the arraylist grows, searching through it to determine if there are duplicate name items becomes cumbersome. Can anyone provide some guidance on this?

like image 503
tyh Avatar asked Apr 01 '12 05:04

tyh


3 Answers

You would have to create your own method to check to see if the the name field of class Thing was set to "Star Wars" then add to the corresponding num field of Class Thing, that is one possible solution.

Another solution is to use a Map with the name field as the key, and the num field as the value.

ex:

public class Thing
{
   private String name;
   private int    num;

   public Thing(String name, int num)
   {
       this.name = name;
       this.num  = num;
   } 
}

public class ThingMap
{
    Map<String, Integer> thingMap; 

    public ThingMap()
    {
       this.thingMap = new HashMap<>();
    }

    public void put(Thing t)
    {
       String  k = t.getName();
       Integer v = t.getNum();

       if(thingMap.get(k) == null) //no entry exists
       {
          thingMap.put(k, v);
       }
       else //entry exists
       {
          //add to the current value
          thingMap.put(k, thingMap.get(k) + v);
       }
    }

    public Integer get(String k)
    {
       return this.thingMap.get(k);
    }
}

public class TestThing
{
   public static void main(String[] args)
   {
      ThingMap tMap = new ThingMap();
      Thing a = new Thing("Star Wars", 3);
      Thing b = new Thing("Star Wars", 1);

      tMap.put(a);
      tMap.put(b);

      System.out.println("Current value: " + tMap.get(a.getName());
   }

}

Hope this helps.

like image 76
Hunter McMillen Avatar answered Oct 28 '22 10:10

Hunter McMillen


If you want to have a set of unique objects, use Set instead of List.

Also, if you want to define by yourself when objects are considered equal, consider overriding the equals and hashCode methods of the class.

like image 26
MByD Avatar answered Oct 28 '22 11:10

MByD


IMHO, it makes more sense to use a Map<String, Integer> instead of the ArrayList, or a Map<String, Thing> if you don't want to change your class.

like image 43
Vincent Cantin Avatar answered Oct 28 '22 11:10

Vincent Cantin