0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Java8メソッド参照

Posted at

今回みなさんと一緒にJava8のメソッド参照を勉強しましょう。

まずは、Java8以前の方法で年齢ソードのメソットを作りましょうか。

lombokを使う

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

public class App {
    public static void main( String[] args ) {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Jack",25));
        personList.add(new Person("Mary",23));
        personList.add(new Person("Jill",24));
        personList.add(new Person("Dock",34));
        personList.add(new Person("Lily",27));


        // 匿名クラス
        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        System.out.println(personList);
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private String name;
    private int age;
}

実装結果:[Person(name=Mary, age=23), Person(name=Jill, age=24), Person(name=Jack, age=25), Person(name=Lily, age=27), Person(name=Dock, age=34)]

Lambda式

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

public class App {
    public static void main( String[] args ) {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Jack",25));
        personList.add(new Person("Mary",23));
        personList.add(new Person("Jill",24));
        personList.add(new Person("Dock",34));
        personList.add(new Person("Lily",27));

        // Lambda式
        Collections.sort(personList,(p1,p2) -> p1.getAge() - p2.getAge());
        System.out.println(personList);
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private String name;
    private int age;
    
}

実装結果:[Person(name=Mary, age=23), Person(name=Jill, age=24), Person(name=Jack, age=25), Person(name=Lily, age=27), Person(name=Dock, age=34)]

static メソッド参照

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

public class App {
    public static void main( String[] args ) {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Jack",25));
        personList.add(new Person("Mary",23));
        personList.add(new Person("Jill",24));
        personList.add(new Person("Dock",34));
        personList.add(new Person("Lily",27));

        // static メソッド参照
        Collections.sort(personList, Person::CompareAge);
        System.out.println(personList);

    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private String name;
    private int age;

    public static int CompareAge(Person o1, Person o2) {
        return o1.getAge() - o2.getAge();
    }
}


0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?