All posts in "Java"

6 Ways To Use forEach in Java

August 23, 2020

To use for each in Java, you need to use the syntax like

Let’s have a look at some more examples of forEach in Java.   1. forEach and list Using forEach, you can conveniently iterate over elements in a list. Have a look at the example below

The last two lines show […]


How to convert float to int in Java?

May 17, 2018

You can convert a float to an int in Java by using the round method Math.round(float):

The method rounds the float to the closest int value. Other methods that you can use to convert a float to int are: (int) Math.ceil(float) – returns the smallest int which is greater than or equal to the method […]


Initialize String[] in Java

April 10, 2018

To initialize String[] you can do:

More details are given below. Declaration vs Initialization Let’s consider the array strings:

The above line of code is just a declaration of an array. There is no memory allocated for the elements of strings. If you would try to assign some value to the first element of […]


Java Sorting Map by key

March 9, 2018

To have a sorted map by key in Java you can use the TreeMap. It guarantees the natural order. You can use it like this:

For more details read the following paragraphs. TreeMap with the natural order This type of a Map stores the elements in the natural order. If you just want to sort a variable […]


How to convert String to Character array in Java

October 16, 2017

Using ArrayUtils If you are familiar with Apache Commons you can try this code:

The toCharArray() method will converts the String to char array. The ArrayUtils.toObject(String) method to converts the array of chars to the array of Character objects. Own method If for some reason you don’t want to use the Apache solution, you can write your […]


How to get file extension in Java?

August 31, 2017

Simple Java parser If you want to get a file extension from the file path you can try this code:

The fileName is a variable which contains the name of your file. lastIndexOf() will give you the index of the last dot in a file name. The if statement guarantees that you will get a substring […]


How to set proxy for JVM

August 28, 2017

Java applications connect to the Internet very often. If you want to use the proxy server to connect to the Internet, you need to set your JVM to use the proxy. To do this, you can set: http.proxyHost and http.proxyPort. These are JVM flags. We will explain how to set them correctly. JVM flags You can […]


org.apache.maven.plugin.PluginResolutionException: Plugin or one of its dependencies could not be resolved

August 23, 2017

If you are struggling with the error:

You should check the maven repository server is up firstly. It can be the root of the problem. Of course, sometimes the server is up, and you still get the error. That’s why there are more ways to deal with it in paragraphs below. Set up the […]


How to create an InputStream from a String in Java?

August 22, 2017

This short post will present how to convert String to InputStream in Java. 1. The String.getBytes() method with ByteArrayInputStream constructor

At the beginning, we declared a String and used the String.getBytes() method to convert it to the bytes array. We created a new InputStream object by using the constructor of the ByteArrayInputStream class, passing the bytes array as […]


How to round a double to n decimal places in Java

July 21, 2017

To round a double to n decimal places, we need to use the BigDecimal.doubleValue() method. 1. Creating a proper method We can easily reach the rounding by creating the round method, like in the example below.

As you can see, this code allows us to round to a specific number of places. In the above example, […]

1 2 3 4
Page 1 of 4