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 7 in Java by Pradip
58 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 7 by sampriti
• 1,100 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 by Sirajul
• 30,540 points

Related Questions In Java

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 in Java by Sirajul
• 30,540 points
95 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,540 points
97 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 parth295
• 4,640 points
555 views
0 votes
1 answer

How do i create generic array in Java

Generic array creation is not allowed in Java. But, ...READ MORE

answered May 15, 2018 in Java by parth295
• 4,640 points
2,926 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,880 points
585 views
+8 votes
11 answers

Default parameters of XMS and XMX in JVM

You use these Java command-line parameters to ...READ MORE

answered Nov 13, 2018 in Java by Murali
114,650 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 parth295
• 4,640 points
1,368 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,320 points
80 views
0 votes
1 answer

how to compare two words in java?

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

answered Aug 7 in Java by sampriti
• 1,100 points

edited Aug 8 by Kalgi 230 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 in Java by sampriti
• 1,100 points
268 views