IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Comparator -> 正文阅读

[游戏开发]Comparator

package com.easypoi.stu.oneDay.collection;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparatorTest {
    static Student s1 = new Student("Ram", 18);
    static Student s2 = new Student("Shyam", 22);
    static Student s3 = new Student("Mohan", 17);

    public static void main(String[] args) {
        thenComparing();
    }

    private static void thenComparing() {
        List<Student2> studentList = Student2.getStudentList();
        // 先根据年龄进行排序,如果有年龄相同的再跟进姓名排序
        Comparator<Student2> comparator = Comparator.comparing(Student2::getAge)
                .thenComparing(Student2::getName);

        Collections.sort(studentList, comparator);
        studentList.forEach(s -> {
            System.out.println(s.getAge() + "-" + s.getName());
        });

        System.out.println("\r\n---------------");
        // 先根据school的sname进行排序,如果sname相同再根据code排序。如果school的sname和code都一样再根据学生age排序,如果还是相同再根据学生name进行排序
        Comparator<Student2> comparator1 = Comparator.comparing(Student2::getSchool)
                .thenComparing(Student2::getSchool, Comparator.comparing(School::getCode))
                .thenComparing(Student2::getAge)
                .thenComparing(Student2::getName);
        Collections.sort(studentList, comparator1);
        studentList.forEach(s -> System.out.println(s.getSchool().getSname() + "|" + s.getSchool().getCode() + "|" + s.getAge() + "|" + s.getName()));
    }

    private static void comparingIntLongDouble() {
        List<Student2> studentList = Student2.getStudentList();
        Collections.sort(studentList, Comparator.comparingInt(Student2::getAge));

        Collections.sort(studentList, Comparator.comparingLong(Student2::getHomeDistance));

        Collections.sort(studentList, Comparator.comparingDouble(Student2::getWeight));
    }

    private static void comparing() {
        List<Student2> studentList = Student2.getStudentList();
        // 这里school的比较,是根据它从写的compareTo来比较的,也就是比较的是sname
        Comparator<Student2> schoolComparator1 = Comparator.comparing(Student2::getSchool);
        Collections.sort(studentList, schoolComparator1);
        studentList.forEach(s -> System.out.print(s.getName() + "-" + s.getSchool().getSname() + " | "));
        System.out.println("\n-------------------");

        // 这里根据school来排序。但是比较school的时候指定了comparator,比较的是code而不是sname了
        Comparator<Student2> schoolComparator2 =
                Comparator.comparing(Student2::getSchool, (sch1, sch2) -> sch1.getCode() - sch2.getCode());
        Collections.sort(studentList, schoolComparator2);
        studentList.forEach(s -> System.out.print(s.getName() + "-" + s.getSchool().getCode() + " | "));
        System.out.println("\n-------------------");
    }


    private static void nullAtLast() {
        System.out.println("-------Case1: One null----------");

        List<Student> list = Arrays.asList(s1, s2, null, s3);
        Collections.sort(list, Comparator.nullsLast(Comparator.comparing(Student::getName)));
        list.forEach(s -> System.out.println(s));

        System.out.println("--------Case2: More than one null---------");

        list = Arrays.asList(s1, null, s2, null, s3);
        Collections.sort(list, Comparator.nullsLast(Comparator.comparing(Student::getName)));
        list.forEach(s -> System.out.println(s));
    }

    private static void nullFirst() {
        System.out.println("-------Case1: One null----------");

        List<Student> list = Arrays.asList(s1, s2, null, s3);
        Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(Student::getName)));
        list.forEach(s -> System.out.println(s));

        System.out.println("--------Case2: More than one null---------");

        list = Arrays.asList(s1, null, s2, null, s3);
        Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(Student::getName)));
        list.forEach(s -> System.out.println(s));

        System.out.println("--------Case5: Specify natural order Comparator to nullsFirst---------");

        list = Arrays.asList(s1, null, s2, null, s3);
        Collections.sort(list, Comparator.nullsFirst(Comparator.naturalOrder()));
        list.forEach(s -> System.out.println(s));
    }

    private static void naturalOrder() {
        List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
        // 自然顺序
        numList.sort(Comparator.naturalOrder());
        numList.forEach(n -> System.out.print(n + " "));
        System.out.println("\n-----------");
        Collections.sort(numList, Comparator.reverseOrder());
        numList.forEach(n -> System.out.print(n + " "));
    }

    private static void test1() {
        List<Student> studentList = Student.getStudentList();
        System.out.println("--- Sort Students by age ---");
        // Comparator<Student> comparing = Comparator.comparing(Student::getAge);
        Comparator<Student> ageCom = (s1, s2) -> s1.getAge() - s2.getAge();
        studentList.sort(ageCom);
        studentList.forEach(s -> System.out.println(s));

        System.out.println("--- Sort Students by name ---");
        Comparator<Student> nameCom = (s1, s2) -> s1.getName().compareTo(s2.getName());
        studentList.sort(nameCom);
        studentList.forEach(s -> System.out.println(s));

        System.out.println("--- Reversed Sort Students by age ---");
        Comparator<Student> reversed = ageCom.reversed();
        studentList.sort(reversed);
        studentList.forEach(System.out::println);
    }

    static class Student implements Comparable<Student> {
        private String name;
        private int age;

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        @Override
        public int compareTo(Student s) {
            return name.compareTo(s.getName());
        }

        @Override
        public String toString() {
            return name + "-" + age;
        }

        public static List<Student> getStudentList() {
            Student s1 = new Student("Ram", 18);
            Student s2 = new Student("Shyam", 22);
            Student s3 = new Student("Mohan", 19);
            List<Student> list = Arrays.asList(s1, s2, s3);
            return list;
        }
    }

    static public class School implements Comparable<School> {
        private int code;
        private String sname;

        public School(int code, String sname) {
            this.code = code;
            this.sname = sname;
        }

        public int getCode() {
            return code;
        }

        public String getSname() {
            return sname;
        }

        @Override
        public int compareTo(School s) {
            return s.sname.compareTo(sname);
        }
    }

    static public class Student2 {
        private String name;
        private int age;
        private long homeDistance;
        private double weight;
        private School school;

        public Student2(String name, int age, long homeDistance, double weight, School school) {
            this.name = name;
            this.age = age;
            this.homeDistance = homeDistance;
            this.weight = weight;
            this.school = school;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public long getHomeDistance() {
            return homeDistance;
        }

        public double getWeight() {
            return weight;
        }

        public School getSchool() {
            return school;
        }

        public static List<Student2> getStudentList() {
            Student2 s1 = new Student2("Ram", 18, 3455, 60.75, new School(101, "PQ College"));
            Student2 s2 = new Student2("Shyam", 22, 3252, 65.80, new School(103, "RS College"));
            Student2 s3 = new Student2("Mohan", 19, 1459, 65.20, new School(102, "AB College"));
            Student2 s4 = new Student2("Mahesh", 20, 4450, 70.25, new School(104, "CD College"));

            Student2 s5 = new Student2("Aam", 18, 3455, 60.75, new School(101, "PQ College"));
            List<Student2> list = Arrays.asList(s1, s2, s3, s4, s5);
            return list;
        }
    }
}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-04-04 12:42:53  更:2022-04-04 12:45:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/24 10:14:24-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码