Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an object to a java structure only if it isn't already there

Tags:

java

arraylist

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?

like image 633
user2076476 Avatar asked Sep 10 '13 19:09

user2076476


People also ask

What is the difference between ADD and addAll in list in Java?

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.

What is .ADD Java?

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).

How does contains work in Java for object?

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.


2 Answers

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

like image 100
Kon Avatar answered Oct 19 '22 23:10

Kon


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.

like image 41
nanofarad Avatar answered Oct 20 '22 00:10

nanofarad