Purpose: Use Hashing Techniques Create a ADT to handle the data in the Customer.
ID: 3825885 • Letter: P
Question
Purpose: Use Hashing Techniques
Create a ADT to handle the data in the Customer.csv data file: (last,first,id)
perez,diana,86824983-3587182
oxford,greg,49451687-6884854
smith,tsung,34722447-9802850
Place each ADT data object into a Hashing structure using a custom hashing function. Demonstrate you can hash the name data as key and id as value, and visa versa. One of the Keys or Values to the hash structure is required to be an ADT type. This will require overloading appropriate operators (in C++) to support the < operator.
Write a hashing function that produces (ideally) a maximum of 5% collisions. Also, ideally, your space usage should be around 75% of the container.
Explanation / Answer
1. Create calss to hold customer data
package csvadt;
public class CustomerData {
private String id;
private String lastName;
private String firstName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CustomerData other = (CustomerData) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
@Override
public String toString() {
return "CustomerData [id=" + id + ", lastName=" + lastName
+ ", firstName=" + firstName + "]";
}
}
2. Now save read date from data.csv file and store in hashmap
package csvadt;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CSVReader {
public static void main(String[] args) {
Map<String, CustomerData> dataMap = new HashMap<String, CustomerData>();
String csvFile = "F:/csv/data.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] customerDataArr = line.split(cvsSplitBy);
CustomerData data = new CustomerData();
data.setLastName(customerDataArr[0]);
data.setFirstName(customerDataArr[1]);
data.setId(customerDataArr[2]);
dataMap.put(data.getId(), data);
}
System.out.println("data Map: "+dataMap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.