[JAVA] Examples of lambda, sort, and anonymous functions

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;

// Sort test // https://techacademy.jp/magazine/34841 class Test1 { public static void sort test () {

//小さい順 // List 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. Sort test ();

me test = new me();

// Part 1 Method chain story // In other words, narrow down the index range of the number of pages // Example of making a list: 100 cases, 10 cases each on the 5th page, 50-60, etc. // Extract the element Extract and put it in list format

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

String Result = test.test (). Substring (2,4);

String Result = test.test ();

System.out.println (result);

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

// If you rewrite the contents // In the ○○ method, the argument is an int type arg variable, and the process means that arg and 1 are compared. If all are omitted, it will be written like this. boolean keltuka = test.test2(arg -> arg == 1); System.out.println(keltuka);

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

// If o1 <o2, -1

// 0 if o1 == o2

// o1> o2 then -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;
    }
}

}

Recommended Posts

Examples of lambda, sort, and anonymous functions
Frequently used functions and commands of Xcode
Basic usage of enums and code examples
JDBC promises and examples of how to write
Functions and methods
[Java] Sort the list using streams and lambda expressions
Implement Thread in Java and try using anonymous class, lambda