I have an arrayList that I want to add to, as long as the value isn't already stored in it. For example if my array is like this:
H
LK
KL
LS
And I have the value LS, it wouldn't be added. But if the value was A, it would be. I don't care about order or sorting. Just whether or not the information is in there.
I was thinking that the code should look something like this:
value = A;
no = 0;
for (int o; arraylist.length; o++)
if (arraylist.get(o).equals(value)){
continue;
}else{
no++;
}
}
if (arraylist.length = no){
arraylist.add(value);
}
But there has to be an easier way to do it. Does anyone know a more concise way to do this?
add is used when you need to add single element into collection. addAll is used when you want to add all elements from source collection to your collection.
Description. The java. util. ArrayList. add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
contains() in Java. ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.
I would consider using a Set
instead of an ArrayList. Sets are defined by their requirement that all objects be unique. A HashSet
is a good default choice and might be the more correct data structure for you to use in this case, but that is ultimately your call.
When you add to a set, the add
method will return false
if the object is already contained in the set, and a duplicate will not be added.
If you need predictable iteration order, you could use the LinkedHashSet.
This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)
Finally, you could consider using a navigable set in TreeSet. It doesn't look sorting is important based on the information above, so it's probably not the right choice for you, but it's good information to have in your back pocket.
Just make sure that if you are adding your own class's objects to a Set, that you override, and override properly, the equals() method. Not doing so will cause only references to be compared and will lead to unexpected behavior, and a notable headache. Use the @Override annotation to make sure you're overriding properly, but I digress.
HashSet Javadocs
TreeSet Javadocs
LinkedHashSet Javadocs
Use ArrayList#contains
instead:
if (!arraylist.contains(value)){
arraylist.add(value);
}
Note that contains
depends on your objects having an equals()
method. Namely, either the list contains null and value
is null, or value.equals(o)
for some o
that is an element of the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With