Search
 
SCRIPT & CODE EXAMPLE
 

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);
    }
}
Comment

String remove duplicate method in java

public static void main(String[] args) {

        String result = removeDup("AAABBBCCC");
        System.out.println(result); // ABC

public static  String  removeDup( String  str) {
        String result = "";
        for (int i = 0; i < str.length(); i++)
            if (!result.contains("" + str.charAt(i)))
                result += "" + str.charAt(i);
        return result;
    }
}
Comment

java remove duplicates

public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list){
  Set<T> set = new LinkedHashSet<>(list);
  return new ArrayList<T>(set);
}
Comment

remove duplicates from list java

Set<String> set = new HashSet<>(yourList);
yourList.clear();
yourList.addAll(set);
Comment

remove duplicates from list java

 ArrayList<Object> withDuplicateValues;
 HashSet<Object> withUniqueValue = new HashSet<>(withDuplicateValues);
 
 withDuplicateValues.clear();
 withDuplicateValues.addAll(withUniqueValue);
Comment

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);
Comment

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]
Comment

PREVIOUS NEXT
Code Example
Java :: define 2d array in java 
Java :: compare time in java 
Java :: java runtime.getruntime().exec 
Java :: object class java 
Java :: how to find last element in array java 
Java :: java hashtable 
Java :: bufferedreader 
Java :: java digit in number 
Java :: char to charsequence in java 
Java :: how to set current item for spinner android 
Java :: check if string is decimal java 
Java :: how to check if array is full java 
Java :: enum to string java 
Java :: java string start with substring 
Java :: arraylist remove value in java 
Java :: how to find a int 
Java :: java stack with max size 
Java :: /bin/sh 1 java not found docker 
Java :: grepper mcm java 
Sql :: safe update mysql 
Sql :: oracle change nls_date_format permanently 
Sql :: create database mysql utf8 
Sql :: mysql_secure_installation 
Sql :: alter table column change data type to text mysql 
Sql :: mysql get column names from table 
Sql :: sql server alter table column nullable 
Sql :: liquibase alter column type / length 
Sql :: rename column oracle 
Sql :: wordpress change url in database 
Sql :: sqlite3 now 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =