Il metodo add() di Set in Java viene utilizzato per aggiungere un elemento specifico in una raccolta Set. La funzione set add() aggiunge l'elemento solo se l'elemento specificato non è già presente nel set altrimenti la funzione restituisce False se l'elemento è già presente nel Set.
tutorial di javascript
Dichiarazione del metodo add()
boolean add (E element) Where, E is the type of element maintained by this Set collection.>
parametri: Il parametro elemento è il tipo di elemento mantenuto da questo Set e si riferisce all'elemento da aggiungere al Set.
Valore di ritorno: La funzione restituisce True se l'elemento non è presente nell'insieme ed è nuovo, altrimenti restituisce False se l'elemento è già presente nell'insieme. I seguenti programmi illustrano l'uso del metodo Java.util.Set.add():
Esempi del metodo add() in Java
Esempio 1
Giava
// 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);> > }> }> |
>
convertire da stringa a intero Java
>Produzione
Set: [Set, 4, Geeks, Welcome, To]>
Esempio 2
Giava
// 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);> > }> }> |
>
>
algoritmo di pianificazione round robinProduzione
Set: [50, 20, 40, 10, 60, 30]>