AbstractSet contains() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The contains() method of Java AbstractSet is used to check whether an element is present in a set or not. It takes the element as a parameter and returns True if the element is present in the set. Syntax: public boolean contains(Object element) Parameters: The parameter element is of type set. This parameter refers to the element whose occurrence is needed to be checked in the set. Return Value: The method returns a boolean value. It returns True if the element is present in the set otherwise it returns False. Below programs illustrate the AbstractSet.contains() method: Program 1: Java // Java code to illustrate // AbstractSet.contains() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty set AbstractSet<String> abs = new TreeSet<String>(); // Use add() method to add // elements in the set abs.add("Geeks"); abs.add("for"); abs.add("Geeks"); abs.add("10"); abs.add("20"); // Displaying the set System.out.println("AbstractSet: " + abs); // Check if the set contains "Hello" System.out.println("\nDoes the set" + " contains 'Hello': " + abs.contains("Hello")); // Check if the set contains "20" System.out.println("Does the set" + " contains '20': " + abs.contains("20")); // Check if the set contains "Geeks" System.out.println("Does the set" + " contains 'Geeks': " + abs.contains("Geeks")); } } Output: AbstractSet: [10, 20, Geeks, for] Does the set contains 'Hello': false Does the set contains '20': true Does the set contains 'Geeks': true Program 2: Java // Java code to illustrate // AbstractSet.contains() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty set AbstractSet<Integer> abs = new TreeSet<Integer>(); // Use add() method to add // elements in the set abs.add(10); abs.add(20); abs.add(30); abs.add(40); abs.add(50); // Displaying the set System.out.println("AbstractSet:" + abs); // Check if the set contains 10 System.out.println("\nDoes the set " + "contains '10': " + abs.contains(10)); // Check if the set contains 50 System.out.println("\nDoes the set" + " contains '50': " + abs.contains(50)); // Check if the set contains 100 System.out.println("Does the set" + " contains '100': " + abs.contains(100)); } } Output: AbstractSet:[10, 20, 30, 40, 50] Does the set contains '10': true Does the set contains '50': true Does the set contains '100': false Create Quiz Comment C code_r Follow 0 Improve C code_r Follow 0 Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-AbstractSet +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like