You will receive a file by email of the most frequent last n
You will receive a file by email of the most frequent last names in the U.S. Each line includes name, rank, frequency, proportion per 100,000, and cumulative proportion per 100,000 all separated by commas. Create a Java class to represent the information for each name with five data members. Include all the usual methods including equals and hashCode, both of which use the name.
Write a Java program to read in the information in the file, create instances of the class from part 2, and store them in your NewHashTable using the name as the key and the class from part 2 as the value. After reading and storing the first 10 names in the file, display the NewHashTable using the toString method, continue to read in the rest of the data. Also, make sure the rehash method is called at least once.
Solution
/* As input file is not given I have taken sample data for the programme and displayed all results using toString method*/
import java.util.Enumeration;
import java.util.Hashtable;
public class HashtableExample {
public static void main(String a[]){
Hashtable<User,String> tm = new Hashtable<User, String>();
Enumeration names;
// Java Hashtable example to put object into Hashtable
// put(key, value) is used to insert object into map
tm.put(new User(1,\"Ram\",2,4), \"RAM\");
tm.put(new User(2,\"John\",25,1), \"JOHN\");
tm.put(new User(3,\"Crish\",12,20), \"CRISH\");
tm.put(new User(4,\"Tom\",11,15), \"TOM\");
System.out.println(\"Fecthing value by creating new key:\");
User e = new User(4,\"Tom\",11,15);
// Show all balances in hash table.
names = tm.keys();
while(names.hasMoreElements()) {
e = (User) names.nextElement();
System.out.println(e + \"----- \" + tm.get(e));// Java Hashtable example to get Object from Hashtable
// get(key) method is used to retrieve Objects from Hashtable
}
}
}
class User{
private String name;
private int rank;
private int id;
private int frequency;
public User(int id, String n, int s,int f){
this.id = id;
this.name = n;
this.rank = s;
this.frequency=f;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.frequency = frequency;
}
public int getFrequency() {
return frequency;
}
public void setFrequency(int rank) {
this.rank = rank;
}
public String toString(){
return \"Id: \"+this.id+\" Name: \"+this.name+\" Rank: \"+this.rank+\" Frequency :\"+this.frequency ;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
@Override
public int hashCode() {
System.out.println(\"In hashcode\");
return this.getId();
}
@Override
public boolean equals(Object obj) {
User e = null;
if(obj instanceof User){
e = (User) obj;
}
System.out.println(\"In equals\");
if(this.getId() == e.getId()){
return true;
} else {
return false;
}
}
}
OUTPUT::
In hashcode
In hashcode
In hashcode
In hashcode
Fecthing value by creating new key:
In hashcode
In equals
Id: 4 Name: Tom Rank: 11 Frequency :15----- TOM
In hashcode
In equals
Id: 3 Name: Crish Rank: 12 Frequency :20----- CRISH
In hashcode
In equals
Id: 2 Name: John Rank: 25 Frequency :1----- JOHN
In hashcode
In equals
Id: 1 Name: Ram Rank: 2 Frequency :4----- RAM



