El método add() de Set en Java se utiliza para agregar un elemento específico a una colección Set. La función set add() agrega el elemento solo si el elemento especificado aún no está presente en el conjunto; de lo contrario, la función devuelve False si el elemento ya está presente en el conjunto.
tutorial de pyspark
Declaración del método add()
boolean add (E element) Where, E is the type of element maintained by this Set collection.>
Parámetros: El parámetro elemento es el tipo de elemento que mantiene este Conjunto y se refiere al elemento que se agregará al Conjunto.
Valor de retorno: La función devuelve Verdadero si el elemento no está presente en el conjunto y es nuevo; en caso contrario, devuelve Falso si el elemento ya está presente en el conjunto. Los siguientes programas ilustran el uso del método Java.util.Set.add():
Ejemplos del método add() en Java
Ejemplo 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 s =>new> HashSet();> >// 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);> >}> }> |
>
números para el alfabeto
>Producción
Set: [Set, 4, Geeks, Welcome, To]>
Ejemplo 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 s =>new> HashSet();> >// 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);> >}> }> |
>
rdbms
>Producción
Set: [50, 20, 40, 10, 60, 30]>