JavaRush /Java Blog /Random EN /Coffee break #149. How to upgrade from Java 8 to Java 17....

Coffee break #149. How to upgrade from Java 8 to Java 17. Different ways to iterate over a list in Java

Published in the Random EN group

How to migrate from Java 8 to Java 17

Source: Medium Using this publication, you can easily upgrade from Java 8 to Java 17. Coffee break #149.  How to upgrade from Java 8 to Java 17. Different ways to iterate over a list in Java - 1 Each new release of Java, especially if we are talking about the long-term support (LTS) version, provides developers with many new features and functions, eliminating previously discovered errors and improves performance. But so far, despite the fact that Java 17 was released almost a year ago, the most popular version of the language is still Java 8. Many are hesitant to upgrade, believing that the new version of Java is not fully compatible with previous versions, and upgrading to it will require a lot of effort. Personally, I think it's necessary to upgrade. I'm already using Java 17, although I did have some problems with the upgrade. In addition, it is worth considering that Java 8 has long ceased to be a free service. Java 17 performance is much better. For example, by using ZGC, you no longer have to worry about configuring garbage collection settings. If you want to migrate from Java 8 to Java 17, you'll have to do some extra work. Since I have already gone this route, I can share my experience in migrating to the new version.

Update the version of the Maven compiler plugin

For some developers, the version of the Maven compiler plugin may be too old. If you want to work with Java 17, the Maven compiler plugin version must be at least 3.5.4.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin>

Update dependencies

The first step when upgrading from Java 8 to Java 17 is to update your dependencies. Since the dependency you originally used probably doesn't support Java 17 at all, you'll have to update it if you want to minimize the impact of the version upgrade. Otherwise, you may face a number of problems. 1. For example, Lombok will cause a compilation error. The error information is as follows:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project encloud-common: Fatal error compiling: java.lang.ExceptionInInitializerError: Unable to make field private com.sun .tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs accessible: module jdk.compiler does not "opens com.sun.tools.javac.processing" to unnamed module
Since Lombok injects the generated code at compile time and uses the classes from the com.sun.tools.javac.* package , you need to update Lombok to the latest version. This will solve the problem. 2. The zookeeper connection will throw an UnresolvedAddressException error , the error information is as follows:
org.apache.zookeeper.ClientCnxn - Session 0x0 for server 10.0.*.*/<unresolved>:2181, unexpected error, closing socket connection and attempting to reconnect java.nio.channels.UnresolvedAddressException: null at sun.nio.ch.Net .checkAddress(Net.java:149) ~[?:?] at sun.nio.ch.Net.checkAddress(Net.java:157) ~[?:?] at sun.nio.ch.SocketChannelImpl.checkRemote(SocketChannelImpl .java:816) ~[?:?] at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:839) ~[?:?] at org.apache.zookeeper.ClientCnxnSocketNIO.registerAndConnect(ClientCnxnSocketNIO.java:277 ) ~[zookeeper-3.4.13.jar:3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03] at org.apache.zookeeper.ClientCnxnSocketNIO.connect(ClientCnxnSocketNIO.java:287) ~[zookeeper-3.4.13.jar:3 .4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03] at org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1021) ~[zookeeper-3.4.13.jar:3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03] at org.apache.zookeeper.ClientCnxn$ SendThread.run(ClientCnxn. java:1064) [zookeeper-3.4.13.jar:3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03]
The reason for the error is that in Java 15 the toString method for InetSocketAddressHolder was refactored . If you are using maven, you can use the mvn versions:display-dependency-updates command to check for dependency updates. The result will look like this: Coffee break #149.  How to upgrade from Java 8 to Java 17. Different ways to iterate over a list in Java - 2

Add a dependency

If you compile with JAXB, you will receive an error. This is because JAXB has been removed from Java 11. So you need to add some JAXB Maven dependencies.
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml .bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId> jaxb-impl</artifactId> <version>2.3.0</version> </dependency>
In addition to this, Java 11 also removes Java EE and CORBA modules, so you will have to manually add javax packages.
<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency>

Handling InaccessibleObjectException

If you use reflection in your application, especially setAccessible(true) , then you will likely receive an InaccessibleObjectException error when the application starts. The error is caused by the Java platform's modular system, which allows access only under certain conditions:
  1. The class must be public.
  2. You must export your own packages.
To resolve this error, you can manually set the module to public. To do this, you need to add the --add-opens option to the Java startup command line as shown below.
# --add-opens has the following syntax: {modules}/{package}=ALL-UNNAMED java --add-opens java.base/java.lang=ALL-UNNAMED
Or you can change your Java code to finally solve this problem.
module java.base {
    exports java.lang;
}
Overall, after solving all the above issues, your application should run fine on Java 17. I hope this article will help you migrate to Java 17.

Different ways to iterate over a list in Java

Source: Rrtutors In this article, we will learn several ways to iterate over a list in Java. Coffee break #149.  How to upgrade from Java 8 to Java 17. Different ways to iterate over a list in Java - 3The Collection framework has a List interface that allows us to maintain an ordered collection of objects. The List interface is implemented by ArrayList, LinkedList, Stack, and Vector. A significant number of Java applications use ArrayList and LinkedList. There are several ways to iterate through a list, each of which has its own characteristics.

How to iterate over a list in Java

You can use three ways to iterate a list in Java:
  • Using the advanced For Loop method.
  • Using the iterator method.
  • Iterate over elements forEach.

1. Using the advanced For Loop method.

This is a version of the basic for loop that can be used to iterate over a list. Here's an example:
import java.util.ArrayList;
import java.util.List;
public class Enhanced_for_loop {
            public static void main(String[] args) {
        List<Integer> figurez = new ArrayList<>();
        figurez.add(100);
        figurez.add(200);
        figurez.add(300);
        figurez.add(400);
        for(int i: figurez) {
            System.out.print(i+ " ");
        }
            }
}
Conclusion:
100 200 300 400

2. Using the iterator method.

You can also use iterators to iterate over a list. Iterators are especially useful when you need to modify an iterable list. Example:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Iterate_List_using_Iterators {
            public static void main(String[] args) {
                        List<Integer> numbari = new ArrayList<>();
                        numbari.add(100);
                        numbari.add(200);
                        numbari.add(300);
                    Iterator<Integer> itr = numbari.iterator();
                    while(itr.hasNext()) {
                        int i = itr.next();
                        System.out.print(i+ " ");
                        if(i==3) {
                            itr.remove();
                        }

            }
            }
}
Conclusion:
100 200 300

3. Iterate over elements forEach.

After lambda functions were added to Java, the language syntax introduced the forEach function, which allows you to iterate over any collection. The following code example shows how you can use the forEach method to iterate over a list:
import java.util.ArrayList;
import java.util.List;
public class Iterate_List_using_ForeEach_iterable {
            public static void main(String[] args) {
                        List<Integer> numbari = new ArrayList<>();
                        numbari.add(1);
                        numbari.add(2);
                        numbari.add(3);
                        numbari.add(4);
                        numbari.forEach(System.out::println);
            }
}
Conclusion:
1 2 3 4
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION