java-stream

Java map an array of Strings to an array of Integers

↘锁芯ラ 提交于 2021-02-07 12:14:33
问题 I found this code on SO to map strings to ints Arrays.stream(myarray).mapToInt(Integer::parseInt).toArray(); But how do I make it map to Integer type not the primitive int? I tried switching from Integer.parseInt to Integer.valueOf , but it seems that the mapToInt() method forces the primitive type. I have an ArrayList of arrays of Integers, so I cannot use primitive ints. 回答1: Since String and Integer are both reference types you can simply call Stream::map to transform your array. Integer[]

Find elements in a list that are not present in another list using java 8

ε祈祈猫儿з 提交于 2021-02-07 08:13:45
问题 I have 2 lists. The requirement is to filter out elements in list1 that are not in list2 based on condition. Class Fighter { String name; String address; } List<Fighter> pairs1 = new ArrayList(); pairs1.add(new Fighter("a", "a")); pairs1.add(new Fighter("b", "a")); List<Fighter> pairs2 = new ArrayList(); pairs2.add(new Fighter("a", "c")); pairs2.add(new Fighter("a", "d")); Set<Fighter> finalValues = new HashSet<>(); finalValues = pairs1.stream().filter(firstList -> pairs2.stream().noneMatch

Find elements in a list that are not present in another list using java 8

懵懂的女人 提交于 2021-02-07 07:59:29
问题 I have 2 lists. The requirement is to filter out elements in list1 that are not in list2 based on condition. Class Fighter { String name; String address; } List<Fighter> pairs1 = new ArrayList(); pairs1.add(new Fighter("a", "a")); pairs1.add(new Fighter("b", "a")); List<Fighter> pairs2 = new ArrayList(); pairs2.add(new Fighter("a", "c")); pairs2.add(new Fighter("a", "d")); Set<Fighter> finalValues = new HashSet<>(); finalValues = pairs1.stream().filter(firstList -> pairs2.stream().noneMatch

Java 8 Stream - parallel execution - different result - why?

99封情书 提交于 2021-02-06 10:15:06
问题 Let's say i have a List<Integer> ints = new ArrayList<>(); and i want to add values to it and compare the results of parallel execution using forEach() and Collectors.toList() . First i add to this list some values from an sequential IntStream and forEach: IntStream.range(0,10).boxed().forEach(ints::add); And i get the correct result: ints ==> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Now i .clear() the list and do the same thing in parallel: IntStream.range(0,10).parallel().boxed().forEach(ints::add);

What's the difference between groupingby and mapping in Collectors (Java)?

若如初见. 提交于 2021-02-06 08:43:34
问题 Take a look at this piece of code. // group by price, uses 'mapping' to convert List<Item> to Set<String> Map<BigDecimal, Set<String>> result = items.stream().collect( Collectors.groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet()) ) ); Is groupingBy and Mapping interchangeable? What is their differences? For the third parameter in collect(), would I get the same output type Map if I used Collectors.toList() instead of Collectors.toSet()? I heard that toList() is a

Cannot cast from List<Functions.Function1<Object,String>> to List<String> creating a list from list of WebElements using Selenium and stream() Java8

狂风中的少年 提交于 2021-02-05 07:49:51
问题 I'm trying to read an HTML table and get all the values into a 2d list using selenium. I'm using streamable to create a List inside another map() method. But I get Cannot cast from List<Functions.Function1<Object,String>> to List<String> error at nested collect method's line HTML: +--------+-------+ | r1 v1 | r1 v2 | +--------+-------+ | r2 v1 | r2 v2 | +--------+-------+ <html> <body> <table> <tbody> <tr> <td> <div> <span>r1 v1</span> </div> </td> <td> <div> <span>r1 v2</span> </div> </td> <

Cannot cast from List<Functions.Function1<Object,String>> to List<String> creating a list from list of WebElements using Selenium and stream() Java8

巧了我就是萌 提交于 2021-02-05 07:49:08
问题 I'm trying to read an HTML table and get all the values into a 2d list using selenium. I'm using streamable to create a List inside another map() method. But I get Cannot cast from List<Functions.Function1<Object,String>> to List<String> error at nested collect method's line HTML: +--------+-------+ | r1 v1 | r1 v2 | +--------+-------+ | r2 v1 | r2 v2 | +--------+-------+ <html> <body> <table> <tbody> <tr> <td> <div> <span>r1 v1</span> </div> </td> <td> <div> <span>r1 v2</span> </div> </td> <

How to group into map of arrays?

谁都会走 提交于 2021-02-04 21:22:52
问题 Can a groupingBy operation on a stream produce a map where the values are arrays rather than lists or some other collection type? For example: I have a class Thing . Things have owners, so Thing has a getOwnerId method. In a stream of things I want to group the things by owner ID so that things with the same owner ID end up in an array together. In other words I want a map like the following where the keys are owner IDs and the values are arrays of things belonging to that owner. Map<String,

How to group into map of arrays?

拟墨画扇 提交于 2021-02-04 21:22:16
问题 Can a groupingBy operation on a stream produce a map where the values are arrays rather than lists or some other collection type? For example: I have a class Thing . Things have owners, so Thing has a getOwnerId method. In a stream of things I want to group the things by owner ID so that things with the same owner ID end up in an array together. In other words I want a map like the following where the keys are owner IDs and the values are arrays of things belonging to that owner. Map<String,

Converting a list of objects to Map

[亡魂溺海] 提交于 2021-02-04 21:11:36
问题 I'm trying to convert a list of players into map. player contains name & runs as variables. List<Player> runnerList = Arrays.asList(new Player("Virat", 4654), new Player("Jaddu", 5798), new Player("Dhoni", 4581), new Player("Virat", 8709), new Player("Dhoni", 4711), new Player("Virat", 4541)); my problem is i'm trying to convert to map by combining the runs using streams and not getting through. Tried for each and merged the values, like below, and getting expected result. playerList.forEach