Amar
Amar

Reputation: 971

Sorting map containing a key and list of objects

I have a map with the structure as Map<String, List<EmployeeIncome>> where the key holds the employee Id and the Employee Income holds the employee details such as empName, empCompany,incomeSrc.The source of income might be multiple (salary , house let out for rent etc).

public class EmployeeIncome{
     private String empName;
     private String empCompany;
     private String incomeSrc;


    public String getEmpName() {
        return empName;
     }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public String getEmpCompany() {
        return empCompany;
    }
    public void setEmpCompany(String empCompany) {
        this.empCompany = empCompany;
    }
    public String getIncomeSrc() {
        return incomeSrc;
    }
    public void setIncomeSrc(String incomeSrc) {
        this.incomeSrc = incomeSrc;
    }



 }

The employee company name will be unique for an employee id will be the same in all case of income source.Now maps contains multiple data of employee, i would like to sort the map based on employee name.Though I am trying to use Comparator and trying out but not able to get the desired output.

Thanks in advance.

Upvotes: 0

Views: 286

Answers (3)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

A sorting map like TreeMap would sort based on its keys, not the values being assigned to them. So, since your keys are employee id strings, you'll get your output sorted by id only.

You should probably rethink your data structure. It's better to map an Employee object as the key. Then you would have access to both, the employee's name as well as his id.

public class Employee implements Comparable<Employee> {

    private long id;
    private String name;

    public long getId() {
        return id;
    }

    public long getName() {
        return name;
    }

    public int compareTo(Employee other) {
      if (name != null) {
        if (other != null) {
          return name.compareTo(other.getName());
        }
        return 1;
      }
      if (other != null && other.getName() != null) {
         return -1;
      }
      return 0;
}

You would also implement Comparable so that the TreeMap sorts your employees by their names.

Upvotes: 2

Beri
Beri

Reputation: 11600

Your are asking for wrong data structure. Most map implementations have unsorted keys.

Only exception - TreeMap is sorging keys only. In your example key is employee id, not their name. If you want to have a sorted list by employee names, you would have to extract all employees from your map, put them to a list and use comparator that compares empName.

In short your map will always be sorted by employee id, because it is this map key.

Upvotes: 1

ha9u63a7
ha9u63a7

Reputation: 6804

Because you haven't shown what you have done so far, I am simply going to suggest things. You can use a TreeMap<EmployeeName, OtherValue> where EmployeeName is unique as you said? TreeMap is an implementation of NavigableMap (special case of SortedMap where navigational features such as nearest match floor, ceil, etc.) are allowed.

Also, Your keys will be ordered using "Natural ordering". E.g. if your EmployeeName is in String, they will be ordered lexicographical.

Upvotes: 1

Related Questions