19

I need to keep a unique list of elements seen and I also need to pick random one from them from time to time. There are two simple ways for me to do this.

  1. Keep elements seen in a Set - that gives me uniqueness of elements. When there is a need to pick random one, do the following:

    elementsSeen.toArray()[random.nextInt(elementsSeen.size())]
    
  2. Keep elements seen in a List - this way no need to convert to array as there is the get() function for when I need to ask for a random one. But here I would need to do this when adding.

    if (elementsSeen.indexOf(element)==-1) {elementsSeen.add(element);}
    

So my question is which way would be more efficient? Is converting to array more consuming or is indexOf worse? What if attempting to add an element is done 10 or 100 or 1000 times more often?

I am interested in how to combine functionality of a list (access by index) with that of a set (unique adding) in the most performance effective way.

5
  • 2
    why don't you use both? No need to create extra objects as both Collections can point to the same. Aug 5, 2016 at 18:28
  • Are you going to be looking them up a lot? Then use a List. Are you going to be inserting a lot? Then use a Set. I definitely don't like option 1 but also, I'm not entirely sure you need to use it. Sets generally return stuff in non-deterministic order anyway, so you could just take any element. set.iterator().next(); or keep a reference of the iterator and call .next() when you want a random element. The latter should be random, though you won't get repeats. Alternatively int i = random.nextInt(set.size()); for (Object o : set) { if (i-- == 0) return o;) - less memory than the array.
    – VLAZ
    Aug 6, 2016 at 1:21
  • Possible duplicate of Picking a random element from a set
    – Raedwald
    Aug 9, 2016 at 7:09
  • So, you do not really need a List at all. You only think you need a List because you want a random selection.
    – Raedwald
    Aug 9, 2016 at 7:13
  • My question isn't really about getting a random from a set, but about how to combine characteristics of set and list in an efficient way. Random is more of a figurative thing here, what I really wanted from the list side was access by index.
    – Sunny
    Aug 9, 2016 at 7:58

6 Answers 6

25

If using more memory is not a problem then you can get the best of both by using both list and set inside a wrapper:

public class MyContainer<T> {
    private final Set<T> set = new HashSet<>();
    private final List<T> list = new ArrayList<>();

    public void add(T e) {
        if (set.add(e)) {
            list.add(e);
        }
    }

    public T getRandomElement() {
        return list.get(ThreadLocalRandom.current().nextInt(list.size()));
    }
    // other methods as needed ...
}
2
  • If you only need these methods, only have these methods.
    – Caleth
    Aug 5, 2016 at 14:23
  • 1
    this solution won't necessarily increase memory usage by much, as Ts won't be copied all over the place Aug 5, 2016 at 18:29
11

HashSet and TreeSet both extend AbstractCollection, which includes the toArray() implementation as shown below:

public Object[] toArray() {
    // Estimate size of array; be prepared to see more or fewer elements
    Object[] r = new Object[size()];
    Iterator<E> it = iterator();
    for (int i = 0; i < r.length; i++) {
        if (! it.hasNext()) // fewer elements than expected
            return Arrays.copyOf(r, i);
        r[i] = it.next();
    }
    return it.hasNext() ? finishToArray(r, it) : r;
}

As you can see, its responsible for allocating the space for an array, as well as creating an Iterator object for copying. So, for a Set, adding is O(1), but retrieving a random element will be O(N) because of the element copy operation.

A List, on the other hand, allows you quick access to a specific index in the backing array, but doesn't guarantee uniqueness. You would have to re-implement the add, remove and associated methods to guarantee uniqueness on insert. Adding a unique element will be O(N), but retrieval will be O(1).

So, it really depends on which area is your potential high usage point. Are the add/remove methods going to be heavily used, with random access used sparingly? Or is this going to be a container for which retrieval is most important, since few elements will be added or removed over the lifetime of the program?

