0

I'm writing a piece of code which takes a great deal of objects and adds them to another array. The catch is, I don't want any duplicates. Is there a way I could implement a Hashset to solve this problem?

    public static Statistic[] combineStatistics(Statistic[] rptData, Statistic[] dbsData) {
    HashSet<Statistic> set = new HashSet<Statistic>();
    for (int i=0; i<rptData.length; i++) {
        set.add(rptData[i]);
    }
    /*If there's no data in the database, we don't have anything to add to the new array*/
    if (dbsData!=null) {
        for (int j=0; j<dbsData.length;j++) {
            set.add(dbsData[j]);
        }
    }
    Statistic[] total=set.toArray(new Statistic[0]);
    for (int workDummy=0; workDummy<total.length; workDummy++) {
        System.out.println(total[workDummy].serialName);
    }

    return total;
}//end combineStatistics()
eggHunter
  • 279
  • 2
  • 4
  • 14

3 Answers3

2

Properly implement equals(Object obj) and hashCode() on YourObject if you expect value equality instead of reference equality.

Set<YourObject> set = new HashSet<YourObject>(yourCollection);

or

Set<YourObject> set = new HashSet<YourObject>();
set.add(...);

then

YourObject[] array = set.toArray(new YourObject[0])
Zong
  • 5,701
  • 5
  • 27
  • 46
  • This method seems promising but it doesn't seem to work. I've posted my code (see above). – eggHunter Dec 06 '13 at 19:22
  • @eggHunter As stated, if you expect value equality your `Statistic` class will need to implement `equals` and `hashCode` properly. See [Overriding equals and hashCode in Java](http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java) – Zong Dec 06 '13 at 19:25
  • I'm not familiar with this, I'll look into it. Thank you. – eggHunter Dec 06 '13 at 19:26
1

I think you should pay attention to:

1 - what to do if there is a duplicate in the original Collection? Use the first added to the array? Use the other(s)?

2 - You definitely need to implement equals and hashcode so that you can tell what are duplicate objects

3 - Are you going to create a fixed size array and then won't add anymore objects? Or are you going to keep adding stuff?

You can use any kind of Set actually, but if you use LinkedHashSet, then you will have a defined iteration order (which looks like an array). HashSet wont't garantee any order and TreeSet will try to order data ascending.

prmottajr
  • 1,804
  • 1
  • 12
  • 22
  • 1. Yes, I'd use the object in the first array. 2. I'm not familiar with this but I'm doing my research. 3.The array I'm returning will be fixed. – eggHunter Dec 06 '13 at 19:38
0

Depends on what you are referring to as a duplicate. If you mean an identical object, then you could use a List and simply see if the List contains the object prior to adding it to the list.

Object obj = new Object();
List<Object> list = new ArrayList<Object>();

if (!list.contains(obj)) {
    list.add(obj);
}
Joe
  • 539
  • 1
  • 6
  • 14
  • 1
    This is O(n^2) and will be really bad for large collections. – Zong Dec 06 '13 at 19:08
  • Correct, for large collections. Although, if he is only working with a small collection, hashing could result in being slower. What would be better for him, he would have to just measure it out. Either way, I prefer your solution and will give you an up vote for it. – Joe Dec 06 '13 at 19:18
  • he directly states that he has a "great deal of objects" in his question – turbo Dec 06 '13 at 19:26
  • There are literally thousands of objects I'll be working with. – eggHunter Dec 06 '13 at 19:34