java-8

Change annotation value of field annotation in Java 8

佐手、 提交于 2020-01-01 14:39:35
问题 I think title describes the problem. Here's some code: import static org.junit.Assert.assertEquals; import java.lang.annotation.*; public class Main { public static void main(String[] args) throws Exception { assertEquals("foo", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param()); // the magic here -> set to bar assertEquals("bar", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param()); } @Anno(param = "foo") public void myMethod() {} @Retention

Change annotation value of field annotation in Java 8

﹥>﹥吖頭↗ 提交于 2020-01-01 14:37:56
问题 I think title describes the problem. Here's some code: import static org.junit.Assert.assertEquals; import java.lang.annotation.*; public class Main { public static void main(String[] args) throws Exception { assertEquals("foo", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param()); // the magic here -> set to bar assertEquals("bar", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param()); } @Anno(param = "foo") public void myMethod() {} @Retention

How to get enclosed class? [duplicate]

家住魔仙堡 提交于 2020-01-01 13:28:22
问题 This question already has answers here : Java reflection: How can I retrieve anonymous inner classes? (2 answers) Closed 2 years ago . I know Class.getDeclaredClasses() can obtains all the classes that it has declared but doesn't including anonymous classes. I'd like to know is there a way to get all the enclosed classes via the enclosing class ? for example, I want to get the all enclosed classes defined in Root for test purpose. class Root{ void run(){ Runnable task = new Runnable(){ public

How to get enclosed class? [duplicate]

陌路散爱 提交于 2020-01-01 13:28:07
问题 This question already has answers here : Java reflection: How can I retrieve anonymous inner classes? (2 answers) Closed 2 years ago . I know Class.getDeclaredClasses() can obtains all the classes that it has declared but doesn't including anonymous classes. I'd like to know is there a way to get all the enclosed classes via the enclosing class ? for example, I want to get the all enclosed classes defined in Root for test purpose. class Root{ void run(){ Runnable task = new Runnable(){ public

Obtain last modification date/time of file as local date/time string

落爺英雄遲暮 提交于 2020-01-01 12:26:08
问题 new File(url).lastModified() returns a long equal to the number of milliseconds since the epoch, which is GMT-based. What is a simple way to convert this to a String representing system-local date/time? If you really need to see an attempt from me here it is but it's a terrible mess and it's wrong anyway: LocalDateTime.ofEpochSecond(new File(url).lastModified()/1000,0,ZoneOffset.UTC).atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)) Beyond LocalDateTime

Java 8: How to convert String to Map<String,String>?

情到浓时终转凉″ 提交于 2020-01-01 11:23:00
问题 I have a Map: Map<String, String> utilMap = new HashMap(); utilMap.put("1","1"); utilMap.put("2","2"); utilMap.put("3","3"); utilMap.put("4","4"); I converted it to a String: String utilMapString = utilMap .entrySet() .stream() .map(e -> e.toString()).collect(Collectors.joining(",")); Out put: 1=1,2=2,3=3,4=4,5=5 How to convert utilMapString to Map in Java8? Who can help me with? 回答1: Split the string by , to get individual map entries. Then split them by = to get the key and the value. Map

Java 8: How to convert String to Map<String,String>?

孤人 提交于 2020-01-01 11:20:33
问题 I have a Map: Map<String, String> utilMap = new HashMap(); utilMap.put("1","1"); utilMap.put("2","2"); utilMap.put("3","3"); utilMap.put("4","4"); I converted it to a String: String utilMapString = utilMap .entrySet() .stream() .map(e -> e.toString()).collect(Collectors.joining(",")); Out put: 1=1,2=2,3=3,4=4,5=5 How to convert utilMapString to Map in Java8? Who can help me with? 回答1: Split the string by , to get individual map entries. Then split them by = to get the key and the value. Map

Designing tail recursion using java 8

坚强是说给别人听的谎言 提交于 2020-01-01 10:52:04
问题 I was trying out the following example provide in the talk to understand the tail recursion in java8. @FunctionalInterface public interface TailCall<T> { TailCall<T> apply(); default boolean isComplete() { return false; } default T result() { throw new Error("not implemented"); } default T get() { return Stream.iterate(this, TailCall::apply).filter(TailCall::isComplete) .findFirst().get().result(); } } Utility class to use the TailCall public class TailCalls { public static <T> TailCall<T>

Designing tail recursion using java 8

青春壹個敷衍的年華 提交于 2020-01-01 10:51:44
问题 I was trying out the following example provide in the talk to understand the tail recursion in java8. @FunctionalInterface public interface TailCall<T> { TailCall<T> apply(); default boolean isComplete() { return false; } default T result() { throw new Error("not implemented"); } default T get() { return Stream.iterate(this, TailCall::apply).filter(TailCall::isComplete) .findFirst().get().result(); } } Utility class to use the TailCall public class TailCalls { public static <T> TailCall<T>

How to eliminate duplicate entries within a stream based on a own Equal class

假如想象 提交于 2020-01-01 10:49:32
问题 I do have a simialar problem like descripted here. But with two differences first I do use the stream api and second I do have an equals() and hashCode() method already. But within the stream the equalitity of the of Blogs are in this context not the same as defined in the Blog class. Collection<Blog> elements = x.stream() ... // a lot of filter and map stuff .peek(p -> sysout(p)) // a stream of Blog .? // how to remove duplicates - .distinct() doesn't work I do have a class with an equal