Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set vs List when need both unique elements and access by index

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.

like image 726
Sunny Avatar asked Aug 05 '16 11:08

Sunny


People also ask

When should we use list and when should we use Set?

The main difference between List and Set is that List allows duplicates while Set doesn't allow duplicates. List is an ordered collection it maintains the insertion order, which means upon displaying the list content it will display the elements in the same order in which they got inserted into the list.

Is Set allow duplicate elements & List allow duplicate elements?

List allows duplicates while Set doesn't allow duplicate elements . All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value.

What is the main difference between Set and list?

List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered.

What are the advantages of using Set interface in comparison to list?

List and Set interfaces are one of them that are used to group the object. Both interfaces extend the Collection interface. The main difference between List and Set is that Set is unordered and contains different elements, whereas the list is ordered and can contain the same elements in it.


1 Answers

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 ...
}
like image 84
binoternary Avatar answered Sep 19 '22 19:09

binoternary