How do I get distinct element from list in java

0 votes
Hey, I just want to remove repeated element from my list how do I do that?
Aug 8, 2019 in Java by Pradip
2,222 views

2 answers to this question.

0 votes
import java.util.*;

public class SetE

{

    public static void main(String[] args)

    {

        // Set deonstration using HashSet

        Set<String> hash_Set = new HashSet<String>();

        hash_Set.add("Goat");

        hash_Set.add("For");

        hash_Set.add("Goat");

        hash_Set.add("Example");

        hash_Set.add("Set");


        System.out.print("Set output without the duplicates");

  

        System.out.println(hash_Set);

  


}
}

I think this code can help you out .

Thank you

answered Aug 8, 2019 by sampriti
• 1,120 points
0 votes

You should use a Set. it doesn't contain duplicates.

If you have a List that contains duplicates, you can get the unique entries like this:

List<String> gasList = // create list with duplicates...
Set<String> uniqueGas = new HashSet<String>(gasList);
System.out.println("Unique gas count: " + uniqueGas.size());
answered Aug 23, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
1 answer

How do I make a list with checkboxes in Java Swing?

Hii @kartik, Create a custom ListCellRenderer and asign it to ...READ MORE

answered May 8, 2020 in Java by Niroj
• 82,840 points
2,423 views
0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
2,983 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
1,207 views
0 votes
2 answers

How can I get current time in YYYY:MM:DD HH:MI:Sec:Millisecond format in Java?

public String getCurrentTimeStamp() { ...READ MORE

answered Sep 21, 2018 in Java by Parth
• 4,640 points
7,570 views
0 votes
3 answers

Does Java support Default Parameters?

You can try this with method overloading. void ...READ MORE

answered Sep 21, 2018 in Java by Sushmita
• 6,920 points
6,964 views
+11 votes
13 answers

Default parameters of XMS and XMX in JVM

Run this code to see all the ...READ MORE

answered Nov 13, 2018 in Java by anonymous
402,919 views
0 votes
2 answers

Java Security: Illegal key size or default parameters

Starting from Java 9 or 8u151, you ...READ MORE

answered Aug 23, 2018 in Java by Parth
• 4,640 points
8,657 views
0 votes
1 answer

Executing commands with parameters in Java

Try using the below command: Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", ...READ MORE

answered Sep 5, 2018 in Java by geek.erkami
• 2,680 points
3,159 views
0 votes
1 answer

how to compare two words in java?

hey,  I think in your code the function ...READ MORE

answered Aug 8, 2019 in Java by sampriti
• 1,120 points

edited Aug 8, 2019 by Kalgi 2,296 views
0 votes
1 answer

How to add items in list at a time?

Hi Priyanka , I think this code snippet ...READ MORE

answered Aug 9, 2019 in Java by sampriti
• 1,120 points
2,398 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP