Set add() method in Java with Examples Last Updated : 24 May, 2023 Comments Improve Suggest changes 8 Likes Like Report The add() method of Set in Java is used to add a specific element into a Set collection. The set add() function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Declaration of add() methodboolean add(E element) Where, E is the type of element maintained by this Set collection. Parameters: The parameter element is the type of element maintained by this Set and it refers to the element to be added to the Set. Return Value: The function returns True if the element is not present in the set and is new, else it returns False if the element is already present in the set. The below programs illustrate the use of Java.util.Set.add() method: Examples of add() method in JavaExample 1 Java // Java code to illustrate add() method import java.io.*; import java.util.*; public class TreeSetDemo { public static void main(String args[]) { // Creating an empty Set Set<String> s = new HashSet<String>(); // Use add() method to add elements into the Set s.add("Welcome"); s.add("To"); s.add("Geeks"); s.add("4"); s.add("Geeks"); s.add("Set"); // Displaying the Set System.out.println("Set: " + s); } } OutputSet: [Set, 4, Geeks, Welcome, To] Example 2 Java // Java code to illustrate add() method import java.io.*; import java.util.*; public class TreeSetDemo { public static void main(String args[]) { // Creating an empty Set Set<Integer> s = new HashSet<Integer>(); // Use add() method to add elements into the Set s.add(10); s.add(20); s.add(30); s.add(40); s.add(50); s.add(60); // Displaying the Set System.out.println("Set: " + s); } } OutputSet: [50, 20, 40, 10, 60, 30] Create Quiz Comment G gopaldave Follow 8 Improve G gopaldave Follow 8 Improve Article Tags : Java Java-Collections Java-Functions java-set 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