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

Set size() method in Java with Example

Last Updated : 26 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 take 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
// 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
MessageFormat parse() method in Java with Example : Set - 2
The parse() method of java.text.MessageFormat class is used to parse the string object starting from the passed parse position in the parse() method.Syntax: public Object[] parse(String source, ParsePosition pos) Parameter: This method takes the following arguments as parameter. source :- string over which parsing is going to perform.pos :- it is t
2 min read
HijrahChronology date() method in Java with Example : Set - 1
The date() method of java.time.chrono.HijrahChronology class is used get the local date according to Hijrah calendar system for the given year, month and day.Syntax: public HijrahDate date(int prolepticYear, int month, int day) Parameter: This method takes the following arguments as parameter. prolepticYear: integer proleptic year for the year fiel
2 min read
HijrahChronology date() method in Java with Example : Set - 2
The date() method of java.time.chrono.HijrahChronology class is used get the local date according to Hijrah calendar system from the another TemporalAccessor object . Syntax: public HijrahDate date(TemporalAccessor temporal) Parameter: This method takes the object of any temporal accessor as a parameter. Return Value: This method returns the local
2 min read
HijrahChronology dateYearDay() method in Java with Example : Set 2
The dateYearDay() method of java.time.chrono.HijrahChronology class is used to retrieve the local date according to the Islamic hijri calendar for the particular era. Syntax: public HijrahDate dateYearDay(Era era, int year, int day) Parameter: This method takes the following arguments as parameter. era: which is the object of type Era year: which i
2 min read
HijrahChronology localDateTime() method in Java with Example : Set - 2
The localDateTime() method of java.time.chrono.HijrahChronology class is used to retrieve the local date and time according to Hijrah calendar system from any other object of temporal accessor. Syntax: public ChronoLocalDateTime localDateTime(TemporalAccessor temporal) Parameter: This method takes the object of any temporal accessor as a parameter.
2 min read
HijrahChronology dateYearDay() method in Java with Example : Set 1
The dateYearDay() method of java.time.chrono.HijrahChronology class is used to retrieve the local date according to the Islamic hijri calendar for the particular year and date.Syntax: public HijrahDate dateYearDay(int year, int day) Parameter: This method takes the following arguments as parameter: year: which is the integer proleptic year for the
2 min read
HijrahChronology zonedDateTime() method in Java with Example : Set 1
The zonedDateTime() method of java.time.chrono.HijrahChronology class is used to retrieve the date and time of a particular zone according to Islamic hijri calendar from a particular instant. Syntax: public ChronoZonedDateTime zonedDateTime(Instant instant, ZoneId zone) Parameter: This method takes the following arguments as parameter. instant: whi
2 min read
Java 8 | DoubleToIntFunction Interface in Java with Example
The DoubleToIntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a double-valued argument and gives an int-valued result. The lambda expression assigned to an object of DoubleToIntFunction type is used to define
1 min read
Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
TimeZone class (the methods of this class was discussed in this article Java.util.TimeZone Class | Set 1) can be used in many cases like using TimeZone class we can get the time difference in term of hour and minute between two places.Problem : How we can get time difference of time in terms of hours and minutes between two places of Earth?Solution
5 min read
Java Math asin() method with Example
The java.lang.Math.asin() returns the arc sine of an angle in between -pi/2 and pi/2. Arc sine is also called as an inverse of a sine. If the argument is NaN or its absolute value is greater than 1, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument. Syntax : public static double asin(doubl
1 min read
Java String isEmpty() method with example
Java.lang.String.isEmpty() String method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false. The isEmpty() method of the String class is included in Java string since JDK 1.6. In other words, you can say that this method returns true if the length of the string is 0. Syntax of Java
3 min read
Java String contains() method with example
The java.lang.String.contains() method searches the sequence of characters in the given string. It returns true if the sequence of char values is found in this string otherwise returns false. Implementation of contains() methodpublic boolean contains(CharSequence sequence) { return indexOf(sequence.toString()) > -1; } Here conversion of CharSequ
2 min read
Java Math log() method with example
The java.lang.Math.log() method returns the natural logarithm (base e) of a double value as a parameter. There are various cases : If the argument is NaN or less than zero, then the result is NaN.If the argument is positive infinity, then the result is positive infinity.If the argument is positive zero or negative zero, then the result is negative
1 min read
Java Math hypot() method with Example
java.lang.Math.hypot() function is an inbuilt math function in Java that returns the Euclidean norm, [TEX] \sqrt{(x * x + y * y)} [/TEX]. The function returns sqrt(x2 + y2) without intermediate overflow or underflow. If either argument is infinite, then the result is positive infinity. If either argument is NaN and neither argument is infinite, the
1 min read
Java Math exp() method with Example
The java.lang.Math.exp() returns euler's number e raised to the power of a double value. If the argument is NaN, the result is NaN. If the argument is positive infinity, then the result is positive infinity. If the argument is negative infinity, then the result is positive zero. Syntax : public static double exp(double a) Parameter : a : the expone
1 min read
Java Math getExponent() method with Example
The java.lang.Math.getExponent() returns the unbiased exponent used in the representation of a double or float. Note: If the argument is NaN or infinite of double or float type, then the result is (Double.MAX_EXPONENT + 1) or (Float.MAX_EXPONENT + 1). If the argument is zero or subnormal of double or float type, then the result is (Double.MIN_EXPON
2 min read
Java Math getExponent() method with Example
The java.lang.Math.getExponent() is a built-in math function which returns the unbiased exponent used in the representation of a float. If the argument is NaN or infinite, then the result is Float.MAX_EXPONENT + 1. If the argument is zero or subnormal, then the result is Float.MIN_EXPONENT -1. Syntax: public static int getExponent(float val) Parame
1 min read
Java Math expm1() method with Example
The java.lang.Math.expm1() returns ex -1. Note that for values of x near 0, the exact sum of expm1(x) + 1 is much closer to the true result of ex than exp(x). If the argument is NaN, the result is NaN. If the argument is positive infinity, then the result is positive infinity. If the argument is negative infinity, then the result is -1.0. If the ar
2 min read
Java Math round() method with Example
The java.lang.Math.round() is a built-in math function which returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result after adding 1/2, and casting the result to type long. If the argument is NaN, the result is 0. If the argument is negative infinity or any value less than or equal
2 min read
Java toRadians() method with Example
The java.lang.Math.toRadians() is used to convert an angle measured in degrees to an approximately equivalent angle measured in radians. Note: The conversion from degrees to radian is generally inexact. Syntax: public static double toRadians(double deg) Parameter: deg : This parameter is an angle in degrees which we want to convert in radians. Retu
1 min read
three90RightbarBannerImg