当前位置:   article > 正文

java中的各种集合排序_private string id

private string id

一、背景

1.软件开发过程中集合排序是比较强大的功能,会使用集合Map、Set、List实现排序功能,知道匿名内部类Comparator很关键,搞清楚集合排序的性能开销,排序遇到的坑以及解决的方法,注意下面的例子都是JDK1.8的用法。

二、LIst集合排序

1.UML类图

2.重点分析下ArrayList的排序,毕竟实战开发用的最频繁的就是它了

三、第一种做法

介绍:这种叫定制排序,或自定义排序,需编写匿名内部类,先new一个Comparator接口的比较器对象c,同时实现compare()其方法; 
然后将比较器对象c传给Collections.sort()方法的参数列表中,实现排序功能;一般用这种的比较多。

1.实体类(private int id)

  1. public class Person{
  2. private int id;
  3. private String name;
  4. private String address;
  5. public Person(int id, String name, String address) {
  6. this.id = id;
  7. this.name = name;
  8. this.address = address;
  9. }
  10. public int getId() {
  11. return id;
  12. }
  13. public void setId(int id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getAddress() {
  23. return address;
  24. }
  25. public void setAddress(String address) {
  26. this.address = address;
  27. }
  28. @Override
  29. public String toString() {
  30. return "Person{" +
  31. "id='" + id + '\'' +
  32. ", name='" + name + '\'' +
  33. ", address='" + address + '\'' +
  34. '}';
  35. }
  36. }

2.测试类

  1. public class ArrayListTest {
  2. public static void main(String[] args) {
  3. List<Person> list=Lists.newArrayList();
  4. //产生10以内的随机数
  5. int num = (int)(Math.random()*1000+1);
  6. for(int i=num;i>0;i--){
  7. list.add(new Person(i,"张三","河南"));
  8. }
  9. for(Object o : list){
  10. System.out.println(o);
  11. }
  12. System.out.println("++++++++++++++++++++++++++++++++++++");
  13. Collections.sort(list, new Comparator<Person>() {
  14. @Override
  15. public int compare(Person o1, Person o2) {
  16. if (o1.getId() > o2.getId()) {
  17. return 1;
  18. } else if (o1.getId() < o2.getId()) {
  19. return -1;
  20. }
  21. return 0;
  22. }
  23. });
  24. for(Object o : list){
  25. System.out.println(o);
  26. }
  27. }
  28. }

3.结果(升序)

  1. Person{id='10', name='张三', address='河南'}
  2. Person{id='9', name='张三', address='河南'}
  3. Person{id='8', name='张三', address='河南'}
  4. Person{id='7', name='张三', address='河南'}
  5. Person{id='6', name='张三', address='河南'}
  6. Person{id='5', name='张三', address='河南'}
  7. Person{id='4', name='张三', address='河南'}
  8. Person{id='3', name='张三', address='河南'}
  9. Person{id='2', name='张三', address='河南'}
  10. Person{id='1', name='张三', address='河南'}
  11. ++++++++++++++++++++++++++++++++++++
  12. Person{id='1', name='张三', address='河南'}
  13. Person{id='2', name='张三', address='河南'}
  14. Person{id='3', name='张三', address='河南'}
  15. Person{id='4', name='张三', address='河南'}
  16. Person{id='5', name='张三', address='河南'}
  17. Person{id='6', name='张三', address='河南'}
  18. Person{id='7', name='张三', address='河南'}
  19. Person{id='8', name='张三', address='河南'}
  20. Person{id='9', name='张三', address='河南'}
  21. Person{id='10', name='张三', address='河南'}

四、第二中做法

1.实体类(private String id)

  1. public class Person{
  2. private String id;
  3. private String name;
  4. private String address;
  5. public Person(String id, String name, String address) {
  6. this.id = id;
  7. this.name = name;
  8. this.address = address;
  9. }
  10. public String getId() {
  11. return id;
  12. }
  13. public void setId(String id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getAddress() {
  23. return address;
  24. }
  25. public void setAddress(String address) {
  26. this.address = address;
  27. }
  28. @Override
  29. public String toString() {
  30. return "Person{" +
  31. "id='" + id + '\'' +
  32. ", name='" + name + '\'' +
  33. ", address='" + address + '\'' +
  34. '}';
  35. }
  36. }

2.测试类

  1. public class ArrayListTest {
  2. public static void main(String[] args) {
  3. List<Person> list=Lists.newArrayList();
  4. //产生10以内的随机数
  5. int num = (int)(Math.random()*100+1);
  6. for(int i=num;i>0;i--){
  7. list.add(new Person(""+i,"张三","河南"));
  8. }
  9. for(Object o : list){
  10. System.out.println(o);
  11. }
  12. System.out.println("++++++++++++++++++++++++++++++++++++");
  13. Collections.sort(list, new Comparator<Person>() {
  14. @Override
  15. public int compare(Person o1, Person o2) {
  16. return new Double(o1.getId()).compareTo(new Double(o2.getId()));
  17. }
  18. });
  19. for(Object o : list){
  20. System.out.println(o);
  21. }
  22. }
  23. }

3.结果

  1. Person{id='10', name='张三', address='河南'}
  2. Person{id='9', name='张三', address='河南'}
  3. Person{id='8', name='张三', address='河南'}
  4. Person{id='7', name='张三', address='河南'}
  5. Person{id='6', name='张三', address='河南'}
  6. Person{id='5', name='张三', address='河南'}
  7. Person{id='4', name='张三', address='河南'}
  8. Person{id='3', name='张三', address='河南'}
  9. Person{id='2', name='张三', address='河南'}
  10. Person{id='1', name='张三', address='河南'}
  11. ++++++++++++++++++++++++++++++++++++
  12. Person{id='1', name='张三', address='河南'}
  13. Person{id='2', name='张三', address='河南'}
  14. Person{id='3', name='张三', address='河南'}
  15. Person{id='4', name='张三', address='河南'}
  16. Person{id='5', name='张三', address='河南'}
  17. Person{id='6', name='张三', address='河南'}
  18. Person{id='7', name='张三', address='河南'}
  19. Person{id='8', name='张三', address='河南'}
  20. Person{id='9', name='张三', address='河南'}
  21. Person{id='10', name='张三', address='河南'}

五、第三种做法

另外一种称为自然排序,参与排序的对象需实现comparable接口,重写其compareTo()方法,方法体中实现对象的比较大小规则。

1.实体类

  1. public class Person implements Comparable{
  2. private String id;
  3. private String name;
  4. private String address;
  5. public Person(String id, String name, String address) {
  6. this.id = id;
  7. this.name = name;
  8. this.address = address;
  9. }
  10. public String getId() {
  11. return id;
  12. }
  13. public void setId(String id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getAddress() {
  23. return address;
  24. }
  25. public void setAddress(String address) {
  26. this.address = address;
  27. }
  28. @Override
  29. public String toString() {
  30. return "Person{" +
  31. "id='" + id + '\'' +
  32. ", name='" + name + '\'' +
  33. ", address='" + address + '\'' +
  34. '}';
  35. }
  36. @Override
  37. public int compareTo(Object o) {
  38. if (o instanceof Person){
  39. Person o1 = (Person)o;
  40. return new Double(this.getId()).compareTo(new Double(o1.getId()));
  41. }
  42. throw new ClassCastException("不能转换为Person类型的对象...");
  43. }
  44. }

2.测试类

  1. public class ArrayListTest {
  2. public static void main(String[] args) {
  3. List<Person> list=Lists.newArrayList();
  4. //产生10以内的随机数
  5. int num = (int)(Math.random()*100+1);
  6. for(int i=num;i>0;i--){
  7. list.add(new Person(""+i,"张三","河南"));
  8. }
  9. for(Object o : list){
  10. System.out.println(o);
  11. }
  12. System.out.println("++++++++++++++++++++++++++++++++++++");
  13. Collections.sort(list);
  14. for(Object o : list){
  15. System.out.println(o);
  16. }
  17. }
  18. }

3.结果

  1. Person{id='10', name='张三', address='河南'}
  2. Person{id='9', name='张三', address='河南'}
  3. Person{id='8', name='张三', address='河南'}
  4. Person{id='7', name='张三', address='河南'}
  5. Person{id='6', name='张三', address='河南'}
  6. Person{id='5', name='张三', address='河南'}
  7. Person{id='4', name='张三', address='河南'}
  8. Person{id='3', name='张三', address='河南'}
  9. Person{id='2', name='张三', address='河南'}
  10. Person{id='1', name='张三', address='河南'}
  11. ++++++++++++++++++++++++++++++++++++
  12. Person{id='1', name='张三', address='河南'}
  13. Person{id='2', name='张三', address='河南'}
  14. Person{id='3', name='张三', address='河南'}
  15. Person{id='4', name='张三', address='河南'}
  16. Person{id='5', name='张三', address='河南'}
  17. Person{id='6', name='张三', address='河南'}
  18. Person{id='7', name='张三', address='河南'}
  19. Person{id='8', name='张三', address='河南'}
  20. Person{id='9', name='张三', address='河南'}
  21. Person{id='10', name='张三', address='河南'}

六、结束

今天就写到这吧,太困了,晚安!各位,希望能帮到你们。

Always keep the faith!!!

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/440064
推荐阅读
相关标签
  

闽ICP备14008679号