Add object to java structure only if it doesn't exist yet

I have an array that I want to add if the value is not already stored in it. For example, if my array looks like this:

H LK KL LS 

And I have LS value, it will not be added. But if the value was A, that would be. I don't care about ordering or sorting. Just there or not information.

I thought 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 must be an easier way to do this. Does anyone know a shorter way to do this?

+4
source share
5 answers

I would consider using Set instead of ArrayList. Sets are defined by their requirement that all objects are unique. A HashSet is a good default choice and may be a more proper data structure for you in this case, but it is ultimately your challenge.

When you add to a set, the add method returns false if the object is already contained in the set and a duplicate is not added.

If you need a predictable iteration order, you can use LinkedHashSet.

This implementation differs from HashSet in that it maintains a double-linked list that goes through all its entries. This linked list defines the iteration order, which is the order in which elements were inserted into the set (insert order)

Finally, you might consider using a navigation set in a TreeSet. It doesn't seem that sorting is important based on the information above, so this is probably not the right choice for you, but it is good information for your back pocket.

Just make sure that if you add your own class objects to Set, which you override and override correctly, the equals () method. This will not only match links and will lead to unexpected behavior and a noticeable headache. Use the @Override annotation to make sure you override correctly, but I'm distracted.

Hashset javadocs

TreeSet Javadocs

LinkedHashSet Javadocs

+6
source

Use ArrayList#contains instead:

 if (!arraylist.contains(value)){ arraylist.add(value); } 

Note that contains depends on your objects having the equals() method. Namely, either the list contains null and value is null, or value.equals(o) for some o , which is an element of the list.

+3
source

One clean way to do this is if you don't mind that a performance hit due to copying and organizing is not important:

Use a HashSet , paste everything into it and copy its contents into the List . Example:

 Set<String> set = new HashSet<String>(); set.add("H"); set.add("LK"); set.add("KL"); set.add("LS"); set.add("LS"); set.add("A"); List<String> list = new ArrayList<String>(); list.addAll(set); System.out.println(list); 

Output:

 [A, KL, LK, LS, H] 
+3
source

If you need List semantics (i.e. the elements are ordered), you can subclass ArrayList (you would call it UniqueArrayList or something else) and override its add method to call contains and add the element only if it does not exist.

 public class UniqueArrayList<E> extends ArrayList<E> { @Override public boolean add(E e) { if (!contains(e)) { return super.add(e); } else { return false; } } // TODO: override addAll etc. } 

This would be the most object-oriented way compared to having if(!list.contains(... checks (or even loops) wherever needed.

It will also allow you to preserve the existing functionality of List objects, for example, access elements by index via get , if this is a requirement.

If the concept of order is not important, you can use Set . Installations, both by definition and implementation, contain only unique elements.

+1
source

You probably have the wrong data structure. In this case, the set will be better.

0
source

Source: https://habr.com/ru/post/1501547/


All Articles