In: Computer Science
if i miss something please et me know by comment as very less details given
Created Employee class 5 data members for adding to hashtable
//Employee.java
import java.util.Objects; public class Employee { // class with 5 data members String fname; String lname; String ssn; int id; String city; // constructor public Employee(String fname, String lname, String ssn, int id, String city) { this.fname = fname; this.lname = lname; this.ssn = ssn; this.id = id; this.city = city; } public Employee() { } // setters and getters public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public String getSsn() { return ssn; } public void setSsn(String ssn) { this.ssn = ssn; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Employee employee = (Employee) o; return id == employee.id && Objects.equals(fname, employee.fname) && Objects.equals(lname, employee.lname) && Objects.equals(ssn, employee.ssn) && Objects.equals(city, employee.city); } @Override public int hashCode() { return Objects.hash(fname, lname, ssn, id, city); } @Override public String toString() { return "Employee{" + "fname='" + fname + '\'' + ", lname='" + lname + '\'' + ", ssn='" + ssn + '\'' + ", id=" + id + ", city='" + city + '\'' + '}'; } }
Created main class for adding the object into hashtable , for key taken as int value
//EmployeeMain.java
import java.util.Hashtable; import java.util.Iterator; import java.util.Random; import java.util.Set; public class EmpoyeeMain { public static void main(String[] args) { // hashtable created Hashtable<Integer, Employee> hashtable = new Hashtable<>(); for (int i = 0; i < 10; i++) { Employee e = new Employee(); e.setFname(getRandomName()); e.setLname(getRandomName()); e.setSsn(getRandonSSN()); e.setId(i+1); e.setCity("Pune"); hashtable.put(i,e);//adding to hashtable } // printing table Set<Integer> keys = hashtable.keySet(); Iterator<Integer> itr = keys.iterator(); while (itr.hasNext()) { int i = itr.next(); System.out.println("Key: "+i+" & Value: "+hashtable.get(i)); } } //helper function public static String getRandomName() { String alphabets = leetter_digits.toLowerCase().substring(0, 25); StringBuilder builder = new StringBuilder(); Random rand = new Random(); int length = rand.nextInt(5) + 5; for (int i = 0; i < length; i++) { builder.append(alphabets.charAt(rand.nextInt(alphabets.length()))); } return builder.toString(); } final static String leetter_digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890"; public static String getRandonSSN() { Random rand = new Random(); StringBuilder builder = new StringBuilder(); int length = rand.nextInt(5) + 5; for (int i = 0; i < length; i++) { builder.append(leetter_digits.charAt(rand.nextInt(leetter_digits.length()))); } return builder.toString(); } }
//OUTPUT it may differ as it used random
Key: 9 & Value: Employee{fname='igndlhmw', lname='mfwuurwy',
ssn='1WN8JX', id=10, city='Pune'}
Key: 8 & Value: Employee{fname='pnqmoa', lname='jmjtww',
ssn='BMEPH', id=9, city='Pune'}
Key: 7 & Value: Employee{fname='rbapxq', lname='uhpbcknyv',
ssn='4IGO8', id=8, city='Pune'}
Key: 6 & Value: Employee{fname='htkjhxfg', lname='tnojnwgv',
ssn='XRJ8XYJGR', id=7, city='Pune'}
Key: 5 & Value: Employee{fname='bsqsv', lname='onfqurc',
ssn='QAHIDVZM', id=6, city='Pune'}
Key: 4 & Value: Employee{fname='qlncsa', lname='ehfprjg',
ssn='9O40NOBHB', id=5, city='Pune'}
Key: 3 & Value: Employee{fname='mswogm', lname='kbitah',
ssn='ACDJ7W4W', id=4, city='Pune'}
Key: 2 & Value: Employee{fname='unqgcupg', lname='ycpxvf',
ssn='RXPZVQL', id=3, city='Pune'}
Key: 1 & Value: Employee{fname='smbqjuqob', lname='jwjwo',
ssn='9NJ4K', id=2, city='Pune'}
Key: 0 & Value: Employee{fname='glerxjfg', lname='vccgc',
ssn='ALHKV4YY7', id=1, city='Pune'}