java-8

What decides which functional interface to create from a lambda?

回眸只為那壹抹淺笑 提交于 2020-12-05 07:27:58
问题 Please consider this example: import java.util.function.Consumer; public class Example { public static void main(String[] args) { Example example = new Example(); example.setConsumer(test -> System.out.println("passed string is " + test)); //uses MyConsumer, why ? example.getConsumer().accept("Test 1"); example.setConsumer((MyConsumer<String>)test -> System.out.println("passed string is " + test)); //uses MyConsumer example.getConsumer().accept("Test 2"); example.setConsumer((Consumer<String>

What decides which functional interface to create from a lambda?

流过昼夜 提交于 2020-12-05 07:27:20
问题 Please consider this example: import java.util.function.Consumer; public class Example { public static void main(String[] args) { Example example = new Example(); example.setConsumer(test -> System.out.println("passed string is " + test)); //uses MyConsumer, why ? example.getConsumer().accept("Test 1"); example.setConsumer((MyConsumer<String>)test -> System.out.println("passed string is " + test)); //uses MyConsumer example.getConsumer().accept("Test 2"); example.setConsumer((Consumer<String>

Stuck with lambda expression and Map

六月ゝ 毕业季﹏ 提交于 2020-12-05 05:30:06
问题 I have the Person class: import java.util.*; public class Person { private String name; Map<String,Integer> Skills=new HashMap<>(); // skill name(String) and level(int) public String getName(){ return this.name; } public Map<String,Integer> getSkills(){ return this.Skills; } } And the App class: import java.util.*; import java.util.Map.Entry; import static java.util.stream.Collectors.*; import static java.util.Comparator.*; public class App { private List<Person> people=new ArrayList<>(); //

Stuck with lambda expression and Map

a 夏天 提交于 2020-12-05 05:28:10
问题 I have the Person class: import java.util.*; public class Person { private String name; Map<String,Integer> Skills=new HashMap<>(); // skill name(String) and level(int) public String getName(){ return this.name; } public Map<String,Integer> getSkills(){ return this.Skills; } } And the App class: import java.util.*; import java.util.Map.Entry; import static java.util.stream.Collectors.*; import static java.util.Comparator.*; public class App { private List<Person> people=new ArrayList<>(); //

Java 8 Optional and flatMap - what is wrong?

◇◆丶佛笑我妖孽 提交于 2020-12-03 17:59:29
问题 Some piece of code: public class Player { Team team; String name; } public class Team { List<Player> players; } public class Demo { @Inject TeamDAO teamDAO; @Inject PlayerDAO playerDAO; List<String> findTeamMatesNames(String playerName) { Optional<Player> player = Optional.ofNullable(playerDAO.get(playerName)); return player.flatMap(p -> teamDAO.findPlayers(p.team)) .map(p -> p.name) .orElse(Collections.emptyList()); } } Why am I not able to do this? In flatMap method I am getting error "Type

Java 8 Optional and flatMap - what is wrong?

安稳与你 提交于 2020-12-03 17:58:08
问题 Some piece of code: public class Player { Team team; String name; } public class Team { List<Player> players; } public class Demo { @Inject TeamDAO teamDAO; @Inject PlayerDAO playerDAO; List<String> findTeamMatesNames(String playerName) { Optional<Player> player = Optional.ofNullable(playerDAO.get(playerName)); return player.flatMap(p -> teamDAO.findPlayers(p.team)) .map(p -> p.name) .orElse(Collections.emptyList()); } } Why am I not able to do this? In flatMap method I am getting error "Type

Java 8 Optional and flatMap - what is wrong?

烈酒焚心 提交于 2020-12-03 17:57:57
问题 Some piece of code: public class Player { Team team; String name; } public class Team { List<Player> players; } public class Demo { @Inject TeamDAO teamDAO; @Inject PlayerDAO playerDAO; List<String> findTeamMatesNames(String playerName) { Optional<Player> player = Optional.ofNullable(playerDAO.get(playerName)); return player.flatMap(p -> teamDAO.findPlayers(p.team)) .map(p -> p.name) .orElse(Collections.emptyList()); } } Why am I not able to do this? In flatMap method I am getting error "Type

Can somebody recommend a java 8 pattern to replace a switch statement?

我是研究僧i 提交于 2020-12-02 07:23:38
问题 I have following code: public class A { private String type; String getType() { return type;} } Now in many code places I have code like this switch (a.geType()) { case "A" : return new Bla(); case "B" : return new Cop(); } or somewhere else switch (a.geType()) { case "A" : return new Coda(); case "B" : return new Man(); } (Note that I know I should use an Enumeration in production code). What I want to achive is that when a new type is added to class A the compiler should flag all the switch

java - The process cannot access the file because it is being used by another process

一世执手 提交于 2020-12-01 09:46:11
问题 I have a piece of code that monitors a directory for addition of files. Whenever a new file is added to the directory, the contents of the file are picked and published on kafka and then the file is deleted. This works when I make a single request but as soon as I subject my code to 5 or 10 user request from jMeter, the contents are published on kafka successfully but the code isn't able to delete the file. I get a FileSystemException with a message that The process cannot access the file

How to know the jdk version on my machine?

感情迁移 提交于 2020-12-01 09:45:06
问题 I have recently uninstalled JDK 11 and installed JDK 8 . For confirmation, I want to check which JDK is installed on my Windows 10 machine. I typed java -version on cmd then get the error message java is not recognized as an internal or external command Question: How to know which JDK version installed on pc? 回答1: you might need to add path in environment variables which you can find in Control Panel open the Jdk where you installed and add until /bin in the path in environment variables. Add