Java
Delete a HashSet Item in Java
⏱️ 10 min de lecture
👁️ 1457 vues
This code deletes an object with the method remove() which takes the object to be deleted as an argument. To see the difference, we display the HashSet list before and after deletion.
import java.util.HashSet;
public class HashSetremove{
public static void main(String[] args) {
HashSet< String> hashset = new HashSet< String> ();
hashset.add("v1");
hashset.add("v2");
hashset.add("v3");
hashset.add("v4");
hashset.add("v5");
System.out.println("HashSet before removal: "+hashset);
Object e = "v2";
boolean b = hashset.remove("v2");
System.out.println("the "+e+" element removed: "+b);
System.out.println("HashSet after removing "+e+": "+hashset);
}
}
Output:
HashSet before removal: [v1, v5, v4, v3, v2]
v2 item removed: true
HashSet after v2 removal: [v1, v5, v4, v3]
Commentaires (0)
Laisser un commentaire
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Chargement des commentaires...