问题:项目中获得的部门属性employeeSet是set类型,想要把他转成list类型, 并有序输出出来
/* 获取set集合 */
Set<Employee> employeeSet = department.getEmployeeSet();
/* 将Set集合转为List,这样获得的list并不能有序排列*/
List<Employee> employeeList = new ArrayList<Employee>(employeeSet);
/*将list有序排列*/
Collections.sort(employeeList, new Comparator<Employee>() {
public int compare(Employee arg0, Employee arg1) {
return arg0.getEmployeeId().compareTo(arg1.getEmployeeId()); // 按照id排列
}
});
转载请注明出处:http://noblebaron.iteye.com/blog/1632220
本文介绍了一种将Java中Set集合转换为List的方法,并通过Collections.sort()方法对List进行排序的过程。具体步骤包括使用ArrayList构造函数直接转换Set为List,然后自定义比较器Comparator来实现特定字段(如ID)的排序。
1034

被折叠的 条评论
为什么被折叠?



