java-8

MalformedInputException with Files.readAllLines()

倖福魔咒の 提交于 2020-01-01 10:48:33
问题 I was iterating over some files, 5328 to be precise. These files are average XML files with 60-200 lines max. They are first filtered through a simple method isXmlSourceFile that parse the path. Files.walk(Paths.get("/home/me/development/projects/myproject"), FileVisitOption.FOLLOW_LINKS) .filter(V3TestsGenerator::isXmlTestSourceFile) .filter(V3TestsGenerator::fileContainsXmlTag) The big question is for the second filter, especially the method fileContainsXmlTag. For each file I wanted to

How should I be using LambdaMetaFactory in my use case?

╄→гoц情女王★ 提交于 2020-01-01 10:18:01
问题 Despite having read all the documentation I'm aware of, I cannot resolve an issue with using lambdas to execute a method. To give a bit of background my use case is a plugin system. I'm using an annotation (@EventHandle) which can be assigned to any method. I use reflection and iterate through every method in the class and check if it has the annotation, if it does the method is added to a handler object (which is added to a list for processing every "tick"). Here is my handler class: package

How should I be using LambdaMetaFactory in my use case?

别等时光非礼了梦想. 提交于 2020-01-01 10:17:09
问题 Despite having read all the documentation I'm aware of, I cannot resolve an issue with using lambdas to execute a method. To give a bit of background my use case is a plugin system. I'm using an annotation (@EventHandle) which can be assigned to any method. I use reflection and iterate through every method in the class and check if it has the annotation, if it does the method is added to a handler object (which is added to a list for processing every "tick"). Here is my handler class: package

ZonedDateTime to UTC with offset applied?

若如初见. 提交于 2020-01-01 10:07:56
问题 I am using Java 8 This is what my ZonedDateTime looks like 2013-07-10T02:52:49+12:00 I get this value as z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) where z1 is a ZonedDateTime . I wanted to convert this value as 2013-07-10T14:52:49 How can I do that? 回答1: Is this what you want? This converts your ZonedDateTime to a LocalDateTime with a given ZoneId by converting your ZonedDateTime to an Instant before. LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneOffset.UTC

Java JDK 8 IndexedPropertyDescriptor has changed since JDK 7 with List object

南笙酒味 提交于 2020-01-01 09:57:08
问题 I have a simple issue. I have a program working in Java JDK7 but it doesn't work in JDK8 because of some introspection changes. Here is a test program to reproduce the issue: import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo

Set opacity of a decorated JFrame in Java 8

会有一股神秘感。 提交于 2020-01-01 09:42:02
问题 I would like to know how to get a transparent JFrame in the latest version of Java. Currently, you can only use <JFrame>.setOpacity(); if the frame is not decorated . I have no use for an undecorated frame, so I'd like to know how to go around this restriction and set the opacity of the frame to 0.5f while still keeping the title bar, resize options etc. I have read the docs here: http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html. The code only worked on Java 6 and

Java 8: How to turn a list into a list of lists using lambda

心不动则不痛 提交于 2020-01-01 09:18:22
问题 I'm trying to split a list into a list of list where each list has a maximum size of 4. I would like to know how this is possible to do using lambdas. Currently the way I'm doing it is as follow: List<List<Object>> listOfList = new ArrayList<>(); final int MAX_ROW_LENGTH = 4; int startIndex =0; while(startIndex <= listToSplit.size() ) { int endIndex = ( ( startIndex+MAX_ROW_LENGTH ) < listToSplit.size() ) ? startIndex+MAX_ROW_LENGTH : listToSplit.size(); listOfList.add(new ArrayList<>

Lambda expression for supplier to generate IntStream

一曲冷凌霜 提交于 2020-01-01 09:04:35
问题 How do I replace the Supplier code here with lambda expression IntStream inStream = Stream.generate(new Supplier<Integer>() { int x= 1; @Override public Integer get() { return x++ ; } }).limit(10).mapToInt(t -> t.intValue()); inStream.forEach(System.out::println); The output of above piece of code is: 1 2 3 4 5 6 7 8 9 10 回答1: Something like this if you're bound to use Stream.generate specifically : IntStream inStream = Stream.generate(new AtomicInteger(1)::getAndIncrement) .limit(10)

Java 8 reference to a static method vs. instance method

半城伤御伤魂 提交于 2020-01-01 08:47:33
问题 say I have the following code public class A { int x; public boolean is() {return x%2==0;} public static boolean is (A a) {return !a.is();} } and in another class... List<A> a = ... a.stream().filter(b->b.isCool()); a.stream().filter(A::is); //would be equivalent if the static method is(A a) did not exist the question is how do I refer to the instance method version using the A::is type notation? Thanks a lot 回答1: In your example, both the static and the non-static method are applicable for

Is it a bug that Java 8's HashMap misbehaves if the keys implement Comparable in a way that isn't consistent with equals?

ぃ、小莉子 提交于 2020-01-01 08:38:28
问题 I know that since Java 8, if a HashMap has enough hash collisions, and the keys implement Comparable , it will use a balanced tree instead of a linked list for the bin. But from what I can see, the Comparable interface does not require that compareTo() be "consistent with equals() " (though it is strongly recommended). Did I miss something? It seems like the new implementation allows HashMap to violate the requirements of the Map interface if the keys happen to have a compliant, but non