如何对键是模型的hashmap进行排序?

时间:2017-03-28 10:20:16

标签: java sorting collections hashmap

我有Hashmap例如

Map<EmailCategoryModel, List<EmailBlogDetailsModel>> map;

如何根据键的某些属性对Map进行排序?

1 个答案:

答案 0 :(得分:0)

使用TreeMap,它是一个有序的映射,然后使用比较器或类似的接口。 代码:

// Use TreeMap like this

TreeMap<EmailCategoryModel, EmailBlogDetailsModel> map = new TreeMap<>(new ComparatorNew());

// And create comparator somewhat like this
class ComparatorNew implements Comparator<EmailCategoryModel> {

    @Override
    public int compare(EmailCategoryModel o1, EmailCategoryModel o2) {
        // Do sorting based on your model attribute here.
        return 0;
    }

}