|
| 1 | +# Android Java 8 Stream API Example |
| 2 | + |
| 3 | +Demo app of using Java 8 features with [Retrolambda](https://github.com/orfjackal/retrolambda) and [Lightweight-Stream-API](https://github.com/aNNiMON/Lightweight-Stream-API). |
| 4 | + |
| 5 | +Features: |
| 6 | + - [() -> lambda expression](app/src/main/java/com/annimon/java8streamexample/MainActivity.java#L46); |
| 7 | + |
| 8 | + ```java |
| 9 | + findViewById(R.id.go).setOnClickListener(v -> { |
| 10 | + final int index = mActionSpinner.getSelectedItemPosition(); |
| 11 | + if (index != Spinner.INVALID_POSITION) { |
| 12 | + action(actions[index]); |
| 13 | + } |
| 14 | + }); |
| 15 | + ``` |
| 16 | + |
| 17 | + |
| 18 | + - [Method::references](app/src/main/java/com/annimon/java8streamexample/Word.java#L37); |
| 19 | + |
| 20 | + ```java |
| 21 | + int cmp = Objects.compare(word, other.word, String::compareToIgnoreCase); |
| 22 | + ``` |
| 23 | + |
| 24 | + |
| 25 | + - [Stream.API()](app/src/main/java/com/annimon/java8streamexample/Utils.java#L38); |
| 26 | + |
| 27 | + ```java |
| 28 | + return Stream.of(lines) |
| 29 | + .map(str -> str.split("\t")) |
| 30 | + .filter(arr -> arr.length == 2) |
| 31 | + .map(arr -> new Word(arr[0], arr[1])) |
| 32 | + .collect(Collectors.toList(new Word[0])); |
| 33 | + ``` |
| 34 | + |
| 35 | + |
| 36 | + - [switch for "string"](app/src/main/java/com/annimon/java8streamexample/MainActivity.java#L82); |
| 37 | + |
| 38 | + ```java |
| 39 | + switch (action) { |
| 40 | + case "filter 1": |
| 41 | + // Filter one word |
| 42 | + stream = stream.filter(p -> p.getWord().split(" ").length == 1); |
| 43 | + break; |
| 44 | + case "filter 2+": |
| 45 | + // Filter two and more words |
| 46 | + stream = stream.filter(p -> p.getWord().split(" ").length >= 2); |
| 47 | + break; |
| 48 | + // ... |
| 49 | + } |
| 50 | + ``` |
| 51 | + |
| 52 | +- [try(with-resources) {}](app/src/main/java/com/annimon/java8streamexample/Utils.java#L27) |
| 53 | + |
| 54 | + ```java |
| 55 | + final List<String> lines = new LinkedList<>(); |
| 56 | + try (final InputStream is = context.getAssets().open("words.txt"); |
| 57 | + final InputStreamReader isr = new InputStreamReader(is, "UTF-8"); |
| 58 | + final BufferedReader reader = new BufferedReader(isr)) { |
| 59 | + String line; |
| 60 | + while ( (line = reader.readLine()) != null ) { |
| 61 | + lines.add(line); |
| 62 | + } |
| 63 | + } catch (IOException e) { |
| 64 | + Log.e("Java 8 Example", "Utils.readWords", e); |
| 65 | + } |
| 66 | + ``` |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +## Links |
| 71 | + |
| 72 | +Demo app: [Java8StreamExample.apk](http://annimon.com/ablogs/file325/Java8StreamExample.apk) |
| 73 | + |
| 74 | +Blog (Russian): [Java 8 в Android со Stream API и лямбдами](http://annimon.com/article/1176) |
| 75 | + |
| 76 | +Retrolambda: https://github.com/orfjackal/retrolambda |
| 77 | + |
| 78 | +Lightweight-Stream-API: https://github.com/aNNiMON/Lightweight-Stream-API |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +## Screenshots |
| 83 | +  |
0 commit comments