remove duplicates from list in java using set

remove duplicates from list in java using set

Hi,

I have a list with multiple duplicate values inside it. I want ready to use api for removing duplicates.

I searched and found that set can be used. But don't know how to do this?

Tell me program for the removal of duplicates from list in java using set?

Thanks

View Answers

February 19, 2020 at 3:57 AM

Hi,

The Set interface in Java is part of Collection API which does not allow the duplicate elements.

Here is example code to remove duplicates from list in java using set:

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class RemoveDuplicatesWithSet {

    public static void main(String[] args) {
        // Define array list
        List<String> daysList = Arrays
                .asList(new String[] { "Sunday", "Monday", "Tuesday", "SUNDAY", "MONDAY", "TUESDAY" });
        //Create set object with CASE_INSENSITIVE_ORDER feature
        Set<String> uniqueSet = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);

        //Add list to the set - Set will add only unique values
        uniqueSet.addAll(daysList);

        for (String day : uniqueSet) {
            System.out.println(day);
        }

    }

}

Thanks


February 19, 2020 at 3:59 AM

Hi,

You can convert the set object back to list with following example code:

List<String> list = new ArrayList<>(uniqueSet);

Thanks


February 19, 2020 at 4:00 AM

Hi,

This is another example:

Thanks









Related Tutorials/Questions & Answers:
remove duplicates from list in java using set
remove duplicates from list in java using set  Hi, I have a list... this? Tell me program for the removal of duplicates from list in java using set... duplicates from list in java using set: import java.util.Arrays; import java.util.List
remove item from list box using java script - Java Beginners
remove item from list box using java script  remove item from list box using java script  Hi friend, Code to remove list box item using java script : Add or Remove Options in Javascript function addItem
Advertisements
Remove duplicates from ArrayList
Remove duplicates from ArrayList Sometimes we added large amount of data... is to add the contents to a Set (which will not allow duplicates) and then add...) { HashSet set = new HashSet(list); list.clear(); list.addAll(set
List with out duplicates - Java Interview Questions
List with out duplicates   Hi all, Naturally in java a list will allow duplicates, but if i want the list which shouldn't allow duplicates..., Use contains(Object o) method of Interface List public boolean contains(Object
Finding duplicates using array in for loop
Finding duplicates using array in for loop  how to find the duplicates in array using for loop
How to access the "Add/Remove Programs" list in Control Panel using Java Program? - Java Beginners
How to access the "Add/Remove Programs" list in Control Panel using Java Program?  Dear Sir, I'm very interested in creating java programs innovatively. I've planned to write a java coding for accessing the Add/Remove
remove punctuation from string in java
remove punctuation from string in java  Hi, I want to remove punctuation in text from Java program. How to remove punctuation from string in java... String str = "Welcome to the Java Programming,; ???? .. Here you
How to set attachment from browse button to send mails using java mail api
How to set attachment from browse button to send mails using java mail api   Hello Sir, I am making Email Sending app using java mail api in jsp and servlet in that i have to make attachment option available using
write a program to remove the element from hashset using keyboard.
write a program to remove the element from hashset using keyboard.  write a program to remove the element from hashset using keyboard
Need to remove duplicated rows from excel using apache POI API
Need to remove duplicated rows from excel using apache POI API  Hi... duplicated rows from excel sheet by using apache poi api oe jxl api... duplicated rows from excel. same as below i need to do by using apache POI
how to remove the column from a java web page
how to remove the column from a java web page  i have a web page with account#, qtr, year if i want to remove the year column which is a drop down list from my jsp what should i do and what is the process please give a brief view
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
3 dropdown list from the database using JSP
3 dropdown list from the database using JSP  Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other and get the options from the database using JSP
Need to remove duplicate rows from the Excel Sheet using the Apache POI API
Need to remove duplicate rows from the Excel Sheet using the Apache POI API   Hi There, Is any one aware of java code on how to find the duplicate rows in excel.I am using the Apache POI API to achieve the same have googled
How to store unique values in Java using Set?
Store unique values in Java using Set interface In this section we will discuss about the Set interface in Java which is designed to store unique values. We will show you how to store unique values in Java using Set? The Set interface
How to store unique values in Java using Set?
Store unique values in Java using Set interface In this section we will discuss about the Set interface in Java which is designed to store unique values. We will show you how to store unique values in Java using Set? The Set interface
Creating custom output using XMLUnit - MatchTracker duplicates the output data
Creating custom output using XMLUnit - MatchTracker duplicates the output data  how to create a custom output using xmlunit, I have tried it and used MatchTracker, but the match tracker was repeated for the total number of nodes
Java Remove a character from string
Java Remove a character from string In this tutorial, you will learn how to remove a character from the string. There are several methods for examining... to remove a particular character from the string. In the given example, we have
remove a substring from a string
remove a substring from a string  Please I jave an arraylist composed as follwoing [w1, w2, w3, w4, w1.w2.w3, w2.w3.w4, w3.w4, w2.w4, w1.w3, w1.w2] w2.w4 is a subset of w2.w3.w4 ?how I recognize it also ADS_TO_REPLACE_1 w1.w3
remove element from stack
remove element from stack  Hi there I'm looking around on the internet how to remove names from stack by asking user how many names they want... error message and if not then remove the names from stack until the stack is empty
How to set the border in header and footer from login page using swings/awt
How to set the border in header and footer from login page using swings/awt  Hi, How to set the border in header and footer from login page using swings/awt
Add/Remove Rows Dynamically with drop down list in html table using php
Add/Remove Rows Dynamically with drop down list in html table using php  Here Is My Code. I want to give and get the id of drop down list. Can Anyone... the following link: JSP Add Remove row using Javascript
How do I set environment variables from Java?
How do I set environment variables from Java?  How do I set environment variables from Java
How to access Widows" Add/Remove Programs List"? - Java Beginners
How to access Widows" Add/Remove Programs List"?  Dear Friends, How to access the Windows' Add/Remove Program List? Is there any special jar to add for acccessing it? Kindly provide me the solution.... Thanks & Regards
how to set values in object using java and what are the ways?
how to set values in object using java and what are the ways?  How to set values in object using java ?what are the ways
Set Textbox in PowerPoint Slide Using Java
Set Textbox in PowerPoint Slide Using Java       In this example we are going create textbox to set text in PowerPoint using java. In this example we are creating an object of 
how to show set of images continously using JScrollpane in java
how to show set of images continously using JScrollpane in java  how to show set of images continously using JScrollpane in java
how to show set of images continously using JScrollpane in java
how to show set of images continously using JScrollpane in java  how to show set of images continously using JScrollpane in java
displaying List of records from database in a jsp using ajax
displaying List of records from database in a jsp using ajax  Sir, I need to retrieve the records from the database for every 7 seconds and display... the records using jstl tag libraries. But it is not working. I think
view data from database using drop down list
view data from database using drop down list  hi i want to view the data from database by selecting a value in a drop down list. for an example drop down list have picture element.when click it select pictures from the database
how to get data from list to map with out using a loop?
how to get data from list to map with out using a loop?  List list...("15"); how to get this data into map with out using a loop. i want this data like this--map.put(name,id); Is it possible to do without using loop
Display list of files from a directory using Java
Display list of files from a directory using Java In this section, you will learn how to display the list of files of a particular directory or a sub...() method display the list of file names of that directory and if it is having any
How to access Contacts from gmail using LDAP in JSP dropdown list
How to access Contacts from gmail using LDAP in JSP dropdown list  HI, I am creating a JSP page in which i have to make a dropdown box, and access gmail contacts in that drop downlist using LDAP. Can any one plz help me out
Remove Repeated Characters from the String
Remove Repeated Characters from the String In this we are going to remove repeated characters from the string. For this, we have allowed the user to enter the string. The class BufferedReader reads the string from the console. Then we
How to set background of an excel sheet using POI - Java Magazine
How to set background of an excel sheet using POI  Hi, i am trying to format the excel using java. How to set the background color of an excel sheet( complete sheet which includes all cells). i am using poi2.5.1. Thanks
Eliminate Duplicates in Arraylist - Java Interview Questions
Eliminate Duplicates in Arraylist    Hi Friends, Can any one give sample code for removing duplicates in arraylist and hashmap. Thank u in advance.  Hi friend, Code to help in solving the problem
Create list from array in Java
Create list from array in Java  Hi, I have an array with some data. How to Create list from array in Java? Thanks   Hi, Here is example of creating of creating an array: String[] names = new String[]{"Java", "JSP
remove comma from string php
remove comma from string php  How to remove the last comma from the string in PHP
remove comma from string php
remove comma from string php  How to remove the last comma from the string in PHP?   remove last comma from string names = names.replaceAll(",$", ""); //Or a simple substring: if (names.endsWith(",")) { names
remove from superview iphone sdk
remove from superview iphone sdk  How to remove the old view from super-view and add the new view safely? My main concern is memory leak.. so, please suggest how can i do it without facing a memory leak. Thanks
Remove duplicate characters from the string
Remove duplicate characters from the string In this tutorial, you will learn how to remove duplicate characters from the string. Java has provide several.... Here we are going to remove duplicate characters from the string
remove all objects from NSMutablearray
remove all objects from NSMutablearray  Hi, How to remove all objects from nsmutablearray? Thanks   HI, Here is the code for creating NSMutablearray, adding objects and then removing all the objects: NSMutableArray
JSP Get Data Into Dropdown list From Database
for fetching data from the database and set it into the dropdown list in JSP...JSP Get Data Into Dropdown list From Database In this section we will discuss.... This tutorial explains you that how to fetch data from database and set
how to load value in dropdown list after selecting value from first dropdown list using javascript
how to load value in dropdown list after selecting value from first dropdown list using javascript  how to load value in dropdown list after selecting value from first dropdown list using javascript
set the focus on an element using Javascript
set the focus on an element using Javascript  How to set the focus on an element using Javascript
checkboxes set to true using JavaScript
checkboxes set to true using JavaScript  To set all checkboxes to true using JavaScript
How to remove specific element from Hashset.
How to remove specific element from Hashset. In this  exmple, you will see how to remove specific element from hashset. The HashSet class provides a method called remove. It removes a elements from set. Code: 
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected

Ads