Convert IntStream to List< Integer > in Java
List< Integer > list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
map in Java
List list = Arrays.asList(1, 4, 5);
List ls = IntStream.range(0, list.size()).mapToObj(i -> 2*list.get(i)).collect(Collectors.toList());
List list = Arrays.asList(1, 4, 5);
List ls = list.stream().map(i -> 2*i).collect(Collectors.toList());
// Though it is not as elegant as Haskell
// map (*2) [1, 4, 5]
reduce in Java
List list = Arrays.toList(1, 2, 3);
Optional sum = list.stream().reduce((x, y) -> x + y);
sum.ifPresent(System.out::println);
Integer sum = list.stream().reduce(0, (x, y) -> x + y);
filter in Java
List ls = Arrays.asList("file.htmlkk", "file.html", "kk.html_33", ".html", "html");
List fls = ls.stream().filter( s -> FileType.isHtml(s) ).collect(Collectors.toList());
List ls3 = Arrays.asList(1, 2, 3, 4);
Set set = ls3.stream().filter(i -> i >= 2).collect(Collectors.toSet());
p(set);
More are coming
Optional sum = list.stream().reduce(Double::sum);
sum.ifPresent(System.out::println);
Integer[] array = {1, 2, 3, 4};
List listInt = Arrays.asList(array);
// list filter
List< Integer> leftList = listInt.stream().filter(x-> x < 3).collect(Collectors.toList());
List< Integer> rightList = listInt.stream().filter(x-> x > 3).collect(Collectors.toList());
Aron.printList(leftList);
Aron.printList(rightList);
// list forEach
leftList.forEach(System.out::println);
leftList.forEach(x->System.out.println(x));
// HashMap filter
Map< Integer, Integer> map = new HashMap< Integer, Integer>();
map.put(1, 4);
map.put(2, 5);
map.put(3, 7);
Map< Integer, Integer> newMap1 = map.entrySet().parallelStream()
.filter(e->e.getValue() > 1)
.collect(Collectors.toMap(e->e.getKey(), e->e.getValue()));
Aron.printMap(newMap1);
Map< Integer, Integer> newMap2 = map.entrySet().stream()
.filter(e->e.getValue() > 1)
.collect(Collectors.toMap(e->e.getKey(), e->e.getValue()));
Aron.printMap(newMap2);
// list of String to list of Integer
String[] arrStr = {"0", "1", "2", "3"};
List< String> list1 = Arrays.asList(arrStr);
List< Integer> strIntList = list1.stream().map(Integer::parseInt).collect(Collectors.toList());
Aron.printList(strIntList);