If the former, I'd suggest using the Set with toArray(). If the latter, it may be beneficial for you to implement a unique List to take advantage to the fast retrieval. The significant downside is add contains many edge cases for which the standard Java library takes great care to work with in an efficient manner. Will your implementation be up to the same standards?

4
  • 1
    Since there's no replacement, only extension, could he not just inherit from List and preappend the uniqueness check before a super call? Aug 5, 2016 at 16:10
  • 2
    @IsaacvanBakel that breaks the List contract and just gets utterly messy. If you're going that direction, don't extend List but just create a class that has a List and does a uniqueness check before adding an element to the inner List. Aug 5, 2016 at 19:54
  • @LouisWasserman i find it much easier to shadow an inner List decorated with my own implementation that way then to try to reimplement the entire class Aug 5, 2016 at 22:50
  • 1
    I'm not suggesting reimplementing the class, I'm suggesting a class that has-a List and only exposes the operations actually needed for your use case. Aug 5, 2016 at 22:51
3

Write some test code and put in some realistic values for your use case. Neither of the methods are so complex that it's not worth the effort, if performance is a real issue for you.

I tried that quickly, based on the exact two methods you described, and it appears that the Set implementation will be quicker if you are adding considerably more than you are retrieving, due to the slowness of the indexOf method. But I really recommend that you do the tests yourself - you're the only person who knows what the details are likely to be.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class SetVsListTest<E> {
    private static Random random = new Random();
    private Set<E> elementSet;
    private List<E> elementList;

    public SetVsListTest() {
        elementSet = new HashSet<>();
        elementList = new ArrayList<>();
    }

    private void listAdd(E element) {
        if (elementList.indexOf(element) == -1) {
            elementList.add(element);
        }
    }

    private void setAdd(E element) {
        elementSet.add(element);
    }

    private E listGetRandom() {
        return elementList.get(random.nextInt(elementList.size()));
    }

    @SuppressWarnings("unchecked")
    private E setGetRandom() {
        return (E) elementSet.toArray()[random.nextInt(elementSet.size())];
    }

    public static void main(String[] args) {
        SetVsListTest<Integer> test;
        List<Integer> testData = new ArrayList<>();
        int testDataSize = 100_000;
        int[] addToRetrieveRatios = new int[] { 10, 100, 1000, 10000 };

        for (int i = 0; i < testDataSize; i++) {
            /*
             * Add 1/5 of the total possible number of elements so that we will
             * have (on average) 5 duplicates of each number. Adjust this to
             * whatever is most realistic
             */
            testData.add(random.nextInt(testDataSize / 5));
        }

        for (int addToRetrieveRatio : addToRetrieveRatios) {
            /*
             * Test the list method
             */
            test = new SetVsListTest<>();
            long t1 = System.nanoTime();
            for(int i=0;i<testDataSize; i++) {
                // Use == 1 here because we don't want to get from an empty collection
                if(i%addToRetrieveRatio == 1) {
                    test.listGetRandom();
                } else {
                    test.listAdd(testData.get(i));
                }
            }
            long t2 = System.nanoTime();
            System.out.println(((t2-t1)/1000000L)+" ms for list method with add/retrieve ratio "+addToRetrieveRatio);

            /*
             * Test the set method
             */
            test = new SetVsListTest<>();
            t1 = System.nanoTime();
            for(int i=0;i<testDataSize; i++) {
                // Use == 1 here because we don't want to get from an empty collection
                if(i%addToRetrieveRatio == 1) {
                    test.setGetRandom();
                } else {
                    test.setAdd(testData.get(i));
                }
            }
            t2 = System.nanoTime();
            System.out.println(((t2-t1)/1000000L)+" ms for set method with add/retrieve ratio "+addToRetrieveRatio);
        }
    }
}

Output on my machine was:

