我想按名称和按ids的地点类别对以下类别进行排序:-1,开始时间为0 。
public class Category {
Long id;
String name;
public Category(Long id, String name) {
this.id=id;
this.name=name;
}
}
Set<Categories> categories = new HashSet<>;
categories.add(new Category(2,"Drama"))
categories.add(new Category(-1,"Favorites"))
categories.add(new Category(3,"Cartoons"))
categories.add(new Category(1,"Biography"))
categories.add(new Category(0,"All"))
如何获得此类列表?
[{-1,"Favorites"}, {0,"All"}, {3,"Cartoons"}, {1,"Drama"}, {2,"Historical"}]
P.S。我有 Guava lib
答案 0 :(得分:2)
使用TreeSet而不是HashSet,让Category实现Comparable(并根据需要实现compareTo)。 TreeSet将存储一组有序的Category对象。
public class Category implements Comparable<Category>
{
private final int id;
private final String name;
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public Category(int id, String name)
{
this.id = id;
this.name = name;
}
@Override
public int compareTo(Category category)
{
return this.getId() - category.getId();
}
public static void main(String[] args)
{
Set<Category> categories = new TreeSet<>();
categories.add(new Category(2, "Drama"));
categories.add(new Category(-1, "Favorites"));
categories.add(new Category(3, "Cartoons"));
categories.add(new Category(1, "Biography"));
categories.add(new Category(0, "All"));
for (Category category : categories)
{
System.out.println(category.getId());
}
}
}
答案 1 :(得分:1)
将其列为清单并对其进行排序:
Collections.sort(categories, (a, b) -> {
if (a.id != b.id) {
if (a.id == -1) {
return -1;
}
if (b.id == -1) {
return 1;
}
if (a.id == 0) {
return -1;
}
if (b.id == 0) {
return 1;
}
}
return a.name.compareTo(b.name);
});
答案 2 :(得分:0)
首先,您的代码存在问题。 HashSet通过首先检查对象的hashCode()然后通过调用equals()来确保它是同一个对象来检查对象是否已经存在。如果你没有定义这两个函数,那么无论你输入什么值,所有的Category对象都会有所不同。
其次,要对对象进行排序,最好通过实现使它们具有可比性 可比&LT;类别&GT;
最后,HashSet不会保留对象的任何顺序。如果您使用TreeSet而不是HashSet,您将自然地获得订单。
答案 3 :(得分:0)
HashSet没有Ordered Set。
如果您需要Ordered Set,则使用java.util.TreeSet。
要使用java.util.TreeSet,您必须更改implements java.lang.Comparable的Category,并在java.util.TreeSet构造函数中实现方法compareTo以提供比较方法或java.util.Comparator。
使用比较器的示例:
用法:
Set<Category> categories = new TreeSet<>(new Comparator<Category>() {
@Override
public int compare(Category o1, Category o2) {
return o1.getId().compareTo(o2.getId());
}
});
categories.add(new Category(2, "Drama"));
categories.add(new Category(-1, "Favorites"));
categories.add(new Category(3, "Cartoons"));
categories.add(new Category(1, "Biography"));
categories.add(new Category(0, "All"));
Comparable示例:
Category.java
public class Category implements Comparable<Category> {
private Integer id;
private String name;
public Category() {
}
public Category(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Category o) {
return id.compareTo(o.id);
}
}
用法:
Set<Category> categories = new TreeSet<>();
categories.add(new Category(2, "Drama"));
categories.add(new Category(-1, "Favorites"));
categories.add(new Category(3, "Cartoons"));
categories.add(new Category(1, "Biography"));
categories.add(new Category(0, "All"));