java-8

filter Map in Java 8 Streams

泄露秘密 提交于 2020-01-13 10:11:26
问题 I was trying to filter Entries in HashMap using Streams API, but stuck in last method call Collectors.toMap . So, I don't have clue to implemement toMap method public void filterStudents(Map<Integer, Student> studentsMap){ HashMap<Integer, Student> filteredStudentsMap = studentsMap.entrySet().stream(). filter(s -> s.getValue().getAddress().equalsIgnoreCase("delhi")). collect(Collectors.toMap(k , v)); } public class Student { private int id; private String firstName; private String lastName;

How to divide 1 completablefuture to many completablefuture in stream?

喜你入骨 提交于 2020-01-13 10:09:24
问题 For example I have such methods: public CompletableFuture<Page> getPage(int i) { ... } public CompletableFuture<Document> getDocument(int i) { ... } public CompletableFuture<Void> parseLinks(Document doc) { ... } And my flow: List<CompletableFuture> list = IntStream .range(0, 10) .mapToObj(i -> getPage(i)) // I want method like this: .thenApplyAndSplit(CompletableFuture<Page> page -> { List<CompletableFuture<Document>> docs = page.getDocsId() .stream() .map(i -> getDocument(i)) .collect

Will java allow to use functional interfaces as methods?

廉价感情. 提交于 2020-01-13 08:28:26
问题 With the new java lambdas and the concept of functional interfaces, will it be possible to treat those functional interfaces as methods? interface Func { void execute(int i); } void call(Func f) { f(1); //instead of f.execute(1); } I found a lot of information about the syntax of actual lambda expressions, but nothing about this. 回答1: Your proposition What you propose has been discussed on the lambda-dev mailing list before: http://mail.openjdk.java.net/pipermail/lambda-dev/2012-February

IntelliJ compile for Java 7 JRE using Java 8 SDK

允我心安 提交于 2020-01-13 07:38:10
问题 I have the Java 8 SDK and runtime, and a project that is only using Java 7 features. I was wondering whether I could set up intellij to build a Java-7-level jar, something that is definitely possible from command line? So far, I have tried simply setting the project language level to 7 as an experiment, but that is obviously insufficient and not really what I need. I have also seen this question on SO: "Intellij IDEA using java7 compiler to compile when I have configured it to use Java6", but

White Screen is appearing in javaFx application & its getting stuck

心已入冬 提交于 2020-01-13 06:21:09
问题 We are developing video streaming app using JavaFx and JavaCv, While playing Stream into gridPane(8X8), we are occasionally facing splashing White Screen issue and it's continuous. Details:- new Thread(new Runnable() { @Override public void run() { frameGrabber = new FFmpegFrameGrabber(Url); frameGrabber.setVideoOption("preset","ultrafast"); frameGrabber.setOption("rtsp_transport","tcp"); frameGrabber.setOption("stimeout" , "60000"); frameGrabber.setAudioChannels(0); frameGrabber

Peek() really to see the elements as they flow past a certain point in a pipeline

╄→гoц情女王★ 提交于 2020-01-13 05:31:09
问题 My problem in most simple expressible way: According to JavaDoc : Peek() method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline. I have a pipe of 10 Meters and at the distance of 3 and 7 meter from input head i have two markers [aka peek() ] for checking/debugging my elements. Now from Input end i am giving input of 1,2,3,4,5 . At the point x = 4 meter , i have a filter() which filters all elements less than and equal to 3

How to avoid using Optional.get and Optional.isPresent

半腔热情 提交于 2020-01-13 02:14:22
问题 public ValueA map(ValueB valueB, Date date) { Optional<ValueC> valueCOpt = find(valueB); if (valueCOpt.isPresent()) { ValueC valueC = valueCOpt.get(); // call many getters on valueC and do a lot of logic with it. return map(/*some parameters*/); } return null; } This seems quite ugly. The advantage of optionals is completely gone in here. I read that one should rather use map or flatMap instead of get . But is it really a benefit if I replace every getter like valueC.getFieldA() with

Java8 internal iteration

可紊 提交于 2020-01-12 18:47:19
问题 Does java8 forEach method use an iterator or not really? I google it to the bone, could not find it precisely. Only the fact that it will iterate in the same order the data are. Any tips? 回答1: The default implementation of Iterable#forEach is based on a iterator. default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } But in ArrayList is overridden to this, and not uses the iterator, it uses a for loop over its internal array

Convert SimpleDateFormat to DateTimeFormatter

旧城冷巷雨未停 提交于 2020-01-12 13:52:06
问题 So when trying to replace some legacy code using SimpleDateFormat and Date, to use java.time.DateTimeFormatter and LocalDate I ran into a problem. The two date formats are not equivalent. At this point I must say I know the two date types are not the same but the scenario I am in means I never care about the time aspect so can ignore it. public Date getDate(String value) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); try { return dateFormat.parse(value); } catch

Convert SimpleDateFormat to DateTimeFormatter

守給你的承諾、 提交于 2020-01-12 13:51:19
问题 So when trying to replace some legacy code using SimpleDateFormat and Date, to use java.time.DateTimeFormatter and LocalDate I ran into a problem. The two date formats are not equivalent. At this point I must say I know the two date types are not the same but the scenario I am in means I never care about the time aspect so can ignore it. public Date getDate(String value) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); try { return dateFormat.parse(value); } catch