Clément Béra | 81a031f | 2022-07-06 12:18:33 +0200 | [diff] [blame] | 1 | // Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package lambda; |
| 6 | |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.List; |
| 9 | |
| 10 | public class Lambda { |
| 11 | |
| 12 | interface StringPredicate { |
| 13 | |
| 14 | boolean test(String t); |
| 15 | |
| 16 | default StringPredicate or(StringPredicate other) { |
| 17 | return (t) -> test(t) || other.test(t); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | public static void main(String[] args) { |
| 22 | ArrayList<String> strings = new ArrayList<>(); |
| 23 | strings.add("abc"); |
| 24 | strings.add("abb"); |
| 25 | strings.add("bbc"); |
| 26 | strings.add("aac"); |
| 27 | strings.add("acc"); |
| 28 | StringPredicate aaStart = Lambda::aaStart; |
| 29 | StringPredicate bbNot = Lambda::bbNot; |
| 30 | StringPredicate full = aaStart.or(bbNot); |
| 31 | for (String string : ((List<String>) strings.clone())) { |
| 32 | if (full.test(string)) { |
| 33 | strings.remove(string); |
| 34 | } |
| 35 | } |
| 36 | System.out.println(strings); |
| 37 | } |
| 38 | |
| 39 | private static boolean aaStart(String str) { |
| 40 | return str.startsWith("aa"); |
| 41 | } |
| 42 | |
| 43 | private static boolean bbNot(String str) { |
| 44 | return !str.contains("bb"); |
| 45 | } |
| 46 | } |