lambda

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

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

How to collect two fields of an object into the same list?

柔情痞子 提交于 2021-02-04 11:21:08
问题 I have an Object of goods, which has two properties: firstCategoryId and secondCategoryId . I have a list of goods, and I want to get all category Ids (including both firstCategoryId and secondCategoryId). My current solution is: List<Integer> categoryIdList = goodsList.stream().map(g->g.getFirstCategoryId()).collect(toList()); categoryIdList.addAll(goodsList.stream().map(g->g.getSecondCategoryId()).collect(toList())); Is there a more convenient manner I could get all the categoryIds in a

How to build a Python function with a rolling total?

人走茶凉 提交于 2021-02-04 07:38:32
问题 I am looking to build a function that creates a rolling total by code by day for the below DataFrames, where the In for each code on a date is subtracted from the Out for each code on a date, this subtotal is the subtracted from the previous days total but the total must be >=0 ( I have included an example of this it the Desired Output below). Below are an example of my inputs and the function I am using along with an example of my desired output. df1 - In s = """ Date Code Quantity 0 10/01

Passing lambda to a Timer instead of TimerTask [duplicate]

喜你入骨 提交于 2021-02-03 06:18:09
问题 This question already has answers here : Lambda Expressions for Abstract Classes (3 answers) How to use TimerTask with lambdas? (5 answers) Closed 4 years ago . I want to perform a delayed operation on a map, so I am using Timer , to which I am passing a TimerTask and a delay in milliseconds: timer.schedule(new TimerTask() { public void run() { tournaments.remove(id); } }, delay); This is some sort of primitive cache-like functionality where I set an expiration time on a new resource that was

Passing lambda to a Timer instead of TimerTask [duplicate]

元气小坏坏 提交于 2021-02-03 06:11:33
问题 This question already has answers here : Lambda Expressions for Abstract Classes (3 answers) How to use TimerTask with lambdas? (5 answers) Closed 4 years ago . I want to perform a delayed operation on a map, so I am using Timer , to which I am passing a TimerTask and a delay in milliseconds: timer.schedule(new TimerTask() { public void run() { tournaments.remove(id); } }, delay); This is some sort of primitive cache-like functionality where I set an expiration time on a new resource that was

Passing lambda to a Timer instead of TimerTask [duplicate]

戏子无情 提交于 2021-02-03 06:10:31
问题 This question already has answers here : Lambda Expressions for Abstract Classes (3 answers) How to use TimerTask with lambdas? (5 answers) Closed 4 years ago . I want to perform a delayed operation on a map, so I am using Timer , to which I am passing a TimerTask and a delay in milliseconds: timer.schedule(new TimerTask() { public void run() { tournaments.remove(id); } }, delay); This is some sort of primitive cache-like functionality where I set an expiration time on a new resource that was

Understanding Java Stream reduce - parallel vs series

陌路散爱 提交于 2021-01-29 22:20:46
问题 I have Java code which uses lambdas and streams to find the average of a list of integers. It uses Stream.reduce(...) to find the sum of integers and then calculates average. The code works correctly even when I remove the parallel() call. Can someone explain why ? More questions after the code. import java.util.Arrays; import java.util.List; public class Temp { public static void main(String [] args){ //For example, consider a list of a *series* of numbers in increasing order. List<Integer>

Understanding Java Stream reduce - parallel vs series

ⅰ亾dé卋堺 提交于 2021-01-29 21:59:47
问题 I have Java code which uses lambdas and streams to find the average of a list of integers. It uses Stream.reduce(...) to find the sum of integers and then calculates average. The code works correctly even when I remove the parallel() call. Can someone explain why ? More questions after the code. import java.util.Arrays; import java.util.List; public class Temp { public static void main(String [] args){ //For example, consider a list of a *series* of numbers in increasing order. List<Integer>

Parallelise output of input function in Snakemake

南楼画角 提交于 2021-01-29 13:30:51
问题 Hello Snakemake community, I am having quite some troubles to define correctly a function in Snakemake and call it in the params section. The output of the function is a list and my aim is to use each item of the list as a parameter of a shell command. In other words, I would like to run multiple jobs in parallel of the same shell command with a different parameter. This is the function: import os, glob def get_scontigs_names(wildcards): scontigs = glob.glob(os.path.join("reference",