How to save a unique list in Java?

How to create a list of unique / distinct objects (no duplicates) in Java?

Right now I am using HashMap<String, Integer> to do this, since the key is overwritten, and therefore, in the end we can get HashMap.getKeySet() which will be unique. But I'm sure there should be a better way to do this, as part of the cost is lost here.

+84
java list distinct-values
Nov 06
source share
8 answers

You can use the Set implementation:

Information from JAVADoc:

A collection containing non-repeating elements . More formally, the sets do not contain a pair of elements e1 and e2 such that e1.equals (e2) and at most one zero element. As can be seen from his name, this interface models a mathematical abstract abstraction.

Note. Great care should be taken if mutable objects are used as specified elements. The behavior of the set is not indicated if the value of the object changes in a way that affects equal comparisons when the object is an element in the set. A special case of this prohibition is that it is unacceptable for the collection to contain itself as an element.

This is the implementation:

  • Hashset

    This class offers consistent time performance for basic operations (add, delete, presence and size), assuming that the hash function correctly distributes elements among buckets. Iterating over this set takes time proportional to the sum of the size of the HashSet instance (number of elements) plus the "capacity" of the swap HashMap instance (number of buckets). Thus, it is very important not to set the initial power too high (or the load factor too low) if iterative performance is important.

    When repeating a HashSet order of the assigned elements is undefined.

  • LinkedHashSet

    A hash table and associated list of the Set interface, with a predictable iteration order. This implementation differs from HashSet in that it supports a doubly linked list that goes through all its entries. This linked list defines the iteration order, which is the order in which the elements were inserted into the set (insertion order). Note that the insertion order does not change if an item is reinserted into the set. (The element e is reinserted into set s if s.add (e) is called when s.contains (e) returns true immediately before the call.)

    So, the code output above ...

      Set<Integer> linkedHashSet = new LinkedHashSet<>(); linkedHashSet.add(3); linkedHashSet.add(1); linkedHashSet.add(2); for (int i : linkedHashSet) { System.out.println(i); } 

    ... necessarily will

     3 1 2 
  • Treeset

    This implementation provides a guaranteed log (n) time value for basic operations (add, delete and contain). By default, items returned in iterations are sorted by their " natural order , so the code above ...

      Set<Integer> treeSet = new TreeSet<>(); treeSet.add(3); treeSet.add(1); treeSet.add(2); for (int i : treeSet) { System.out.println(i); } 

    ... will output this:

     1 2 3 

    (You can also pass a Comparator instance to the TreeSet constructor to sort the elements in a different order.)

    Note that the order supported by the set (whether it be an explicit comparator) must be consistent with equals if it implements the Set interface correctly. (See Comparable or Comparator for an exact definition of matching with equals.) This is because the Set interface is defined in terms of the equals operation, but the TreeSet instance does all the element comparisons using the compareTo (or comparison) method, so there are two elements that do this methods are considered equal, equal, in terms of set. The behavior of the set is well defined, even if its ordering does not match the equal; it simply does not obey the general contract of the Set interface.

+136
Nov 06
source share

I want to clarify some things here for the original poster, which others mentioned, but actually did not explicitly state. When you say you want a unique list, this is the very definition of an ordered set. Some other key differences between Set Interface and List include the fact that List allows you to specify an insertion index. So the question is, do you really need a list interface (i.e., for compatibility with a third-party library, etc.), Or can you redesign your software to use the Set interface? You should also consider what you do with the interface. Is it important to find items by their index? How many elements do you expect in your set? If you have many items, is it important to order?

If you really need a List that only has a unique constraint, there is an Apache Common Utils class org.apache.commons.collections.list.SetUniqueList that will provide you with a List interface and a unique constraint. Keep in mind that this violates the List interface. However, you will get better performance if you need to search the list by index. If you can deal with the Set interface, and you have a smaller data set, then LinkedHashSet can be a good way. It just depends on the design and intent of your software.

Again, there are certain advantages and disadvantages to each collection. Some fast inserts, but slow reads, some of them read fast, but slow inserts, etc. It makes sense to spend a lot of time building documentation to fully learn about the more subtle details of each class and interface.

+10
Dec 14 '14 at 18:23
source share

Use new HashSet<String> Example:

 import java.util.HashSet; import java.util.Set; public class MainClass { public static void main(String args[]) { String[] name1 = { "Amy", "Jose", "Jeremy", "Alice", "Patrick" }; String[] name2 = { "Alan", "Amy", "Jeremy", "Helen", "Alexi" }; String[] name3 = { "Adel", "Aaron", "Amy", "James", "Alice" }; Set<String> letter = new HashSet<String>(); for (int i = 0; i < name1.length; i++) letter.add(name1[i]); for (int j = 0; j < name2.length; j++) letter.add(name2[j]); for (int k = 0; k < name3.length; k++) letter.add(name3[k]); System.out.println(letter.size() + " letters must be sent to: " + letter); } } 
+9
Nov 06
source share

You can simply use the HashSet<String> to support a collection of unique objects. If the Integer values ​​on your map are important, you can use the containsKey map method instead to check if your key is already on the map.

+3
Nov 06
source share

HashSet<String> (or) any implementation of Set can do the job for you. Set do not allow duplication.

Here is the javadoc for the HashSet.

+2
Nov 06
source share

I do not know how effective this is, but it worked for me in a simple context.

 List<int> uniqueNumbers = new ArrayList<>(); public void AddNumberToList(int num) { if(!uniqueNumbers .contains(num)) { uniqueNumbers .add(num); } } 
+2
Jun 28 '18 at 7:49
source share

You might want to use one of the java.util.Set<E> Interface implementation classes, for example. java.util.HashSet<String> collection class.

A collection that does not contain duplicate elements. More formally, the sets do not contain a pair of elements e1 and e2 such that e1.equals (e2) and at most one zero element. As its name implies, this interface models a mathematical abstract abstraction.

+1
Nov 06
source share

I.ALI

 list = new ArrayList<>(new HashSet<>(list)); 
0
Jun 03 '19 at 12:47 on
source share



All Articles