Java Set find duplicate values in Collection
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) throws java.lang.Exception { List<String> people = new ArrayList<>(); people.add("Java"); people.add("Javascript"); people.add("C++"); people.add("HTML"); people.add("Java"); Set<String> seen = new HashSet<>(); for (String p : people) { if (seen.add(p)) { System.out.println(p + " is new"); } else {/* www.j av a2 s . co m*/ System.out.println(p + " is a duplicate"); } } } }