java-8

How do default and static methods work in java 8 interfaces?

六眼飞鱼酱① 提交于 2021-02-04 19:13:33
问题 I have been trying to get my head around on how actually do the default and static methods work in java 8? consider the following interface: public interface Car { default void drive() { System.out.println("Default Driving"); } static int getWheelCount(){ return wheelCount; } int wheelCount = 7; } and the following implementation: public class Benz implements Car { } Now if I go to my main method and write: public static void main(String[] args){ Car car = new Benz(); car.drive(); System.out

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

Lambda Expression without types

北城以北 提交于 2021-02-04 18:16:50
问题 I understand the syntax of the Java8 lambda expressions but why does the following code work without a specific type declaration of x? Why is "baz" being printed? public class LambdaExpressions { interface Foo { void bar(Object o); } static void doo(Foo f) { f.bar("baz"); } public static void main(String[] args) { doo( x -> {System.out.println(x);}); } } 回答1: Since the interface is a standard functional interface It's a functional interface because it contains only one abstract method. This

Lambda Expression without types

安稳与你 提交于 2021-02-04 18:16:25
问题 I understand the syntax of the Java8 lambda expressions but why does the following code work without a specific type declaration of x? Why is "baz" being printed? public class LambdaExpressions { interface Foo { void bar(Object o); } static void doo(Foo f) { f.bar("baz"); } public static void main(String[] args) { doo( x -> {System.out.println(x);}); } } 回答1: Since the interface is a standard functional interface It's a functional interface because it contains only one abstract method. This

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

Left join using java stream on two lists

折月煮酒 提交于 2021-02-04 16:24:27
问题 the rest descritpion of tthe main classhere is the image for the class descriptionI have an entity Customers with cid,cname and aid and another entity Address with aid, city and state. I have taken data of both the entities in two different lists -list and list . i want a result list (left join on both customer and address)that contains data from both the lists using java stream api or any other feature of java 8. and further what should be the type of result list.? is it possible to do so.?