java-stream

How to compare two ArrayList and get list1 with filter using java streams

百般思念 提交于 2021-02-04 19:36:07
问题 I have two lists list1 & list2 of type List Term{ long sId; int rowNum; long psid; String name; } List<Term> list1 = new ArrayList<>(); List<Term> list2 = new ArrayList<>(); I want to return all the items from list1 where (list1.psid != list2.psid). I tried this but its not working public List<Term> getFilteredRowNum(List<Term> list1, List<Term> list2) { List<Long> psid = list2.stream().map(x -> x.getPsid()).collect(Collectors.toList()); return list1.stream().filter(x -> !psid.contains(x

In java, why stream peek is not working for me?

落爺英雄遲暮 提交于 2021-02-04 19:09:18
问题 we have peek function on stream which is intermediate function which accepts consumer. Then in my case why doesn't it replace "r" with "x". peek should ideally used for debugging purpose but I was just wondering why didn't it worked here. List<String> genre = new ArrayList<String>(Arrays.asList("rock", "pop", "jazz", "reggae")); System.out.println(genre.stream().peek(s-> s.replace("r","x")).peek(s->System.out.println(s)).filter(s -> s.indexOf("x") == 0).count()); 回答1: Because peek() accepts a

Collectors.groupingBy (Function, Supplier, Collector) doesn't accept lambda / dosen't see streamed values

二次信任 提交于 2021-02-04 18:51:47
问题 I tried to group values using streams and Collectors. I have list of String which I have to split. My data: List<String> stringList = new ArrayList<>(); stringList.add("Key:1,2,3") stringList.add("Key:5,6,7") Key is a key in map and 1,2,3 are a values in map First I have tried using simple toMap Map<String, List<Integer>> outputKeyMap = stringList.stream() .collect(Collectors.toMap(id -> id.split(":")[0], id-> Arrays.stream(id.split(":")[1].split(",")).collect(Collectors.toList()); but it

How to flatten a list inside a map in Java 8

孤街浪徒 提交于 2021-02-04 18:17:05
问题 How can I go from a map of integers to lists of strings such as: <1, ["a", "b"]>, <2, ["a", "b"]> To a flattened list of strings such as: ["1-a", "1-b", "2-a", "2-b"] in Java 8 ? 回答1: You can use flatMap on values as: map.values() .stream() .flatMap(List::stream) .collect(Collectors.toList()); Or if you were to make use of the map entries, you can use the code as Holger pointed out : map.entries() .stream() .flatMap(e -> e.getValue().stream().map(s -> e.getKey() + s)) .collect(Collectors

Java 8- Multiple Group by Into Map of Collection

谁说我不能喝 提交于 2021-02-04 17:56:06
问题 I'm trying to do a groupingBy on two attributes of an object with Java streams. That's easy enough as has been documented by some answers: products.stream().collect( Collectors.groupingBy(Product::getUpc, Collectors.groupingBy(Product::getChannelIdentifier))); for example, the above snippet will produce a Map of Maps in the form Map<String, Map<String, List<Product>>> Where a map has keys of UPC codes and its values are maps that have keys of Channel Identifiers which reference a list of

Java 8 matrix * vector multiplication

最后都变了- 提交于 2021-02-04 17:38:29
问题 I'm wondering if there is a more condensed way of doing the following in Java 8 with streams: public static double[] multiply(double[][] matrix, double[] vector) { int rows = matrix.length; int columns = matrix[0].length; double[] result = new double[rows]; for (int row = 0; row < rows; row++) { double sum = 0; for (int column = 0; column < columns; column++) { sum += matrix[row][column] * vector[column]; } result[row] = sum; } return result; } Making an Edit. I received a very good answer,

Java 8 Streams: count all elements which enter the terminal operation

泪湿孤枕 提交于 2021-02-04 16:53:06
问题 I wonder whether there is a nicer (or just an other) approach to get the count of all items that enter the terminal operation of a stream instead of the following: Stream<T> stream = ... // given as parameter AtomicLong count = new AtomicLong(); stream.filter(...).map(...) .peek(t -> count.incrementAndGet()) where count.get() gives me the actual count of the processed items at that stage. I deliberately skipped the terminal operation as that might change between .forEach , .reduce or .collect

How to get the first object (with any ordered by function) of each type (selected by attribute) in Java Stream

大城市里の小女人 提交于 2021-02-04 16:32:25
问题 Imagine a simple object with 3 attributes: public class Obj { boolean toBeAdded; String type; int order; public Obj(boolean toBeAdded, String type, int order) { this.toBeAdded = toBeAdded; this.type = type; this.order = order; } public boolean isToBeAdded() { return toBeAdded; } public String getType() { return type; } public int getOrder() { return order; } } Imagine I have a List of several Obj s, with different types: import java.util.Arrays; import java.util.List; public class Utils {

Getting first element and returning after apply a function

末鹿安然 提交于 2021-02-04 16:18:25
问题 I am new in Java 8, I want to make a method that gets the first element that matched and returning after apply a function public void test() { List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API"); String str = features .stream() .filter(s -> "Lambdas".equals(s)) .findFirst() .ifPresent(this::toLowerCase); } private String toLowerCase (String str) { return str.toLowerCase(); } but I got an Incompatible types error. 回答1: Optional.ifPresent

From for loop to Java 8 Stream example

若如初见. 提交于 2021-02-04 16:14:12
问题 I would like a simple example for Java 8 Streams to understand it. I have this code that returns a free taxi. I would like to replace this for loop with equivalent code that uses Java 8 streams : private List<Taxi> taxis = new ArrayList<Taxi>(); Taxi scheduleTaxi(){ for (Taxi taxi : taxis) { if (taxi.isFree()) { return taxi; } } return null; } I iterate over a list of taxis , and evaluate if taxi respects the condition. If the condition applies, I stop the loop and return taxi . Any