方法引用的简单代码

文章详细介绍了如何在Java中使用Person类进行年龄和姓名的比较,展示了sort方法的不同调用方式,包括静态方法引用和StreamAPI的应用,以及字符串数组的排序方法.

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

public class Person {
    private  String name;
    private Integer age;

    public Person(){}

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public static int compare(Person a ,Person b) {
        int r = a.getAge().compareTo(b.getAge());
        if (r != 0) {
            return r;
        } else {
            return a.getName().compareTo(b.getName());
        }
    }

    public static int compare(Person a ,Person b,Person c) {
        return 0;
    }

    public Person concat(Person b){
        this.setName(this.getName()+","+b.getName());
        System.out.println(this);
        return this;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
public class MrTestCase {
    @Test
    public void case1(){
        ArrayList<Person> personList = new ArrayList<>();
        personList.add(new Person("Sam",20));
        personList.add(new Person("Lily",23));
        personList.add(new Person("Jack",20));

       /* personList.sort(new Comparator<Person>() {
            @Override
            public int compare(Person a, Person b) {
                return Person.compare(a,b);
            }
        });

        personList.sort((a,b)->Person.compare(a,b));*/

        //方法引用写法 调用static静态方法,参数类型动态推断
        personList.sort(Person::compare);
        System.out.println(personList);
    }

    @Test
    public void case2(){
        ArrayList<Person> personList = new ArrayList<>();
        personList.add(new Person("Sam",20));
        personList.add(new Person("Lily",23));
        personList.add(new Person("Jack",20));

        personList.stream().sorted((a,b)->Person.compare(a,b)).forEach(person -> System.out.println(person));

        personList.stream().sorted(Person::compare).forEach(System.out::println);
    }

    @Test
    public void case3(){
        ArrayList<Person> personList = new ArrayList<>();
        personList.add(new Person("Sam",20));
        personList.add(new Person("Lily",23));
        personList.add(new Person("Jack",20));

        Person p = new Person("zhangsan", 0);
        personList.stream().sorted(Person::compare).forEach(p::concat);

    }
    //::实例化对象
    @Test
    public void case4() {
        ArrayList<Person> personList = new ArrayList<>();
        personList.add(new Person("Sam", 20));
        personList.add(new Person("Lily", 23));
        personList.add(new Person("Jack", 20));

        Person p = new Person("老六", 0);
        //personList.stream().sorted(Person::compare).collect(Collectors.toList());
        personList.stream().sorted(Person::compare).collect(Collectors.toCollection(ArrayList::new));

    }

    @Test
    public void case5() {
       String[] arr={"Barbara","James","Mary","John","Patricia","Robert","Michael","Linda"};
       /* Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return a.compareToIgnoreCase(b);
            }
        });

        Arrays.sort(arr,(a,b)->a.compareToIgnoreCase(b));*/
        //Arrays.sort(arr,String::compareTo);
        Arrays.sort(arr,String::compareToIgnoreCase);
        System.out.println(Arrays.toString(arr));
    }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值