Setcolors = new HashSet<>(); colors.add("Red"); colors.add("Blue"); colors.add("Green"); System.out.println(colors.contains("Blue")); // Output: true System.out.println(colors.contains("Yellow")); // Output: false
SetIn this example, we have created a TreeSet of Integers and added three elements to it. We have then used the contains() method to check if the set contains the element 10 and 20. Package Library: java.utilnumbers = new TreeSet<>(); numbers.add(5); numbers.add(10); numbers.add(15); System.out.println(numbers.contains(10)); // Output: true System.out.println(numbers.contains(20)); // Output: false