Google stubbornly says that the collections can solve my problem, but which ones? and how best to use them is silent. can something tell me a newcomer?

    4 answers 4

    You need LinkedHashSet; it stores unique values ​​in the order in which you add them.

    List<String> list=new ArrayList<>("a","a","b"); Set<String> set=new LinkedHashSet<>(list); //внутри сета теперь ,"a","b" 
    • there are three sets. may be worth mentioning about all of them? - Alexey Shimansky
    • It’s inconvenient from a phone ( - Yuriyi SPb

    Exists. For example: let integerArrayList contain (in particular) duplicates, then integerHashSet will contain only unique values ​​from integerArrayList .

     ArrayList<Integer> integerArrayList = new ArrayList<>(); HashSet<Integer> integerHashSet = new HashSet<>(integerArrayList); 

      If you want to get a list of unique elements and the initial order of insertion is important to you, this is done with the following code:

       List<Integer> list = Arrays.asList(1, 2, 3, 4, 1, 1, 1); List<Integer> uniqueElements = list .stream() .distinct() .collect(Collectors.toList()); 

      If, however, you just want to receive a collection of unique elements, then use the answer @post_zeew

      • There is an Android label on the question, but you cannot use features from Java 8 on it - YuriiSPb
      • @Yuriy SPb I heard that you can medium.com/@nicopasso/… - Artem Konovalov
       HashSet<String> data_resh_hashset = new HashSet <String>(); String[] data_resh; 

      helped

      • worth adding an example: exactly how this collection solves your problem - Grundy