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 3 years have passed since last update.

JavaのコードTIPS

Last updated at Posted at 2020-09-15

1.ラムダの知識

1-1.主要な関数型インタフェース

インタフェース名 メソッド 概要
Function<T,R> R apply(T t) 引数T, 戻り値R
Consumer<T> void accept(T t) 引数T, 戻り値なし
Predicate<T> boolean test(T t) 引数T, 戻り値boolean
Supplier<T> T get() 引数なし, 戻り値T
UnaryOperator<T> T apply(T t) 引数T, 戻り値T
BinaryOperator<T> T apply(T t1, T t2) 引数T2個, 戻り値R
BiFunction<T,U,R> R apply(T t, U u) 引数TとU, 戻り値R
Runnable void run() 引数と戻り値なし

1-2.中間操作

メソッド名称 概要
filter 条件に一致する要素
distinct 重複を除いた要素
limit 指定された数の要素
skip 指定された数を除いた要素
map 何らかの処理を行って変換した要素
flatmap
sorted ソート
peek

1-3.終端操作

戻り値の型 メソッド名称 概要 類似
boolean anyMatch いずれかの要素と一致したか allMatch
R collect 可変リダクション操作の結果
long count 要素の個数
Optional<T> findAny いずれかの要素 findFirst
void forEach 戻り値を返さない要素への処理
Optional<T> max 最大要素 min
T reduce 累積処理結果

2.Stream

2-1.Streamへの変換

        String[]array = {"a", "b", "c", "d", "e"};

       //Arrays.stream
        Stream<String> stream1 = Arrays.stream(array);
        stream1.forEach(x -> System.out.println(x));

       //Stream.of
        Stream<String> stream2 = Stream.of(array);
        stream2.forEach(x -> System.out.println(x));

3.List関連

ソートと結合

list.stream().sorted(Comparator.reverseOrder()).reduce("", (all,s) -> all + "\r\n" + s);

list.sort(Comparator.reverseOrder());
String.join("\r\n", list);

独自のソート

		List<String> list = new ArrayList<String>();
		list.add("A2:100");
		list.add("B:101");
		list.add("C:100");
		list.add("A1:100");
		list.stream().sorted((s1, s2) -> {

			int ret = s1.substring(s1.length() -3).compareTo(s2.substring(s2.length() -3));
			if(ret == 0) {
				return s1.compareTo(s2);
			}
			return ret * -1;

		}).forEach(System.out::println);
--結果
B:101
A1:100
A2:100
C:100

Listの内容を出力

list.forEach(System.out::println);

連番作成

9.サンプル

9-1. 関数をラップする

9-1-1.引数無し


public class Sample {

	public static void main(String[] args) {

		test4(() -> test1());
		test4(() -> test2());
		test3();
	}
	public  static void  test1() {
		System.out.println("test1");

	}

	public static void test2() {
		System.out.println("test2");
	}

	public static void test3() {
		System.out.println("test3");
	}

	public static void test4(Runnable  logic) {
		System.out.println("test4 START");
		logic.run();
		System.out.println("test4 END");
	}
}
実行結果
test4 START
test1
test4 END
test4 START
test2
test4 END
test3

9-1-2.引数あり

public class Sample {

	public static void main(String[] args) {

		test4(i -> test1(i),"A");
		test4(i -> test2(i),"B");
		test3("B");
	}
	public  static void  test1(String msg) {
		System.out.println("test:1" + msg);

	}

	public static void test2(String msg) {
		System.out.println("test2:"+ msg);
	}

	public static void test3(String msg) {
		System.out.println("test3:"+ msg);
	}

	public static void test4(Consumer<String>  logic, String s) {
		System.out.println("test4 START");
		logic.accept(s);
		System.out.println("test4 END");
	}
}
実行結果
test4 START
test:1A
test4 END
test4 START
test2:B
test4 END
test3:B

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?