Consider the following question on storing values with duplicate keys:
Suppose there is a class Employee with name, sal and dob as attributes. I want to store the objects of Employee in a Map and the key would be the Employee开发者_运维百科 name. The name can be duplicate.
Also after adding 10 objects in the Map. I want to retrieve the 8th object that was entered.
This is one solution to add objects with duplicate keys but for the 2nd part of the question, this would not work since on displaying the map, all values with the same key will be displayed together.
How would we maintain the order in which the objects were added in this situation? Can we modify equals and hashcode methods to somehow add the elements and then later retrieve them in the order in which they were inserted?
I think a LinkedHashMultimap (from Guava) should work for this. You wouldn't be able to get the 8th entry by index directly, but you could use something like Iterables.get(Iterable iterable, int position) to get it.
Why not just have two containers? One for mapping name to employee (like the one in the stackoverflow question you mentioned), another for mapping number to employee. You can make an "outer" container aggregating multimap and arraylist.
What you intend to do can be easily implemented using an ArrayList
. This is the data structure that you should use.
The requirements are somehow contradictory. At one side several values should be possible for one key, at the other side only one value should be returned for a key. Additionaly, retrievals for a sequence should be possible. I see the nearest approximation in designing a dedicated data structure containing a hash map for fast access based on the name, and a list keeping the order of insertions. The access would be based on the overall sequence number or on the name plus index for the name. The implementation would be according the following lines:
public class Employee {
public String name; public int sal;
public Employee() {name = ""; sal = 0;}
public Employee(String name, int sal) {
this.name = name; this.sal = sal;
}
@Override public String toString() {return "(" + name + "," + sal + ")";}
}
public class Team {
private Map<String, ArrayList<Employee>> employees =
new HashMap<String, ArrayList<Employee>>();
private ArrayList<Employee> order = new ArrayList<Employee>();
public void addEmployee(Employee e) {
ArrayList<Employee> list = employees.get(e.name);
if (list == null) {
list = new ArrayList<Employee>();
employees.put(e.name, list);
}
list.add(e);
order.add(e);
}
public int getNumEmployees() {return order.size();}
public Employee getEmployee(int n) {return order.get(n - 1);}
public int getNumEmployees(String name) {
ArrayList<Employee> list = employees.get(name);
return list == null ? 0 : list.size();
}
public Employee getEmployee(String name, int n) {
ArrayList<Employee> list = employees.get(name);
return list == null ? null : list.get(n - 1);
}
}
// Test:
Team team = new Team();
team.addEmployee(new Employee("Bob", 11));
team.addEmployee(new Employee("Bob", 12));
team.addEmployee(new Employee("Eve", 13));
team.addEmployee(new Employee("Eve", 14));
System.out.println("Num all: " + team.getNumEmployees());
System.out.println("3rd: " + team.getEmployee(3));
System.out.println("Num Bobs: " + team.getNumEmployees("Bob"));
System.out.println("2nd Bob: " + team.getEmployee("Bob", 2));
精彩评论