collectors

java 8 Collector<String, A, R> is not a functional interface, who can tell why?

别来无恙 提交于 2019-12-01 16:20:38
The follow code : public class Test { public static void main(String[] args) { Stream.of(1,2,3).map(String::valueOf).collect(Collectors::toList) } } intellij tell me : Collector<String, A, R> is not a functional interface but when i modify the code as follows, everything is ok, i don't know why? public class Test { public static void main(String[] args) { Stream.of(1,2,3).map(String::valueOf).collect(Collectors.<String>toList) } } The reason that the first syntax is illegal is that the target type implied by the method signature— Stream.collect(Collector) —is a Collector . Collector has

Is collectingAndThen method enough efficient?

巧了我就是萌 提交于 2019-12-01 13:45:57
I have recently started using collectingAndThen and found that it is taking a bit long time comparatively to the other coding procedures, which i used for performing the similar tasks. Here is my code: System.out.println("CollectingAndThen"); Long t = System.currentTimeMillis(); String personWithMaxAge = persons.stream() .collect(Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(Person::getAge)), (Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none" )); System.out.println("personWithMaxAge - "+personWithMaxAge + " time taken = "+(System.currentTimeMillis() - t));

Java Stream Collectors.toList() wont compile

早过忘川 提交于 2019-12-01 09:05:35
Can any one explain why the below code will not compile but the second one does? Do not compile private void doNotCompile() { List<Integer> out; out = IntStream .range(1, 10) .filter(e -> e % 2 == 0) .map(e -> Integer.valueOf(2 * e)) .collect(Collectors.toList()); System.out.println(out); } Compilation errors on the collect line The method collect(Supplier, ObjIntConsumer, BiConsumer) in the type IntStream is not applicable for the arguments (Collector>) Type mismatch: cannot convert from Collector> to Supplier Compiles private void compiles() { List<Integer> in; in = Arrays.asList(1, 2, 3, 4,

Java 8 frequency Object in array [duplicate]

佐手、 提交于 2019-12-01 08:52:51
This question already has an answer here: Word frequency count Java 8 8 answers I have an Object[] array I need to create map Map<Obejct, Integer> , where Integer value contains frequency of key Object in array. How can i do it in java 8 style, using Collectors ? You can do (I hope I don't have any typos) : Map<Object,Long> map = Stream.of(array) .collect(Collectors.groupingBy(o -> o, Collectors.counting())); This should group the elements of the array by equality and count the number of Objects in each group. 来源: https://stackoverflow.com/questions/29450169/java-8-frequency-object-in-array

Is collectingAndThen method enough efficient?

余生长醉 提交于 2019-12-01 07:51:35
问题 I have recently started using collectingAndThen and found that it is taking a bit long time comparatively to the other coding procedures, which i used for performing the similar tasks. Here is my code: System.out.println("CollectingAndThen"); Long t = System.currentTimeMillis(); String personWithMaxAge = persons.stream() .collect(Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(Person::getAge)), (Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none" )); System

Java 8 Stream flatMap and group by code compiler error

孤街醉人 提交于 2019-12-01 06:46:18
问题 // given a set of Item objects, group them by the managers of creator and owners Map<String, List<Item>> managersItems = itemSet.parallelStream().flatMap(item -> { // get the list of the creator and owners List<String> users = new ArrayList(); users.add(item.getCreator()); users.addAll(item.getOwners()); return Stream.of(users.toArray(new String[] {})).map(user -> { LdapUserInfo ldapUser = LdapUserInfoFactory.create(user); String manager = ldapUser.getManager(); return new AbstractMap

Collect Lines using Multimap Collector

让人想犯罪 __ 提交于 2019-12-01 04:39:23
Is there a way to covert the below to using collectors yet? List<String[]> lines = getLines(); Multimap<String,String> multimap = ArrayListMultimap.create(); lines.forEach(line -> multimap.put(line[0],line[1]); ); You can use Multimaps.toMultimap collector: ListMultimap<String, String> multimap = lines.stream() .collect(Multimaps.toMultimap( l -> l[0], l -> l[1], ArrayListMultimap::create )); Or if you don't need mutability, use ImmutableListMultimap.toImmutableListMultimap collector: ListMultimap<String, String> multimap = lines.stream() .collect(toImmutableListMultimap(l -> l[0], l -> l[1]))

What is the default Set/List implementation with Collectors in Java 8 Stream API?

狂风中的少年 提交于 2019-12-01 01:22:56
I have below code snap with Java 8. List<Employee> employees = DataProvider.getEmployees(); Set<Employee> set = employees.stream().filter(emp -> { System.out.println(emp.getName()); return emp.getName().equals("Vishal"); }).collect(Collectors.toSet()); I just want to know which implementation of Set it is using by default when we use Collectors.toSet() (refer above example)? Also, is there any way to tell the Java API to use a particular implementation (for example, HashSet )? The toSet() collector does not specify which implementation it uses; you get a Set , that's all. If you want a

Java8 stream groupingBy enum and counting

末鹿安然 提交于 2019-11-30 21:44:35
With the classes: public class Person { private String name; private Color favouriteColor; } public enum Color {GREEN, YELLOW, BLUE, RED, ORANGE, PURPLE} Having a List<Person> using the Java8 Stream API can I trasform it in a Map<Color, Long> having the count of each Color , also for the color that aren't included in the list. Example: List<Person> list = List.of( new Person("Karl", Color.RED), new Person("Greg", Color.BLUE), new Person("Andrew", Color.GREEN) ); Trasforming this list in a Map with all the colors of the enum with their count. Thanks SOLVED Solved using a custom collector:

What is the default Set/List implementation with Collectors in Java 8 Stream API?

左心房为你撑大大i 提交于 2019-11-30 19:05:26
问题 I have below code snap with Java 8. List<Employee> employees = DataProvider.getEmployees(); Set<Employee> set = employees.stream().filter(emp -> { System.out.println(emp.getName()); return emp.getName().equals("Vishal"); }).collect(Collectors.toSet()); I just want to know which implementation of Set it is using by default when we use Collectors.toSet() (refer above example)? Also, is there any way to tell the Java API to use a particular implementation (for example, HashSet )? 回答1: The toSet(