ArrayList without repeating
I am using an arraylist in java and I need to add integers over 10 iterations (an integer is obtained randomly from an array of integers named arrint) without repeating:
for (int i =0; i<10; ++i)
array.add(integer);
and then add 20 other integers to the same array for the same integer (arrint) array for 20 iterations without repeating
for (int i =0; i<10; ++i)
array.add(integer);
but repetition is allowed between the first 10 integers and whole numbers.
Thank you
a source to share
Set
rather than List
, prevents duplication. Thus, you can install Set<Integer>
and after filling it add all its elements to the list (using list.addAll(set)
). Then clean Set
and repeat the next 20.
It is not clear from your description what you want if there are duplicates. Do you want to add elements to Set
until it contains 10 just by undoing duplicates? Or do you want to throw an exception if a duplicate is encountered?
a source to share
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;
}
}
a source to share
I would personally use a set as an intermediate for each collection that does not allow for iteration and then just add the whole thing to the list. This is far from the best OMG OPTIMIZED solution, but it is 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();
a source to share
I understand that you want a list (because you need to keep the items in the order they were inserted) and also want the set-prevent repeat functionality.
So you can write your own class (say SetList<E>
) that will subclass ArrayList<E>
and implement the interface as well Set<E>
. For this, you would HashSet<E>
store as an attribute of your class SetList<E>
. Example: private Set<E> set = new HashSet<E>();
Then just sync the inserts and deletes with the set in the attribute, and if the set already contains the inserted element, don't insert it. The variable method add(E element)
will look like this:
public void add(E element){
if(set.contains(element)){
return;
}
set.add(element);
super.add(element);
}
For other methods, it would be similar.
I did it myself a while ago, but then I found a better solution: GlazedLists . It's a free library that does all of this and more. I suggest you use UniqueList .
a source to share
The Apache Java Collections project usually has code to do most of the common tasks that Java has forgotten about. This is one of them:
org.apache.commons.collections.list.SetUniqueList in the Commons-Collection .
a source to share