Online Java Compiler By
JavaTpoint.com
import java.util.*; class Book { int id; String name,author,publisher; int quantity; public Book(int id, String name, String author, String publisher, int quantity) { this.id = id; this.name = name; this.author = author; this.publisher = publisher; this.quantity = quantity; } } public class HashSetRemoveExample3 { public static void main(String[] args) { HashSet
set=new HashSet
(); //Creating Books Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8); Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4); Book b3=new Book(103,"Operating System","Galvin","Wiley",6); //Adding Books to HashSet set.add(b1); set.add(b2); set.add(b3); //Traversing HashSet for(Book b:set){ System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity); } //Removing elements to HashSet set.remove(b2); System.out.println("Books after remove method:"); for(Book b:set){ System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity); } } }
Output