桶排序或分选对象的基于ID的ArrayList? [英] Bucket Sorting or sorting an arraylist of objects based on Id?

查看:191
本文介绍了桶排序或分选对象的基于ID的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过关于桶排序。难道任何一个说明它是如何给我们整理数百万条记录的最佳效果?是否与 N *日志中的任何算法(N) O(N)
 ?

I've heard about the Bucket Sorting. Could any one clarify how it gives the best performance when we sort millions of records? Is there any algorithm with n*log(n) or O(n) ?

我有1000个员工的对象,每个员工对象都有编号,姓名,薪金性质。我说这些对象的ArrayList 我想基于id属性排序这些对象。反正是有?使用 Col​​lections.sort()方法了。下面的解决方案考虑,请人帮助其中一个给了最好的性能?

I have 1000 employee objects each employee object has id, name, salary properties. I added these objects to ArrayList I want to sort these objects based on id property. Is there anyway? With out using Collections.sort() method. Considered below solutions, please anybody help which one gives best performance?

感谢

推荐答案

无Collections.sort():

首先实施可比<员工> 在Empoloyee的类并覆盖的compareTo

First implement Comparable<Employee> in Empoloyee's class and override compareTo

@Override
public int compareTo(Employee o) {

    return this.id.compareTo(o.id);
}

通过你的无序列表 TreeSet的并获得设置(上排序ID),然后创建新的List 设置

Pass your unsorted list to TreeSet and get set(sorted on id) then create new List with this set

List<Employee> list=new ArrayList<Employee>();
    list.add(new Employee(1, "A", Double.parseDouble("50")));
    list.add(new Employee(22, "B", Double.parseDouble("11")));
    list.add(new Employee(3, "C", Double.parseDouble("222")));
    list.add(new Employee(34, "D", Double.parseDouble("4")));

    SortedSet<Employee> set=new TreeSet<Employee>( list);

    List<Employee> l=new ArrayList<Employee>();
    l.addAll(set);

    System.out.println(l);

输出:对编号的顺序排列,而不 Col​​lections.sort()

[Employee [id=1, name=A, price=50.0], Employee [id=3, name=C, price=222.0], Employee [id=22, name=B, price=11.0], Employee [id=34, name=D, price=4.0]]

编辑:

Employee类:

class Employee implements Comparable<Employee>{

Integer id;
String name;
Double price;
       -------

}

这篇关于桶排序或分选对象的基于ID的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