Remove an element using collections.
Collections:
import java.util.*;
public class HashSetExample {
public static void main(String [] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter elements: ");
HashSet <String>set = new HashSet <String>();
for(int i=1;i<=5;i++){
String st=input.next();
set.add(st);
}
System.out.print("Which element do you want to remove: ");
String element=input.next();
set.remove(element);
System.out.println("Hash Set data: ");
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
import java.util.*;
public class HashSetExample {
public static void main(String [] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter elements: ");
HashSet <String>set = new HashSet <String>();
for(int i=1;i<=5;i++){
String st=input.next();
set.add(st);
}
System.out.print("Which element do you want to remove: ");
String element=input.next();
set.remove(element);
System.out.println("Hash Set data: ");
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Comments
Post a Comment
Please post comments here:-)