All posts in "Java"

HashMap: How to get keys from value in Java?

June 19, 2017

To get a set of keys from a value in Java, we can use the below method:

The method getKeysByValue accepts two arguments. The first one is a map in which we want to look for the keys. The second is a searched value. At the beginning of it, we define the set named keys. Nextly, we […]


How to cast Double to Integer in Java

June 15, 2017

To cast Double (not-primitive) value to Integer we can use the intValue() method:

  To cast double to int you can use the casting operator

  You can find a detailed description below.   1. Object type

It is worth to say that we don’t need to create an object with the new […]


How to directly initialize a HashMap in Java

May 30, 2017

To initialize a HashMap in Java you can use a below syntax:

However, there are also other options. You can find a detailed description below 1. Static initializer

In this option, we declare a static final map with String key and values. Then we make a static initializer and put needed things in it manually. […]


Encode String to UTF-8 in Java

April 28, 2017

In this short post, we will discuss the String encoding. 1. How do I encode a string to UTF-8? You cannot.  String objects in Java do not have an encoding. Though, that’s not accurate enough because it’s encoding is UTF-16, and it cannot be changed in any way. 2. Where is encoding then? The only thing you can […]


Using switch on Strings in Java

April 10, 2017

In this post, we will learn something about using a switch on String in Java. The way you can do that depends on JDK level you are using. 1. JDK 6 or less Switch statement works with byte, short, char and int primitive data types. It also works with enumerated types. But it doesn’t work with String. So, […]


How to get current stack trace in Java

April 7, 2017

In general, we can get our stack trace by calling a method:

1. What a stack trace is? The stack trace is a report of methods calls in our application. So in simple words: it can tell us where exactly our program is at the point of doing something. Sometimes it is needed when we need […]


How to generate MD5 hash in Java

March 29, 2017

Today, we are going to learn how to generate MD5 hash. This algorithm is useful for verification e.g. files downloaded from the Internet or even hide our beloved password. However, in this short tutorial, we will read up with the second option.

At the very beginning it is good to set our password:

After that, we […]


Iterate through a HashMap in Java

March 22, 2017

In Java, we can iterate through a HashMap in few different ways. First of all, we need to import required packages:

Then, naturally, set our HashMap and put some data in there:

1. Values and Keys The simplest way to iterate is using keys or values. Using values:

If we use keys the […]


Weak and soft reference differences in Java

March 21, 2017

The main difference between WeakReference and SoftReference is the moment of removal by a garbage collector. 1. WeakReference WeakReference objects are always released when garbage collector reclaim.

The output of the above code always be:

2. SoftReference As long as your program has a lot of unused memory, soft references will be kept in memory. […]


How to iteratate over Enum in Java.

March 14, 2017

To iterate over enum, you have to use values() method. When using values() method, you can iterate through enum as if it was an array. In this example, I use this enum.

Java 7 or lower example 1. Get element by index.

2. Iterate using enhanced loop. In the belove example dayOfTheWeek holds […]

Page 2 of 4

Privacy Preference Center