views:

260

answers:

5

Hi

I'm using arraylist in java and I need to add integers during 10 iterations (integer is got randomly from an array of integers named arrint) without any repetition:

for (int i =0; i<10; ++i)
    array.add(integer);

and then add in the same array 20 other integers for the same array of integer(arrint) during 20 iteration without repetition

for (int i =0; i<10; ++i)
    array.add(integer);

but repetition is permitted between the 10 first integers and the 20 integers.

thank you

+8  A: 

Set, rather than List, prevents duplicates. So you could establish a Set<Integer> and after populating it, add all of its elements to the List (with list.addAll(set)). Then clear the Set and repeat for the next 20.

It's not clear from your description what you want to happen if duplicates are encountered. Do you want to add items into the Set until it contains 10, just discarding duplicates? Or do you want to throw an exception if a duplicate is encountered?

Carl Manaster
+1, beat me to it, and with a better answer than I was writing to boot.
BlairHippo
Answered this below, but Apache Commons Collections has code that seems to do just this.
Dean J
+6  A: 
public class Foo {
  private final Random random = new Random();

  public List<Integer> createList() {
    // Create empty list to store results.
    List<Integer> ret = new ArrayList<Integer>(30);

    // Add 10 randomly generated integers.
    ret.addAll(createRandomIntegers(10));

    // Add another 20 randomly generated integers which could potentially
    // contain integers already added previously (the OP states that this is ok).
    ret.addAll(createRandomIntegers(20));

    return ret;
  }

  /**
   * Utility function that creates a set of randomly generated
   * integers of specified size.  We use a Set to avoid duplicates.
   */
  protected Set<Integer> createRandomIntegers(int sz) {
    Set<Integer> ret = new HashSet<Integer>();

    while (ret.size() < sz) {
      ret.add(random.nextInt());
    }

    return ret;
  }
}
Adamski
+1  A: 

I would personally use a set as an intermediate for each collection that does not allow repetition and then simply add it all to the list. It's far from the most 'OMG OPTIMIZED' solution, but it's very clear to future readers what it does.

    List<Integer> list = new ArrayList<Integer>();

    Set<Integer> subSet = new HashSet<Integer>();

    for (int i =0; i<10; ++i) {
         subSet.add(integers10[i]);
    }
    list.addAll(subSet);
    subSet.clear();

    for (int i =0; i<20; ++i) {
         subSet.add(integers20[i]);
    }
    list.addAll(subSet);
    subSet.clear();
Affe
58 seconds too slow!
Affe
+1  A: 

I understand it that you need to have a List (because you need to keep elements in order in which they were inserted) and also wants a functionality of set - prevent repetition.

So you can write your own class (say SetList<E>) that would subclass ArrayList<E> and implement also the Set<E> interface. For that you would keep HashSet<E> as an atribute of your class SetList<E>. Like this: private Set<E> set = new HashSet<E>(); Then just synchronise inserts and removals with the set in atribute and if the set already contains inserted element don't insert it. The overriding method add(E element) would be like:

public void add(E element){
    if(set.contains(element)){
        return;
    }
    set.add(element);
    super.add(element);
}

For other methods it would be similar.

I have done this some time ago myself but then I found better solution: GlazedLists. It is free library that does this all and much more. I suggest you to use UniqueList.

drasto
A: 

Apache's Java Collections project usually has code to do most of the common tasks that the Java language forgot about. This is one of them:

org.apache.commons.collections.list.SetUniqueList in Commons-Collections.

Dean J