第一步:实现Comparable接口;
第二步:重写compareTo()方法;
第三步:add()-输入数据后,执行Collections.sort(集合对象)。
代码示例:
- package com.zhoulz;
-
- import java.util.*;
-
- /**
- * @author zhoulz
- * @Description:
- */
- public class GatewayAddress implements Comparable
{ - /**
- * 属性
- */
- // 优先级
- private int priority;
- // 当前负载数
- private int CurrLoadNum;
- // 最大负载数
- private int MaxLoadNum;
-
- public GatewayAddress() {
- }
-
- public GatewayAddress(int priority, int CurrLoadNum, int MaxLoadNum) {
- this.priority = priority;
- this.CurrLoadNum = CurrLoadNum;
- this.MaxLoadNum = MaxLoadNum;
- }
-
- @Override
- public String toString() {
- return "GatewayAddress{" +
- "priority=" + priority +
- ", CurrLoadNum=" + CurrLoadNum +
- ", MaxLoadNum=" + MaxLoadNum +
- '}';
- }
-
- @Override
- public int compareTo(GatewayAddress ga) {
- //按属性priority升序
- return (this.priority > ga.priority ? 1:(this.priority == ga.priority ? 0:-1));
- //还可以按其他属性进行排序
- }
-
- /**
- * 获取网关
- * @return gatewayAddresss
- */
- public GatewayAddress getGatewayAddress(){
- /**
- * 初始化网关:优先级、当前负载量、最大负载量
- */
- GatewayAddress gatewayAddress1 = new GatewayAddress(1,50,50);
- GatewayAddress gatewayAddress2 = new GatewayAddress(3,15,30);
- GatewayAddress gatewayAddress3 = new GatewayAddress(5,5,10);
- GatewayAddress gatewayAddress4 = new GatewayAddress(2,40,40);
- GatewayAddress gatewayAddress5 = new GatewayAddress(4,10,20);
-
- /**
- * 创建网关池:按顺序装入网关
- */
- ArrayList
gateList = new ArrayList<>(); - gateList.add(gatewayAddress1);
- gateList.add(gatewayAddress2);
- gateList.add(gatewayAddress3);
- gateList.add(gatewayAddress4);
- gateList.add(gatewayAddress5);
-
- Collections.sort(gateList);
- System.out.println(gateList);
-
- /**
- * 对网关进行负载判断
- */
- GatewayAddress gatewayAddress = null;
-
- for(int i = 0; i < gateList.size(); i++){
- GatewayAddress gate= gateList.get(i);
- /*System.out.println("验证是否按排序后的顺序取网关----------");
- System.out.println(gate1);*/
-
- if(gate.CurrLoadNum < gate.MaxLoadNum){
- System.out.println("当前网关使用成功!");
- //gate.loadNumUpdate(EslEvent event);
- gatewayAddress = gate;
- break;
- }
- }
- if(gatewayAddress == null){
- System.out.println("当前所有网关负载已满,无法进行外呼,请2秒钟后重试!");
- }
- return gatewayAddress;
- }
- }
-
- class Test{
- public static void main(String[] args) {
- GatewayAddress gatewayAddress = new GatewayAddress();
- System.out.println(gatewayAddress.getGatewayAddress());
- }
- }
对于上面,初始化网关并装入网关池的操作,可以进行优化:
- // 创建一个存储网关的网关池(ArrayList集合)
- private ArrayList
gatewayList = new ArrayList<>(); - /**
- * 初始化5个网关,并装入网关池中
- */
- Collections.addAll(gatewayList,
- new GatewayAddress(1, 45, 50),
- new GatewayAddress(3, 25, 30),
- new GatewayAddress(5, 5, 10),
- new GatewayAddress(2, 35, 40),
- new GatewayAddress(4, 15, 20)
- );
-
- //排序
- Collections.sort(gatewayList);
- System.out.println("网关池:" + "\n" + gatewayList);
其他的排序方法:
代码示例:
- package com.zhoulz;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.stream.Collectors;
-
- public class test2_addAll {
- private String name;
- private int age;
- private double height;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public double getHeight() {
- return height;
- }
-
- public void setHeight(double height) {
- this.height = height;
- }
-
- public test2_addAll(String name, int age, double height) {
- this.name = name;
- this.age = age;
- this.height = height;
- }
-
- @Override
- public String toString() {
- return "Test2_addAll{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", height=" + height +
- '}';
- }
-
- public static void main(String[] args) {
- // 集合元素为数字时
- List
numbers = new ArrayList<>(); - Collections.addAll(numbers,1,3,2,6,4,8,7,9);
- // 利用集合的工具类Collections类来对集合中的元素进行排序
- // Collections.sort(numbers);
- // 或者
- // 使用java8新特性中的stream,将ArrayList集合中的元素流化实现排序
- numbers = numbers.stream().sorted(Integer::compareTo).collect(Collectors.toList());
-
- System.out.println("numbers:"+numbers.toString());
-
- // 集合元素为字符串时
- List
strNumbers = new ArrayList<>(); - Collections.addAll(strNumbers,"1","3","2","6","4","8","7","9");
- Collections.sort(strNumbers);
- System.out.println("strNumbers:"+strNumbers.toString());
-
- // 如果List集合中的元素是自定义的数据类型
- List
list = new ArrayList<>(); - Collections.addAll(list,
- new test2_addAll("aaa",10,150.0),
- new test2_addAll("ccc",30,170.0),
- new test2_addAll("eee",50,190.0),
- new test2_addAll("bbb",20,210.0),
- new test2_addAll("ddd",40,230.0)
- );
- System.out.println(list);
-
- //按age进行排序
- list =list
- .stream()
- .sorted(Comparator.comparing(test2_addAll::getAge))
- //.sorted(Comparator.comparing(Test2_addAll::getAge).reversed()) 加上reversed()方法就是逆序排序
- .collect(Collectors.toList());
- System.out.println("list:"+ list.toString());
-
- //按height进行排序
- list =list
- .stream()
- .sorted(Comparator.comparing(test2_addAll::getHeight))
- //.sorted(Comparator.comparing(Test2_addAll::getAge).reversed()) 加上reversed()方法就是逆序排序
- .collect(Collectors.toList());
- System.out.println("list:"+ list.toString());
-
- //按name进行排序
- list =list
- .stream()
- .sorted(Comparator.comparing(test2_addAll::getName))
- //.sorted(Comparator.comparing(Test2_addAll::getAge).reversed()) 加上reversed()方法就是逆序排序
- .collect(Collectors.toList());
- System.out.println("list:"+ list.toString());
- }
- }