Listcolors = new ArrayList<>(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); System.out.println(colors.contains("Green")); // Output: true System.out.println(colors.contains("Yellow")); // Output: false
ListIn this example, we have created a list of numbers using the Arrays.asList() method and checked if 10 and 4 are present in the list using contains() method. The List interface belongs to the java.util package library.numbers = Arrays.asList(2, 4, 6, 8); System.out.println(numbers.contains(10)); // Output: false System.out.println(numbers.contains(4)); // Output: true