819 ms for list method with add/retrieve ratio 10
1204 ms for set method with add/retrieve ratio 10
1547 ms for list method with add/retrieve ratio 100
133 ms for set method with add/retrieve ratio 100
1571 ms for list method with add/retrieve ratio 1000
23 ms for set method with add/retrieve ratio 1000
1542 ms for list method with add/retrieve ratio 10000
5 ms for set method with add/retrieve ratio 10000
2

You could extend HashSet and track the changes to it, maintaining a current array of all entries.

Here I keep a copy of the array and adjust it every time the set changes. For a more robust (but more costly) solution you could use toArray in your pick method.

class PickableSet<T> extends HashSet<T> {
    private T[] asArray = (T[]) this.toArray();

    private void dirty() {
        asArray = (T[]) this.toArray();
    }

    public T pick(int which) {
        return asArray[which];
    }

    @Override
    public boolean add(T t) {
        boolean added = super.add(t);
        dirty();
        return added;
    }

    @Override
    public boolean remove(Object o) {
        boolean removed = super.remove(o);
        dirty();
        return removed;
    }
}

Note that this will not recognise changes to the set if removed by an Iterator - you will need to handle that some other way.

3
  • 2
    What you are doing here though is converting to array on each add. This looks to me way way worse than just converting to array on occasion of asking for the random. Even assuming that retrieving is done as often as adding, this offers no improvement to just using regular HashSet and converting on retrieval, in fact it just requires more memory. Am I missing something?
    – Sunny
    Aug 5, 2016 at 12:39
  • @Sunny - Currently it favours rare adding but common pick. The dirty method could be enhanced to just set a dirty flag which is then checked in pick, this would then favour common add and rare pick. Primary aim here was to point out how easy it is to add functionality to current collections via subclassing. Aug 5, 2016 at 13:11
  • I see, if done with a flag it would be exactly the first version, calling toArray() on retrieval, with the added memory usage for always keeping the list in the memory, I'd say there is no point in keeping it when you are recreating it anyway every time you need to use it. I do get your point however, everything does look better wrapped.
    – Sunny
    Aug 5, 2016 at 13:55
1

So my question is which way would be more efficient?

Quite a difficult question to answer depending on what one does more, insert or select at random?

We need to look at the Big O for each of the operations. In this case (best cases):

  • Set: Insert O(1)
  • Set: toArray O(n) (I'd assume)
  • Array: Access O(1)

vs

  • List: Contains O(n)
  • List: Insert O(1)
  • List: Access O(1)

So:

  • Set: Insert: O(1), Access O(n)
  • List: Insert: O(n), Access O(1)

So in the best case they are much of a muchness with Set winning if you insert more than you select, and List if the reverse is true.

Now the evil answer - Select one (the one that best represents the problem (so Set IMO)), wrap it well and run with it. If it is too slow then deal with it later, and when you do deal with it, look at the problem space. Does your data change often? No, cache the array.

0

It depends what you value more.

List implementations in Java normally makes use of an array or a linked list. That means inserting and searching for an index is fast, but searching for a specific element will require looping thought the list and comparing each element until the element is found.

Set implementations in Java mainly makes use of an array, the hashCode method and the equals method. So a set is more taxing when you want to insert, but trumps list when it comes to looking for an element. As a set doesn't guarantee the order of the elements in the structure, you will not be able to get an element by index. You can use an ordered set, but this brings with it latency on the insert due to the sort.

If you are going to be working with indexes directly, then you may have to use a List because the order that element will be placed into Set.toArray() changes as you add elements to the Set.

Hope this helps :)

2
  • 2
    @Seelenvirtuose I am aware that List and Set are both interfaces, I have run many comparisons between their implementations. Yes inserting into a HashSet can be done in O(1) but not always due to the way that the array needs to be adapted to accommodate a full structure. Finally searching for an index (ie array[1]) cannot be done in Set implementations Aug 5, 2016 at 11:59
  • Sad how so many people on SO seems to really appreciate adding a useless extra sentence at the end of their criticism such as "This is a very poor answer". @Seelenvirtuose Aug 5, 2016 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.