Compress and decompress byte[] in java

Here is a simple utility that compresses and decompresses a byte[] array. I have been using this utility to compress serialized objects before storing to and after retrieving them from the database. I have found it very useful. I have added a main method to just show that how the compression works. output:

Convert int[] to Integer[] and vice versa in java

Here is a simple example to convert primitive int array to Integer array and vice versa Using regular for-loop And the output is : Java 8 Using Stream APIs of Java 8. Commons Arrayutils If you are not using java 8 and still want to have a oneliner. Use ArrayUtils from commons-lang3. References http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html https://en.wikipedia.org/wiki/Primitive_wrapper_class … [Read more…]

JavaFx Helloworld

In this example we will create a simple Helloworld application for JavaFx. I have added inline comments to the code. And here is the output   Notes In case you are using this HelloWorld in eclipse and you get the below warning. Access restriction: The type Pane is not accessible due to restriction on required … [Read more…]

Distribute your code in a unique style

So let’s have a look at a very interesting way of loading a class. UnIntrestingClass.java We have a very simple class. There is nothing special about it. It just prints text in constructor. ByteCodeGenerator.java This class prints the bytecode of the UnIntrestingClass. This class just shows how to print the bytecode. It prints InterestingLoadClass.java Let … [Read more…]

Convert byte[] array to String in Java

In this tutorial we will see how to convert a byte[] to String. It can be useful in cases where a String is stored as a byte[] array and needs to be converted to String. Java API Used This can be easily achieved by using the constructor provided by String class that accepts the byte[] … [Read more…]

Java abs() Method

Math.abs() method gives the absolute value of the passed argument. This method accepts arguments of type  int, float, long, double. Avaliable methods   Simple Example output:   Real World Example Problem I need the difference between two dates in minutes. The result should be always a positive number. Solution So here is my utility method … [Read more…]

Two Sum (Java) – Leetcode

Problem: Given an array of integers, return indices of the two numbers such that they add up to a specific target. Example   Solution Java Solution Output Analysis Time complexity depends on the put and containsKey operations of HashMap which is normally O(1). Time complexity of this solution is O(n). Space complexity depends on the … [Read more…]