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
21 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 2 days ago by Sirajul
• 19,670 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 2 days ago in Java by Sirajul
• 19,670 points
61 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
82 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,600 points
270 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,600 points
2,151 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,870 points
264 views
+7 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
89,197 views
0 votes
2 answers

Java Security: Illegal key size or default parameters

The JRE/JDK/Java 8 jurisdiction files can be ...READ MORE

answered 2 days ago in Java by Sirajul
• 19,670 points
1,108 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
73 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 53 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
69 views