Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to use net.jodah.ExpiringMap Maven Java Utility to Remove Expired Objects from HashMap Automatically – Complete Java Tutorial

$
0
0

How to use Expiringmap Maven Java Utility to Remove Expired Objects from HashMap Automatically - Complete Java Tutorial

Java without Java Collection is really hard to imagine. I spend almost couple of hours a day working on Java Projects for my clients.

HashMap, Map, Static Objects, Java Interface are the most commonly used terms.

Sometime back I’ve written an article on How to remove elements from HashMap Automatically using Java Timer and Futures Object?

In this tutorial, we will go over similar concept but not using Timer but ExpiringMap 🙂

java – Map with automatically expiring elements

Let’s get started:

Step-1.

Create Java Class CrunchifyNetJodahExpiringMapExample.java.

Step-2.

Add Maven net.jodah.expiringmap Dependency.

If you don’t see pom.xml file into your Eclipse Environment then follow this steps.

<dependency>
    <groupId>net.jodah</groupId>
    <artifactId>expiringmap</artifactId>
    <version>0.5.7</version>
</dependency>

Step-3

Create object curnchifyMap with Expiration time set to 5 seconds.

Step-4

  • Add an element to crunchifyMap every second
  • We will also print the number of element for that map using size() method

Step-5

As you will see in Output, there will not be more than 5 elements over 5 seconds into crunchifyMap.

Complete Java Example:

package crunchify.com.tutorial;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import net.jodah.expiringmap.ExpiringMap;

/**
 * @author Crunchify.com
 * 
 *         - ExpiringMap is a high performance, thread-safe ConcurrentMap implementation that expires entries. 
 *         - Ideally no performance overhead without any external dependency
 * 
 */

public class CrunchifyNetJodahExpiringMapExample {

	// Create Exipring Map called crunchifyMap with expiry time 5 seconds
	private static Map<String, Double> crunchifyMap = ExpiringMap.builder().expiration(5, TimeUnit.SECONDS).build();

	public static void main(String[] args) {
		{
			// Let's keep running loop for testing
			while (true) {
				try {

					// Just wait for a second everytime
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				// Add Element to Map crunchifyMap
				addElement();

				// Print Element to Map crunchifyMap
				printElement();
			}

		}
	}

	private static void printElement() {

		log("crunchifyMap Size: " + crunchifyMap.size() + "\n");
	}

	// NOTE: We are adding Unique Element to Map Every time.
	private static void addElement() {

		double randomValue = Math.random();

		// If the specified key is not already associated with a value (or is mapped to null) associates it with the
		// given value and returns null, else returns the current value.
		crunchifyMap.putIfAbsent("Crunchify " + randomValue, randomValue);
		
		log("+++++ Element added to crunchifyMap:" + randomValue);

	}

	private static void log(String string) {
		System.out.println(string);

	}
}

Console Output:

+++++ Element added to crunchifyMap:0.217495756642075
crunchifyMap Size: 1

+++++ Element added to crunchifyMap:0.3848009195646136
crunchifyMap Size: 2

+++++ Element added to crunchifyMap:0.7844845177791825
crunchifyMap Size: 3

+++++ Element added to crunchifyMap:0.19386945925964516
crunchifyMap Size: 4

+++++ Element added to crunchifyMap:0.7279066292650116
crunchifyMap Size: 5

+++++ Element added to crunchifyMap:0.6664624769347492
crunchifyMap Size: 5               <==========  check this: map size fixed to 5 as element expires every 1 seconds too

+++++ Element added to crunchifyMap:0.8867273405922911
crunchifyMap Size: 5

+++++ Element added to crunchifyMap:0.06620783012520237
crunchifyMap Size: 5

Ask a question in Forum for faster update OR have a suggestion on article? Share it as a comment.

The post How to use net.jodah.ExpiringMap Maven Java Utility to Remove Expired Objects from HashMap Automatically – Complete Java Tutorial appeared first on Crunchify.

Author: App Shah

Crunchify, LLC Logo


Viewing all articles
Browse latest Browse all 1037

Trending Articles