The Wayback Machine - https://web.archive.org/web/20240618020202/https://www.geeksforgeeks.org/set-size-method-in-java-with-example/
Open In App

Set size() method in Java with Example

Last Updated : 31 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.Set.size() method is used to get the size of the Set or the number of elements present in the Set.

Syntax:

int size()

Parameters: This method does not takes any parameter.

Return Value: The method returns the size or the number of elements present in the Set.

Below program illustrate the java.util.Set.size() method:




// Java code to illustrate Set.size() method
  
import java.util.*;
  
public class SetDemo {
    public static void main(String args[])
    {
        // Creating an empty Set
        Set<String> set = new HashSet<String>();
  
        // Use add() method to add elements into the Set
        set.add("Welcome");
        set.add("To");
        set.add("Geeks");
        set.add("4");
        set.add("Geeks");
  
        // Displaying the Set
        System.out.println("Set: " + set);
  
        // Displaying the size of the Set
        System.out.println("The size of the set is: " + set.size());
    }
}


Output:

Set: [4, Geeks, Welcome, To]
The size of the set is: 4

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#size()


Previous Article
Next Article

Similar Reads

Stack size() method in Java with Example
The Java.util.Stack.size() method in Java is used to get the size of the Stack or the number of elements present in the Stack. Syntax: Stack.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Stack. Below programs illustrate the Java.util.Stack.size() method:
2 min read
AbstractSet size() method in Java with Example
The size() method of AbstractSet in Java is used to get the size for this instance of the AbstractSet. It returns an integer value which is the size for this instance of the AbstractSet. Syntax: public int size() Parameters: This function has no parameters. Returns: The method returns an integer value which is the size for this instance of the Abst
2 min read
AbstractSequentialList size() method in Java with Example
The size() method of AbstractSequentialList in Java is used to get the size for this instance of the AbstractSequentialList. It returns an integer value which is the size for this instance of the AbstractSequentialList. Syntax: public int size() Parameters: This method does not accepts any parameter. Returns: The method returns an integer value whi
2 min read
Stack set() method in Java with Example
The set() method of Java Stack is used to replace any particular element in the stack created using the Stack class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: public E set(int index, Object element) Parameters: This function accept
3 min read
AbstractSequentialList set(int, Object) method in Java with Example
The set() method of Java AbstractSequentialList is used to replace any particular element in the list created using the AbstractSequentialList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: public E set(int index, Object element)
3 min read
Set retainAll() method in Java with Example
The retainAll() method of java.util.Set interface is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this set. Return Value: This method returns true if thi
3 min read
Set toArray() method in Java with Example
The toArray() method of Java Set is used to form an array of the same elements as that of the Set. Basically, it copies all the element from a Set to a new array.Syntax: Object[] toArray() Parameters: The method takes optional parameter. If we provide the type of Object array we want we can pass it as an argument. for ex: set.toArray(new Integer[0]
2 min read
MessageFormat format() method in Java with Example : Set - 1
The format() method of java.text.MessageFormat class is used to get the formatted array of object appended into the string buffer object. formatted array will contain all forms of element lies in the pattern of MessageFormat object.Syntax: public final StringBuffer format(Object[] arguments, StringBuffer result, FieldPosition pos) Parameter: argume
3 min read
MessageFormat format() method in Java with Example : Set - 2
The format() method of java.text.MessageFormat class is used to get the formatted array of object according to the specified pattern of message format object. new string pattern will be considered while performing the action.Syntax: public static String format(String pattern, Object... arguments) Parameter: This method takes following argument as a
2 min read
MessageFormat parse() method in Java with Example : Set - 1
The parse() method of java.text.MessageFormat class is used to parse the string object from the beginning of the index.Syntax: public Object[] parse(String source) throws ParseException Parameter: This method takes string source as an argument over which parsing is going to take place.Return Value: This method returns array of object as an output.E
2 min read