Download and extract the JDK tar.gz files.
Lets assume extracted JDK files are java-7-oracle and java-8-oracle for JDK 7 and JDK 8 respectively.
Move above directories to /usr/lib/jvm
Run below commands.
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-oracle/bin/java 10
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-oracle/bin/java 11
Value 10 and 11 are priorities.
Run below commands to see the details of java versions installed so far.
$ update-alternatives --list java
$ update-alternatives --display java
To switch between different java versions, run below command.
$ update-alternatives --config java
Choose the selection number of java version to be active when prompted.
Run below command to confirm if java version is the one as per your selection above.
$ java -version
Run below command to remove an installed java version.
Below command uninstalls java-7-oracle
$ sudo update-alternatives --remove java /usr/lib/jvm/java-7-oracle/bin/java