在Hashmap中对具有Null值的对象列表进行排序

时间:2018-07-10 14:53:00

标签: java algorithm sorting data-structures collections

我必须根据对象内部的hashMap值对对象进行排序。 对象就像: Employee [getId,getName,getAge属性:{index,height,weight等。}] 我想根据哈希图中可用的索引值对Employee进行排序。虽然对于索引值,Null可能会在某个时候出现,所以我也需要处理。

我正在尝试下面的代码进行排序:

Comparator<GNode> com = Comparator.comparing(Employee::getProperty(Constans.INDEX), Comparator.nullsFirst(Comparator.naturalOrder()));

但这给了编译时错误器。

Employee类类似于:

public class Employee  
{
private UUID id;
private String name;
private int age;

public Employee(UUID id, String name, int age)
{
    this.id = id;
    this.name = name;
    this.age = age;
}

public UUID getId()
{
    return id;
}

public void setId(UUID id)
{
    this.id = id;
}

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}    

public int getAge()
{
    return age;
}

public void setAge(int age)
{
    this.age = age;
}    
public Object getProperty(String key) {
    return properties.get(key);
}

public Object getProperty(String key, Object defValue) {
    Object ret = properties.get(key);
    return  (ret != null) ? ret : defValue;
}

public void addProperty(String key, Object value) {
    properties.put(key, value);
}
}     

请帮助我解决此问题。
非常感谢您

1 个答案:

答案 0 :(得分:0)

您可以创建Comparator的实现并制定规则,

package com.ramiresmoreira;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

public class Employee {
private UUID id;
private String name;
private int age;

public Employee(UUID id, String name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
}

public UUID getId() {
    return id;
}

public void setId(UUID id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public static void main(String[] args) {
    //Employee with name equal null
    Employee e1 = new Employee(UUID.randomUUID(), "Name 1", 22);
    Employee e2 = new Employee(UUID.randomUUID(), null, 19);
    Set<Employee> employees = new HashSet<>();
    employees.add(e1);
    employees.add(e2);
    List<Employee> sorted = employees.stream().sorted(new EmployeeSortNullFirst()).collect(Collectors.toList());
    System.out.println(sorted);
}

@Override
public String toString() {
    return "name=" + name ;
}
}

class EmployeeSortNullFirst implements Comparator<Employee> {

@Override
public int compare(Employee e1, Employee e2) {
    if(e1 == null ||  e2 == null) {
        return 1;
    }
    return Integer.compare(getHash(e1), getHash(e2));
}

private int getHash(Employee e) {
    return hashAge(e) + hasName(e) + hashId(e);
} 

private int hasName(Employee e) {
    return e.getName() != null ? e.getName().hashCode() : Integer.MAX_VALUE;
}

private int hashAge(Employee e) {
    return e.getAge();
}

private int hashId(Employee e) {
    return e.getId() != null ? e.getId().hashCode() : Integer.MAX_VALUE;
}
}