java-8

Conditionally add an operation to a Java 8 stream

女生的网名这么多〃 提交于 2020-01-01 08:33:48
问题 I'm wondering if I can add an operation to a stream, based off of some sort of condition set outside of the stream. For example, I want to add a limit operation to the stream if my limit variable is not equal to -1 . My code currently looks like this, but I have yet to see other examples of streams being used this way, where a Stream object is reassigned to the result of an intermediate operation applied on itself: // Do some stream stuff stream = stream.filter(e -> e.getTimestamp() < max); /

Is Java 8 findFirst().isPresent() more efficient than count() > 0?

佐手、 提交于 2020-01-01 08:26:13
问题 Given I have a stream Stream<T> stream = list.stream().filter(some predicate) where the list is very large, is it more efficient to check whether the stream is non-empty by doing: stream.count() > 0 or by doing: stream.findFirst().isPresent() ? 回答1: If all you want to know, is whether there is a match, you should use list.stream().anyMatch(some predicate) , not only because it is more efficient, but also, because it is the correct idiom for expressing you intention. As said by others,

Using Java 7 Comparators in Java 8

元气小坏坏 提交于 2020-01-01 07:39:09
问题 Situation I have an OSGi project that I'm trying to migrate to Java 8. In my project, I have dependencies to third party libraries that I "OSGi-fied" (by just adding the MANIFEST.MF file and putting metadata into it). These libraries are checked out from read-only SVN repositories, so I just can checkout updates from then when needed and therefore I don't want to make any other changes than in the MANIFEST.MF file, since I cannot commit them. Problem However, these libraries use lots of

In java 8, why cannot call the interface static method that the current class is implementing [duplicate]

余生长醉 提交于 2020-01-01 07:32:07
问题 This question already has answers here : Why are class static methods inherited but not interface static methods? (4 answers) Closed 4 years ago . I'm playing around Java 8's new features recently and observed an interesting behaviour: This is okay: Class A { static void staticMethodInA() {println();} } Class B extends A {} B.staticMethodInA(); This would induce an error of: static method may be invoked on containing interface class only . interface A { static void staticMethodInA() {println(

Java 8 collector for Guava Immutable Table

 ̄綄美尐妖づ 提交于 2020-01-01 07:04:15
问题 Use case: Process list of string via method which returns ImmutableTable of type {R,C,V} . For instance ImmutableTable of {Integer,String,Boolean} process(String item){...} Collect the result i.e, merge all results and return ImmutableTable . Is there a way to achieve it? Current implementation (as suggested by Bohemian): How about using parallel stream ? Is there any concurrency issues in the below code? With Parallel stream i an getting "NullPointerException at index 1800" on tableBuilder

Java 8 : Get week number since epoch time

醉酒当歌 提交于 2020-01-01 07:03:26
问题 Given LocalDate I want to convert to week number since Epoch One way to do that is: LocalDate date = datePicker.getValue(); // input from your date picker Locale locale = Locale.US; int weekOfYear = date.get(WeekFields.of(locale).weekOfWeekBasedYear()); And X = find weeks since Epoch to prevYear And then result = X + weekOfYear Although someway we can find out "week number since epoch", is there clean solution to find it using Java 8 ? UPDATE: Even above solution wont work as one week(always

Java 8 filter List of Map objects based on Map property to remove some duplicates

别等时光非礼了梦想. 提交于 2020-01-01 06:45:08
问题 Have a List<Map<String, Object>> allPoints = new LinkedList<>(); Each map contains a "name" key with a String value; Need to create a List<Map<String, Object>> expectedPoints There are duplicate names in the list; for these, want to keep the last one only. E.g. if the list has three items, and first and third items both have"name" with value "abc", the resulting list should only contain the second and third items from the original list. 回答1: One way to do it is by using an auxiliary map: Map

I run the docker images which start tomcat8 server but it don't start

时光总嘲笑我的痴心妄想 提交于 2020-01-01 05:47:13
问题 I have docker image which I create from my docker file. When I run the image it has able to run the tomcat server then the command prompt come back. That mean the process is terminated and I think the container stops. So when I see http://localhost:8080 no tomcat page is appear. So I am not able find actual what is the problem. I am actually trying to to build custom java8, tomcat8 and maven as environment and I want to deploy my maven project in that tomcat server. Bellow is the Dockerfile

Java 8 Lambda variable scope [duplicate]

大憨熊 提交于 2020-01-01 05:33:26
问题 This question already has an answer here : Why should variables inside a forEach loop not be changed? (1 answer) Closed 3 years ago . I have 2 code samples: int[] idx = { 0 }; List<String> list = new ArrayList<String>(); list.add("abc"); list.add("def"); list.add("ghi"); list.stream().forEach(item -> { System.out.println(idx[0] + ": " + item); idx[0]++; }); Working properly. While this code has compile error: int idx = 0; List<String> list = new ArrayList<String>(); list.add("abc"); list.add(

How to group elements of a List by elements of another in Java 8

[亡魂溺海] 提交于 2020-01-01 05:19:12
问题 I have the following problem: Given these classes, class Person { private String zip; ... public String getZip(){ return zip; } } class Region { private List<String> zipCodes; ... public List<String> getZipCodes() { return zipCodes; } } using the Java 8 Stream API, how do I obtain a Map<Person, List<Region>> based on whether the Region contains that Person 's zip code? In other words how do I group the regions by the people whose zip codes belong to those regions? I've done it in Java 7 the