Last active
July 4, 2018 08:51
-
-
Save nesheep5/6f96fdb969604d5611b4 to your computer and use it in GitHub Desktop.
Java8 逆引き Stream API ref: https://qiita.com/nesheep5/items/da42df92397285d4ad0f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Tom", 21)); | |
| persons.add(new Person("Bob", 25)); | |
| persons.add(new Person("Alice",19)); | |
| persons.add(new Person("Mike", 19)); | |
| List<Integer> ageDistinctList = | |
| persons.stream() | |
| .map(Person::getAge) | |
| .distinct() | |
| .collect(Collectors.toList()); | |
| ageDistinctList.stream().forEach(System.out::println); | |
| // 21 | |
| // 25 | |
| // 19 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Tom", 21, | |
| new Person("Tomas", 1), | |
| new Person("Tommy", 0))); | |
| persons.add(new Person("Bob", 25, | |
| new Person("Bobby", 2))); | |
| persons.add(new Person("Alice", 19)); | |
| persons.stream() | |
| .flatMap(person -> person.getChildren().stream()) | |
| .forEach(System.out::println); | |
| // Tomas(1) | |
| // Tommy(0) | |
| // Bobby(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Tom", 21)); | |
| persons.add(new Person("Bob", 25)); | |
| persons.add(new Person("Alice",19)); | |
| String nameCSV = | |
| persons.stream() | |
| .map(p -> String.format("\"%s\"", p.getName())) | |
| .collect(Collectors.joining(",")); | |
| System.out.println(nameCSV); | |
| // "Tom","Bob","Alice" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Tom", 21)); | |
| persons.add(new Person("John", 18)); | |
| persons.add(new Person("Jack", 19)); | |
| Map<Object,List<Person>> nameIndex = | |
| persons.stream() | |
| .collect(Collectors.groupingBy(p -> p.getName().charAt(0))); | |
| System.out.println(nameIndex); | |
| // {J=[John(18), Jack(19)], T=[Tom(21)]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Tom", 21)); | |
| persons.add(new Person("Bob", 25)); | |
| persons.add(new Person("Alice",19)); | |
| List<String> nameList = | |
| persons.stream() | |
| .map(Person::getName) | |
| .collect(Collectors.toList()); | |
| nameList.stream().forEach(System.out::println); | |
| // "Tom" | |
| // "Bob" | |
| // "Alice" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Person { | |
| private String name; | |
| private int age; | |
| private List<Person> children; | |
| public Person(String name){/*...*/} | |
| public Person(String name, int age){/*...*/} | |
| public Person(String name, int age, Person... children){/*...*/} | |
| @Override | |
| public String toString() {/* name(age) [children...] */} | |
| // getter, setter... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Tom", 21)); | |
| persons.add(new Person("Bob", 25)); | |
| persons.add(new Person("Alice",1); | |
| String nameCSV = | |
| persons.stream() | |
| .map(p -> String.format("\"%s\"", p.getName())) | |
| .collect(Collectors.joining(",")); | |
| System.out.println(nameCSV); | |
| // "Tom","Bob","Alice" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Tom", 21)); | |
| persons.add(new Person("Bob", 25)); | |
| persons.add(new Person("Alice",1); | |
| List<String> nameList = | |
| persons.stream() | |
| .map(Person::getName) | |
| .collect(Collectors.toList()); | |
| nameList.steram().forEach(System.out::println); | |
| // "Tom" | |
| // "Bob" | |
| // "Alice" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String[] names = new String[] {"Tom","Bob","Alice"}; | |
| Stream.of(names) | |
| // コンストラクタ参照 | |
| .map(Person::new) // name -> new Person(name) | |
| // メソッド参照 | |
| .forEach(System.out::println); // person -> System.out.println(person) | |
| // Tom(0) | |
| // Bob(0) | |
| // Alice(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Tom", 21)); | |
| persons.add(new Person("Bob", 25)); | |
| persons.add(new Person("Alice",19)); | |
| persons.stream() | |
| .sorted(Comparator.comparingInt(Person::getAge)) | |
| .forEach(System.out::println); | |
| // Alice(19) | |
| // Tom(21) | |
| // Bob(25) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // コレクション | |
| List<String> list = new ArrayList<>(); | |
| Stream<String> listStream = list.stream(); | |
| // 配列 | |
| String[] ary = new String[] {"AA","BB","CC"}; | |
| Stream<String> aryStream1 = Arrays.stream(ary); | |
| Stream<String> aryStream2 = Stream.of(ary); | |
| // MAP | |
| Map<String,String> map = new HashMap<>(); | |
| Stream<Entry<String, String>> mapStream = map.entrySet().stream(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment