Tags "java-8" Result

Java 8 UTF-8 encoding issue (java bug?)

There is an inconsistency when creating a String with UTF-8 encoding. Run this code: public static void encodingIssue() throws IOException { byte[] array = new byte[3]; array[0] = (byte) -19; array[1] = (byte) -69; array[2] = (byte) -100; String str = new String(array, "UTF-8"); for (char c : str.toCharArray()) {

it1352 1 2019-05-16

Java 8 ConcurrentHashMap

I've observed that ConcurrentHashMap has been entirely rewritten in Java 8 to be more "lock-free". I've browsed the code of the get() method and see that there is no explicit lock mechanism: public V get(Object key) { Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; int h = spread(key.hashCode()); if ((tab = table) != null

it1352 0 2019-05-22

Java 8 Optional

I want to check if a particular object size is greater than 0. If it is greater than 0 then I want to create an optional object, if not then I want to return an Optional empty. This is the long version of the java code: if(fooA.size>0) { return Optional.of(new Foo()); } else { return Optional.empty(); } Is there any way to compact thi

it1352 0 2019-05-22

Java 8 java.util.stream.Streams

I see many blog posts mention a Streams class and I see it was once part of the lambda branch API. It appears to be non-public API now and it does not match the previous implementation. Is there a different way to do Streams.concat() or to append multiple values to a stream? Solution The Streams class got split and some of its methods were moved t

it1352 0 2019-05-22

Java 8 change in UTF-8 decoding

We recently migrated our application to JDK 8 from JDK 7. After the change, we ran into a problem with the following snippet of code. String output = new String(byteArray, "UTF-8"); The byte array may contain invalid UTF-8 byte sequences. The same byte array upon UTF-8 decoding, results in two difference strings on Java 7 and Java 8. According

it1352 4 2019-05-22

Tomcat 8 Maven Plugin for Java 8

Is the tomcat7-maven-plugin working with a tomcat 8 server and java 8? I can't find any tomcat8-maven-plugin. Solution Yes you can, In your pom.xml, add the tomcat plugin. (You can use this for both Tomcat 7 and 8): pom.xml <!-- Tomcat plugin --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId

it1352 6 2019-05-22

Java 7u40 Java SE 8 sun.reflect.Reflection.getCallerClass

i have read the following post Oracle Discontinuing sun.reflect.Reflection.getCallerClass i was wondering what this change really means. 1). Means that this class sun.reflect.Reflection.getCallerClass will be rewritten to provide more security in Java reflection? 2). Means this class will no more be needed? maybe another approach? 3). Reflecti

it1352 1 2020-08-01

Close Java 8 Stream

If we use Java 8 Stream like list.stream().filter(....).collect(..)..... When is it closed this stream? Is it good practice that we close the stream us as the next example? Stream<String> stream = list.stream(); String result = stream.limit(10).collect(Collectors.joining("")); stream.close(); Solution It is generally not necessary to clo

it1352 1 2019-05-22

Java 8 partition list

Is it possible to partition a List in pure Jdk8 into equal chunks (sublists). I know it is possible using Guava Lists class, but can we do it with pure Jdk? I don't want to add new jars to my project, just for one use case. SOLUTONS: The best solution till now was presented by tagir-valeev: I have also found three other possibilities, but they

it1352 2 2019-05-22