• ArrayList集合中元素的排序


    第一步:实现Comparable接口;
    第二步:重写compareTo()方法;
    第三步:add()-输入数据后,执行Collections.sort(集合对象)。

    代码示例:

    1. package com.zhoulz;
    2. import java.util.*;
    3. /**
    4. * @author zhoulz
    5. * @Description:
    6. */
    7. public class GatewayAddress implements Comparable{
    8. /**
    9. * 属性
    10. */
    11. // 优先级
    12. private int priority;
    13. // 当前负载数
    14. private int CurrLoadNum;
    15. // 最大负载数
    16. private int MaxLoadNum;
    17. public GatewayAddress() {
    18. }
    19. public GatewayAddress(int priority, int CurrLoadNum, int MaxLoadNum) {
    20. this.priority = priority;
    21. this.CurrLoadNum = CurrLoadNum;
    22. this.MaxLoadNum = MaxLoadNum;
    23. }
    24. @Override
    25. public String toString() {
    26. return "GatewayAddress{" +
    27. "priority=" + priority +
    28. ", CurrLoadNum=" + CurrLoadNum +
    29. ", MaxLoadNum=" + MaxLoadNum +
    30. '}';
    31. }
    32. @Override
    33. public int compareTo(GatewayAddress ga) {
    34. //按属性priority升序
    35. return (this.priority > ga.priority ? 1:(this.priority == ga.priority ? 0:-1));
    36. //还可以按其他属性进行排序
    37. }
    38. /**
    39. * 获取网关
    40. * @return gatewayAddresss
    41. */
    42. public GatewayAddress getGatewayAddress(){
    43. /**
    44. * 初始化网关:优先级、当前负载量、最大负载量
    45. */
    46. GatewayAddress gatewayAddress1 = new GatewayAddress(1,50,50);
    47. GatewayAddress gatewayAddress2 = new GatewayAddress(3,15,30);
    48. GatewayAddress gatewayAddress3 = new GatewayAddress(5,5,10);
    49. GatewayAddress gatewayAddress4 = new GatewayAddress(2,40,40);
    50. GatewayAddress gatewayAddress5 = new GatewayAddress(4,10,20);
    51. /**
    52. * 创建网关池:按顺序装入网关
    53. */
    54. ArrayList gateList = new ArrayList<>();
    55. gateList.add(gatewayAddress1);
    56. gateList.add(gatewayAddress2);
    57. gateList.add(gatewayAddress3);
    58. gateList.add(gatewayAddress4);
    59. gateList.add(gatewayAddress5);
    60. Collections.sort(gateList);
    61. System.out.println(gateList);
    62. /**
    63. * 对网关进行负载判断
    64. */
    65. GatewayAddress gatewayAddress = null;
    66. for(int i = 0; i < gateList.size(); i++){
    67. GatewayAddress gate= gateList.get(i);
    68. /*System.out.println("验证是否按排序后的顺序取网关----------");
    69. System.out.println(gate1);*/
    70. if(gate.CurrLoadNum < gate.MaxLoadNum){
    71. System.out.println("当前网关使用成功!");
    72. //gate.loadNumUpdate(EslEvent event);
    73. gatewayAddress = gate;
    74. break;
    75. }
    76. }
    77. if(gatewayAddress == null){
    78. System.out.println("当前所有网关负载已满,无法进行外呼,请2秒钟后重试!");
    79. }
    80. return gatewayAddress;
    81. }
    82. }
    83. class Test{
    84. public static void main(String[] args) {
    85. GatewayAddress gatewayAddress = new GatewayAddress();
    86. System.out.println(gatewayAddress.getGatewayAddress());
    87. }
    88. }

    对于上面,初始化网关并装入网关池的操作,可以进行优化:

    1. // 创建一个存储网关的网关池(ArrayList集合)
    2. private ArrayList gatewayList = new ArrayList<>();
    3. /**
    4. * 初始化5个网关,并装入网关池中
    5. */
    6. Collections.addAll(gatewayList,
    7. new GatewayAddress(1, 45, 50),
    8. new GatewayAddress(3, 25, 30),
    9. new GatewayAddress(5, 5, 10),
    10. new GatewayAddress(2, 35, 40),
    11. new GatewayAddress(4, 15, 20)
    12. );
    13. //排序
    14. Collections.sort(gatewayList);
    15. System.out.println("网关池:" + "\n" + gatewayList);

    其他的排序方法

    代码示例:

    1. package com.zhoulz;
    2. import java.util.ArrayList;
    3. import java.util.Collections;
    4. import java.util.Comparator;
    5. import java.util.List;
    6. import java.util.stream.Collectors;
    7. public class test2_addAll {
    8. private String name;
    9. private int age;
    10. private double height;
    11. public String getName() {
    12. return name;
    13. }
    14. public void setName(String name) {
    15. this.name = name;
    16. }
    17. public int getAge() {
    18. return age;
    19. }
    20. public void setAge(int age) {
    21. this.age = age;
    22. }
    23. public double getHeight() {
    24. return height;
    25. }
    26. public void setHeight(double height) {
    27. this.height = height;
    28. }
    29. public test2_addAll(String name, int age, double height) {
    30. this.name = name;
    31. this.age = age;
    32. this.height = height;
    33. }
    34. @Override
    35. public String toString() {
    36. return "Test2_addAll{" +
    37. "name='" + name + '\'' +
    38. ", age=" + age +
    39. ", height=" + height +
    40. '}';
    41. }
    42. public static void main(String[] args) {
    43. // 集合元素为数字时
    44. List numbers = new ArrayList<>();
    45. Collections.addAll(numbers,1,3,2,6,4,8,7,9);
    46. // 利用集合的工具类Collections类来对集合中的元素进行排序
    47. // Collections.sort(numbers);
    48. // 或者
    49. // 使用java8新特性中的stream,将ArrayList集合中的元素流化实现排序
    50. numbers = numbers.stream().sorted(Integer::compareTo).collect(Collectors.toList());
    51. System.out.println("numbers:"+numbers.toString());
    52. // 集合元素为字符串时
    53. List strNumbers = new ArrayList<>();
    54. Collections.addAll(strNumbers,"1","3","2","6","4","8","7","9");
    55. Collections.sort(strNumbers);
    56. System.out.println("strNumbers:"+strNumbers.toString());
    57. // 如果List集合中的元素是自定义的数据类型
    58. List list = new ArrayList<>();
    59. Collections.addAll(list,
    60. new test2_addAll("aaa",10,150.0),
    61. new test2_addAll("ccc",30,170.0),
    62. new test2_addAll("eee",50,190.0),
    63. new test2_addAll("bbb",20,210.0),
    64. new test2_addAll("ddd",40,230.0)
    65. );
    66. System.out.println(list);
    67. //按age进行排序
    68. list =list
    69. .stream()
    70. .sorted(Comparator.comparing(test2_addAll::getAge))
    71. //.sorted(Comparator.comparing(Test2_addAll::getAge).reversed()) 加上reversed()方法就是逆序排序
    72. .collect(Collectors.toList());
    73. System.out.println("list:"+ list.toString());
    74. //按height进行排序
    75. list =list
    76. .stream()
    77. .sorted(Comparator.comparing(test2_addAll::getHeight))
    78. //.sorted(Comparator.comparing(Test2_addAll::getAge).reversed()) 加上reversed()方法就是逆序排序
    79. .collect(Collectors.toList());
    80. System.out.println("list:"+ list.toString());
    81. //按name进行排序
    82. list =list
    83. .stream()
    84. .sorted(Comparator.comparing(test2_addAll::getName))
    85. //.sorted(Comparator.comparing(Test2_addAll::getAge).reversed()) 加上reversed()方法就是逆序排序
    86. .collect(Collectors.toList());
    87. System.out.println("list:"+ list.toString());
    88. }
    89. }
  • 相关阅读:
    OpenAI开发者大会:定义未来AI的新功能、愿景和商业版图
    八大排序java
    急诊与灾难医学-题库及答案
    Java爬虫实战系列2——动手写爬虫初体验
    基于springboot医院碳排放管理平台设计与实现-计算机毕业设计源码+LW文档
    Faiss原理和使用总结
    Java多线程(7):JUC(上)
    运动无线耳机什么牌子好?六款实用性不错的运动耳机推荐
    MaxKey单点登录认证系统v3.5.10GA发布
    docker介绍、安装及卸载
  • 原文地址:https://blog.csdn.net/zhoulizhengZ/article/details/128036809