Java 通过对象属性将对象添加到集合和顺序的有效方法。
我正在尝试构建一个选择菜单对象。我正在使用hibernate标准构建我的对象列表。我正在尝试获取联接表对象并将其添加到集合中以消除重复项。然后,我想按照对象中的属性对集合进行排序,一旦排序,我需要将其添加到arraylist,最后在列表的末尾添加一个额外的条目 这是我到目前为止所做的Java 通过对象属性将对象添加到集合和顺序的有效方法。,java,hibernate,Java,Hibernate,我正在尝试构建一个选择菜单对象。我正在使用hibernate标准构建我的对象列表。我正在尝试获取联接表对象并将其添加到集合中以消除重复项。然后,我想按照对象中的属性对集合进行排序,一旦排序,我需要将其添加到arraylist,最后在列表的末尾添加一个额外的条目 这是我到目前为止所做的 public class Computer { private String name; //other properties getter/setter } 选择菜单查询 public Li
public class Computer {
private String name;
//other properties getter/setter
}
选择菜单查询
public List<Computer> getComputerList() {
//Hibernate critera query
List<ComputerConfiguration> results = session.criteraQuery(ComputerConfiguration.class).add(Restrictions.eq("processor", "amd")).list();
Set<Computer> computerSet = HashSet();
//Get joined object and add to set to remove duplicates
for(ComputerConfiguration computerConfiguration : results) {
computerSet.add(computerConfiguration.getComputer());
}
List<Computer> computers = new ArrayList<>(computerSet);
//Need to somehow order by computer.getName();
//Lastly add the following
computers.add("Other");
//Return computers to select model
return computers;
}
公共列表getComputerList(){
//Hibernate critera查询
List results=session.criteraQuery(ComputerConfiguration.class).add(Restrictions.eq(“processor”,“amd”)).List();
Set computerSet=HashSet();
//获取联接对象并添加到集合以删除重复项
用于(计算机配置计算机配置:结果){
computerSet.add(computerConfiguration.getComputer());
}
列表计算机=新阵列列表(计算机集);
//需要通过计算机以某种方式订购。getName();
//最后添加以下内容
计算机。添加(“其他”);
//返回计算机以选择模型
归还电脑;
}
删除重复对象并按属性名称排序的最有效方法是什么?我刚开始尝试对critera查询进行排序,但是集合似乎没有保持其位置
谢谢。使用将对其中的元素进行排序的。创建集合时,您可以提供自定义的比较器
:
Set<Computer> computerSet = new TreeSet<Computer>(new Comparator<Computer>() {
@Override
public int compare(Computer computer1, Computer computer2) {
//write the logic to define which computer is greater than the other...
}
});
Set computerSet=新树集(新比较器(){
@凌驾
公共整数比较(计算机1、计算机2){
//编写逻辑以定义哪台计算机比另一台计算机大。。。
}
});
请注意,您的计算机
类不需要实现equals
或hashCode
,就可以在树集
中使用,它将对其中的元素进行排序。创建集合时,您可以提供自定义的比较器
:
Set<Computer> computerSet = new TreeSet<Computer>(new Comparator<Computer>() {
@Override
public int compare(Computer computer1, Computer computer2) {
//write the logic to define which computer is greater than the other...
}
});
Set computerSet=新树集(新比较器(){
@凌驾
公共整数比较(计算机1、计算机2){
//编写逻辑以定义哪台计算机比另一台计算机大。。。
}
});
请注意,您的
计算机
类不需要实现equals
或hashCode
,就可以在树集
中使用HashSet
来确保计算机是唯一的(就像您当前正在做的那样)很好-假设您已经适当地实现了hashCode
和equals
要按名称对计算机的列表进行排序,只需编写
Collections.sort(computers, new Comparator<Computer>()
{
@Override
public int compare(Computer c0, Computer c1)
{
return c0.getName().compareTo(c1.getName());
}
});
Collections.sort(计算机、新比较器()
{
@凌驾
公共整数比较(计算机c0、计算机c1)
{
返回c0.getName().compareTo(c1.getName());
}
});
虽然用TreeSet
解决这个问题很有诱惑力(因为它可以在一次运行中排序和删除重复项),但您应该仔细考虑您的比较标准是否与TreeSet
和Comparator
的JavaDoc中描述的等式一致:
比较器c对一组元素S施加的顺序称为与equals一致,当且仅当c.compare(e1,e2)==0具有与e1相同的布尔值。S中的每个e1和e2都具有equals(e2)
()
这里的关键问题是:是否有两台计算机具有相同的名称(由getName()
返回),但仍应而不是视为“相等”
但是,如果排序标准与equals一致,则使用带有适当比较器的TreeSet
是可行的。使用HashSet
以确保计算机是唯一的(就像您当前正在做的那样)很好-假设您已经适当地实现了hashCode
和equals
要按名称对计算机的列表进行排序,只需编写
Collections.sort(computers, new Comparator<Computer>()
{
@Override
public int compare(Computer c0, Computer c1)
{
return c0.getName().compareTo(c1.getName());
}
});
Collections.sort(计算机、新比较器()
{
@凌驾
公共整数比较(计算机c0、计算机c1)
{
返回c0.getName().compareTo(c1.getName());
}
});
虽然用TreeSet
解决这个问题很有诱惑力(因为它可以在一次运行中排序和删除重复项),但您应该仔细考虑您的比较标准是否与TreeSet
和Comparator
的JavaDoc中描述的等式一致:
比较器c对一组元素S施加的顺序称为与equals一致,当且仅当c.compare(e1,e2)==0具有与e1相同的布尔值。S中的每个e1和e2都具有equals(e2)
()
这里的关键问题是:是否有两台计算机具有相同的名称(由getName()
返回),但仍应而不是视为“相等”
但是,如果排序标准与equals一致,则使用带有适当比较器的TreeSet
是可行的。在填充集合时,您可以尝试使用SortedSet对计算机进行实际排序:
还有,我不太懂你的台词
computers.add("Other")
计算机是一个计算机对象数组,它可能无法工作。下面是我的完整解决方案:
首先,使计算机对象具有可比性:
public class Computer implements Comparable<Computer> {
private String name;
public Computer (String name) { this.name = name; }
public int compareTo(Computer c) {
return name.compareTo(c.name) ;
}
}
公共类计算机{
私有字符串名称;
公用计算机(字符串名){this.name=name;}
公共整数比较(计算机c){
返回name.compareTo(c.name);
}
}
您现在可以使用如下所示的SortedSet:
public List<Computer> getComputerList() {
List<ComputerConfiguration> results = session.criteraQuery(ComputerConfiguration.class).add(Restrictions.eq("processor", "amd")).list();
SortedSet<Computer> computerSet = new TreeSet<>();
for(ComputerConfiguration computerConfiguration : results) {
computerSet.add(computerConfiguration.getComputer());
}
computerSet.add(new Computer("Other");
List<Computer> computers = new ArrayList<>(computerSet);
return computers;
}
公共列表getComputerList(){
List results=session.criteraQuery(ComputerConfiguration.class).add(Restrictions.eq(“processor”,“amd”)).List();
SortedSet computerSet=新树集();
用于(计算机配置计算机配置:结果){