Home » Core Java » util » Page 7

util

java.util.concurrent.RejectedExecutionException – How to solve RejectedExecutionException

In this example we are going to talk about java.util.concurrent.RejectedExecutionException. When using an Executor to run your threads, it might reach a state where it cannot run the task you asked him to. This can happen for a number of reason, as we will demonstrate in this example. It is worth noting that this is a java.lang.RuntimeException       ...

Read More »

Java List Contains Method Example

In this example we will discuss the java.util.List.contains method and see how to use it. The method signature in the Java interface List is : boolean contains(Object o). As per the Java documentation, it returns true if and only if this list contains at least one element’s equals method returns true for object which we are searching for. Lets see ...

Read More »

Java Array to List Example

In this example I will show you how to convert an array into a java.util.List. We will use the java.util.Arrays class to convert an Object Array into a List of Objects. Lets see the example: 1) Object Array to List. A List can only store objects and not the primitives. So, lets how we can convert an array of objects into ...

Read More »

Java 8 Base64 Encoding Example

This article is about encoding Strings using Java. Until now, this was only possible by using external libraries, since Java update 8, encoding and decoding is part of the standard JRE. In old Java versions, it was needed to use external libraries like commons-codec or sum.misc.BASE64Decoder for encoding and decoding Strings. Now, the package java.util already contains a couple of ...

Read More »

Java 8 Parallel Arrays Example

In this article we are going to talk about the new possibilities offered in Java 8 for execute Arrays operations in parallel. We are going to focus on the Arrays class methods. At the end of this article you can download all the examples and snippets (and more). All code has been implemented using Java 8 update 11 and Eclipse ...

Read More »

Java Sorted Map Example

In this example we shall show you how to make use of Java Sorted Map. A SortedMap is a Map that sort its entries in ascending order according to the keys’ natural ordering, or according to a Comparator provided at the time of the SortedMap creation. All keys inserted into a SortedMap must implement the Comparable interface (or be accepted by the specified Comparator). Furthermore, all such elements must be mutually comparable (i.e, Mutually ...

Read More »

Java Get current date and time

There are a couple of ways to get the current time and date in Java. You may get the number of milliseconds since January 1, 1970, 00:00:00 GMT using System.currentTimeMillis() and calculate the time by yourself (a problem given to beginners, but too much work if you are developing something), you may use a library not included in the JDK ...

Read More »

Sorted List Java Example

In this article, we will create a Java example on Sorted List. I will show how to sort a java.util.List using methods of java.util.Collections class. 1. SimpleSortExample Create a Java class called SimpleSortExample with the following source code. SimpleSortExample.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 ...

Read More »

Java 8 Concurrency Tutorial

This article is about concurrency and parallel processing features in Java update 8. It is necessary to mention that concurrency is a very complicated matter and depends strongly in the hardware used for testing and benchmarking. The purpose of this article is not to compare performances using the different ways offered by Java but to list the new options that ...

Read More »

Java CountDownLatch Example

In this example we will discuss about the class java.util.concurrent.CountDownLatch. java.util.concurrent.CountDownLatch is a synchronization mechanism that allows one or more threads to wait until a set of operations being performed in other threads completes. 1. How CountDownLatch works A java.util.concurrent.CountDownLatch is initialized with a given count. The constructor of java.util.concurrent.CountDownLatch is defined as follows:     CountDownLatch(int count) Constructs a ...

Read More »