I know that this question has been answered “how to find” many times, however I have a few additional questions. Here is the code I have
public static void main (String [] args){
List<String> l1= new ArrayList<String>();
l1.add("Apple");
l1.add("Orange");
l1.add("Apple");
l1.add("Milk");
HashSet<String> set = new HashSet<String>();
for( String e: l1){
if(!(set).add(e)){
System.out.println(e);
}
Question 1: The list does not work, because List allows duplication, and HashSet is the correct assumption?
Question 2: What does this line mean: if (! (Set) .add (e)) In the for loop we check if the line St is in the list l1, and then what does this line do if (! (Set) .add (e))
This code will print the apple as a result, as this is a duplicate value.
Question 3: How can I print non-Duplicate values, just Orange and Dairy, but not Apple? I tried this approach, but it still prints to Apple. List unique = new ArrayList (new HashSet (l1));
Thanks in advance for your time.