java-8

How to get TYPE_USE annotations on a generic bound

£可爱£侵袭症+ 提交于 2020-05-10 18:48:10
问题 I have this case: public class SomeClass<T> { public <@A1 S extends @A2 T> @A3 S myMethod() { ...} } and I'm trying to get the @A2 annotation on the bound T . This is what I'm seeing, assuming myMethod is SomeClass.class.getDeclaredMethod("myMethod") . Type casts removed for readability. myMethod.getGenericReturnType().getAnnotations() returns @A1 (as expected, I guess) myMethod.getGenericReturnType().getBounds()[0].getAnnotations() returns nothing (?? shoudn't it be @A2 ??) myMethod

How to get TYPE_USE annotations on a generic bound

末鹿安然 提交于 2020-05-10 18:43:08
问题 I have this case: public class SomeClass<T> { public <@A1 S extends @A2 T> @A3 S myMethod() { ...} } and I'm trying to get the @A2 annotation on the bound T . This is what I'm seeing, assuming myMethod is SomeClass.class.getDeclaredMethod("myMethod") . Type casts removed for readability. myMethod.getGenericReturnType().getAnnotations() returns @A1 (as expected, I guess) myMethod.getGenericReturnType().getBounds()[0].getAnnotations() returns nothing (?? shoudn't it be @A2 ??) myMethod

Does synchronized block have max reentrant limit?

孤街醉人 提交于 2020-05-10 17:55:51
问题 As we know, ReentrantLock has a max reentrant limit: Integer.MAX_VALUE ; Does synchronized block have reentrant limit too? Update : I found it is hard to write test code for synchronized reentrant: public class SyncReentry { public static void main(String[] args) { synchronized (SyncReentry.class) { synchronized (SyncReentry.class) { // ...write synchronized block for ever } } } } Can anyone help write some code for synchronized reentrant limit test? 回答1: Since the specification does not

Java 8 Optional. Why of and ofNullable?

大兔子大兔子 提交于 2020-05-10 07:08:15
问题 I have a question regarding Java 8's Optional, the purpose of which is to tackle NullPointerException exceptions. The question is, what is the reason for having both types to let us choose: Optional.of(T value) <-----non-null value, null value will throw NPE Optional.ofNullable(T value) <----- nullable value Because what I expect is, when I use: Optional.of(nullValue); It won't throw a NullPointerException . Expanded my question after some replies: Why would people opt for Optional instead of

How to get the size of a Stream after applying a filter by lambda expression?

时光怂恿深爱的人放手 提交于 2020-05-10 06:56:09
问题 Consider the following code: List<Locale> locales = Arrays.asList( new Locale("en", "US"), new Locale("ar"), new Locale("en", "GB") ); locales.stream().filter(l -> l.getLanguage() == "en"); How do I get the size of the locales ArrayList after applying filter , given that locales.size() gives me the size before applying filter ? 回答1: When you get a stream from the list, it doesn't modify the list. If you want to get the size of the stream after the filtering, you call count() on it. long

How to get the size of a Stream after applying a filter by lambda expression?

只愿长相守 提交于 2020-05-10 06:54:43
问题 Consider the following code: List<Locale> locales = Arrays.asList( new Locale("en", "US"), new Locale("ar"), new Locale("en", "GB") ); locales.stream().filter(l -> l.getLanguage() == "en"); How do I get the size of the locales ArrayList after applying filter , given that locales.size() gives me the size before applying filter ? 回答1: When you get a stream from the list, it doesn't modify the list. If you want to get the size of the stream after the filtering, you call count() on it. long

How do I pass a method to an annotation using Java 8? [duplicate]

二次信任 提交于 2020-05-10 04:27:07
问题 This question already has answers here : Which types can be used for Java annotation members? (4 answers) Closed 2 years ago . I would like to pass a method to an annotation. Is something like this possible? @MyAnnotation(method = MyClass::myMethod) private String myVariable; 回答1: Passing a method isn't an option. Instead, pass the following which should allow you to find the method using reflection. @MyAnnotation(clazz=String.class, method="contains", params= {CharSequence.class}) @interface

java.time: Does the CET time zone considers daylight saving time?

家住魔仙堡 提交于 2020-05-10 02:03:50
问题 I use the new java.time implementation of Java 8 and wonder about the output of a UTC to CET time conversion result. ZonedDateTime utcTime = ZonedDateTime.of(2014, 7, 1, 8, 0, 0, 0, ZoneId.of("UTC")); ZonedDateTime cetTime = ZonedDateTime.ofInstant(utcTime.toInstant(), ZoneId.of("CET")); System.out.println("Summer-UTC-Time: " + utcTime); System.out.println("Summer-CET-Time: " + cetTime); System.out.println(); utcTime = ZonedDateTime.of(2014, 1, 1, 8, 0, 0, 0, ZoneId.of("UTC")); cetTime =

Jackson deserialize ISO8601 formatted date-time into Java8 Instant

僤鯓⒐⒋嵵緔 提交于 2020-05-09 20:30:09
问题 I'm trying to deserialize an ISO8601 formatted date into Java8 Instant using Jackson. I registered JavaTimeModule with the ObjectMapper, turned off WRITE_DATES_AS_TIMESTAMPS. However, if one tries to deserialize 2016-03-28T19:00:00.000+01:00 it will not work, because it seems that JavaTimeModule will only deserialize date-times formatted with UTC timezone offset (e.g. 2016-03-28T18:00:00.000Z). I then tried using @JsonFormat annotation like this: @JsonFormat(shape=JsonFormat.Shape.STRING,

GC (Allocation Failure) VS OutOfMemoryError Exception

夙愿已清 提交于 2020-05-08 03:07:38
问题 'OutOfMemoryError': Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap. GC (Allocation Failure): Allocation Failure” means that there is an allocation request that is bigger than the available space in young generation. Does this mean Allocation Failure will be thrown when Young generation memory is full (Minor GC) and "OutOfMemoryError" is thrown in full GC? 回答1: These could become related as far as I can tell; but they are entirely