DekGenius.com
JAVA
java remove duplicates
import java.util.*;
public class RemoveDuplicatesFromArrayList {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1,2,2,2,3,5);
System.out.println(numbers);
Set<Integer> hashSet = new LinkedHashSet(numbers);
ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);
System.out.println(removedDuplicates);
}
}
java remove duplicates
public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list){
Set<T> set = new LinkedHashSet<>(list);
return new ArrayList<T>(set);
}
Program to remove duplicates in an ArrayList
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class ArrayDuplicate {
public static void main(String args[]) {
List<Integer> num = new ArrayList<Integer>();
num.add(1);
num.add(2);
num.add(3);
num.add(4);
num.add(5);
num.add(6);
num.add(3);
num.add(4);
num.add(5);
num.add(6);
System.out.println("Your list of elements in ArrayList : " + num);
Set<Integer> primesWithoutDuplicates = new LinkedHashSet<Integer>(num);
num.clear();
num.addAll(primesWithoutDuplicates);
System.out.println("list of original numbers without duplication: " + num);
}
}
remove duplicates from list java
Set<String> set = new HashSet<>(yourList);
yourList.clear();
yourList.addAll(set);
java remove duplicates from list by field
List<Obj> list = ...; // list contains multiple objects
Collection<Obj> nonDuplicateCollection = list.stream()
.collect(Collectors.toMap(Obj::generateUniqueKey, Function.identity(), (a, b) -> a))
.values();
compare two lists and remove duplicates java
listA.removeAll(new HashSet(listB));
remove duplicates from list java
ArrayList<Object> withDuplicateValues;
HashSet<Object> withUniqueValue = new HashSet<>(withDuplicateValues);
withDuplicateValues.clear();
withDuplicateValues.addAll(withUniqueValue);
list remove duplicates Java
// remove duplicates with HashSet
Set<Song> songSet = new HashSet<>(songList);
System.out.println("6: " + songSet);
// TreeSet, remove duplicates and keep it sorted
Set<Song> songTreeSet = new TreeSet<>(songList);
System.out.println("7: " + songTreeSet);
// TreeSet can accept Comparator, pass it in its constructor
Set<Song> songTreeSetComparator = new TreeSet<>((o1, o2) -> o1.getBpm() - o2.getBpm());
songTreeSetComparator.addAll(songList);
System.out.println("8: (sort by BPM)" + songTreeSetComparator);
array remove duplicate in java
List<Integer>numbers = Arrays.asList(1,2,2,2,3,5); // [1, 2, 3, 5]
System.out.println(numbers);
Set<Integer> hashSet = new LinkedHashSet(numbers);
ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);
System.out.println(removedDuplicates); // [1, 2, 3, 5]
© 2022 Copyright:
DekGenius.com