LoginSignup
0
0

More than 3 years have passed since last update.

ラムダ、ソート、無名関数の例

Posted at

import java.util.*;
import java.util.function.Predicate;
import java.util.Comparator;

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

// ソートのテスト
// https://techacademy.jp/magazine/34841
class Test1 {
public static void ソートテスト() {

    //小さい順
//  List<Integer> num1 = new ArrayList<>(Arrays.asList(100,35,72));

//     Collections.sort(num1);

//     for (Integer a : num1) {
//         System.out.println(a);//35 72 100
//     }

    List<Integer> num1 = new ArrayList<>(Arrays.asList(72,100,86));

    Collections.sort(num1,new java.util.Comparator<Integer>(){
        @Override
        public int compare(Integer i1,Integer i2) {

            System.out.println(i1);

            return i2 - i1;
        }
    });


    // for (Integer a : num1) {
    //     System.out.println(a); //100 72 35
    // }
}

}

public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
Test1.ソートテスト();

me test = new me();

    //その1 メソッドチェーンの話
    // つまりページの数のインデックス範囲をしぼりこみ
    // 一覧を作る例 100件5ページ目で10件ずつなら 50-60とか
    // 要素を取り出す取り出してリスト形式にする

    // showUserList = IntStream.range(0, allUserList.size())
    // .filter(index -> index >= (currentPage - 1) * showNumber && index < currentPage * showNumber)
    // .mapToObj(allUserList::get).collect(Collectors.toList());
  String 結果= test.test().substring(2,4);

  String 結果= test.test();


  System.out.println(結果);

  //その2 ラムダ式、無名関数の話
  //index -> index >= (currentPage - 1) * showNumber && index < currentPage * showNumber
  //ここの処理はメソッド渡しと言って、メソッドにメソッドそのものを渡す事が出来ます書き方はラムダ式、無名関数とも呼ばれます。

//   中身を書き直すと
//   ○○メソッドで引数がint型のarg変数で処理はargと1を比較するという意味です、全部省略するとこのような書き方になります
    boolean keltuka = test.test2(arg -> arg == 1);
    System.out.println(keltuka);

    //Comparatorインターフェースとは(compareToとは一致しているか調べるメソッドです)

// o1 < o2 なら -1

// o1 == o2 なら 0

// o1 > o2 なら -1
// Comparator c = (s1, s2) -> s1.compareTo(s2);
// System.out.println(c.compare("ABC", "ABC"));
}
}

class me{
public String test()
{
System.out.println("kazuki");
return "kazuki";
}

public boolean test2(Predicate<Integer> predicate)
{
    Integer argument = 1;

    if (predicate.test(argument)) {
        return true;
    } else {
        return false;
    }
}

}

0
0
0

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