devxlogo

upload

Finding out the Java version.

This is generally useful, specifically if you want to perform some operations based on Java version and so on. public class JavaVersion{   public static void main(String args[])   {      JavaVersion javaVersion = new JavaVersion();      javaVersion.proceed();   }      private void proceed()   {      //This works in Java 8 and prior      //For Java 9, there is a new api available in Runtime class      String javaVersion = System.getProperty(“java.version”);        System.out.println(“Java Version: ” + javaVersion);    }} /* Expected output: [root@mypc]# java JavaVersionJava Version: 1.8.0_221 */

Updating a JAR file

At times, requirement to update an already delivered JAR file will need to be handled. Of course, JAR file is a way of packaging and delivering Java class file and related metadata files in a package. But having an easier way is always welcome by the developer group. The following command when executed on a command line will achieve updating a JAR file with 1 or more files as needed jar uf jar-file input-file-name(s) where uf          :    indicates update and filejar-file    :   the jar file that needs to be updatedinput-file-name(s)   :   the name of file(s) that needs to be updated in the jar file.

How to use JPA, @MappedSuperclass

First, define an abstract class and annotate it with @MappedSuperclass. This is not an entity: @MappedSuperclasspublic abstract class User implements Serializable {   …} Second, each entity should extend the User class. For example, Student and Teacher entitites: @Entitypublic class Student extends User implements Serializable {   …} @Entitypublic class Teacher extends User implements Serializable {   …}

How to define an auto-incremented identifier via JPA annotations

For defining an auto-incremented identifier in an entity we need the @Id annotation and the IDENTITY generator as follows: @Entitypublic class User implements Serializable {     @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    …}

Find if a string contains one of the given words in Java

We can use the AnyMatch method to figure out if a string contains any of the given words from an Array. See below for a sample.  List middleEasternCountries = Arrays.asList(“egypt”, “iran”, “turkey”);String sampleString = “Egypt is a famous tourist destination. It contains the Pyramids”; System.out.println(middleEasternCountries.stream().anyMatch(sampleString::contains));

Transpose a Matrix in Python

One of the inbuilt libraries in Python is zip. It can be utilized to transpose a matrix by performing an Unzip followed by zip. Sample code below.   Python comes with many inbuilt libraries zip is among those. The??zip()??function returns an iterator of tuples based on the??iterable??object. In order to get the transpose of the matrix first, we need to unzip the list using??*??operator then zip it. inputMatrix = [ [7, 14, 21], [1, 2, 3] ]zip(*inputMatrix) Output will be as follows:[ (7, 1), (14, 2), (21, 3) ]