DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Trending

  • Designing for Sustainability: The Rise of Green Software
  • Designing AI Multi-Agent Systems in Java
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  • Assessing Bias in AI Chatbot Responses
  1. DZone
  2. Coding
  3. Java
  4. Java Multimaps With Redis

Java Multimaps With Redis

Redis server provides many popular data structures like map, list, set... In practice, however, you may face more complicated requirements, such as storing several values per key, for example. The best practice for that would be implementing the Multimap.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Mar. 14, 16 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
18.8K Views

Join the DZone community and get the full member experience.

Join For Free

Redis server provides many popular data structures like map, list, set...

In practice, however, you as Java developer may face more complicated requirements, such as storing several values per key, for example. The best practice for that would be implementing the Redis based Multimap on Java.

You may need to spend some time to code such structure by yourself. Java code may look like this:

Map<MyKey, Set<MyObject>> setMultimap = new HashMap<>();

void put(MyKey key, MyObject obj) {
   Set<MyObject> list = setMultimap.get(key);
   if (set == null) {
       set = new HashSet<>();
       setMultimap.put(key, obj);
   }
   set.add(obj);
}

void remove(MyObject obj) {
   set<MyObject> set = setMultimap.get(key);
   if (set != null) {
      set.remove(obj);
   }
}

Getting this to work on top of Redis requires much more extra work: create Redis connection, execute Redis command, establish one-way relation between Redis map key and Redis list name, and so on ...

Or no work at all if you are using the Redisson - Redis based In-Memory Data Grid for Java. It takes care of all the tedious jobs for you. With implementation of a set of standard Java objects: Map, List, Set, Lock ... backed by Redis, it offers Multimap object too.

There are two flavors of Multimap objects available:

  1. RListMultimap - List based multimap, values bounded to key stored in List structure.

  2. RSetMultimap - Set-based multimap, values bounded to key stored in Set structure.

Let's take a look at the following Java code of using the Redis based RSetMultimap object:

Config config = new Config();
config.useClusterServers()
    // redis cluster nodes
    .addNodeAddress("127.0.0.1:7000", "127.0.0.1:7001");

RedissonClient redisson = Redisson.create(config);

RSetMultimap<String, String> setMultimap = redisson.getSetMultimap("myFish");

// Adding items

setMultimap.put("favoriteFish", "Flagfin");
setMultimap.put("favoriteFish", "Shiner");
setMultimap.put("favoriteFish", "Ladyfish");
setMultimap.put("oceanFish", "Shark");
setMultimap.put("oceanFish", "Ocean sunfish");

// Removing item

setMultimap.remove("oceanFish", "Shark");
setMultimap.remove("favoriteFish", "Flagfin");

// Getting all items

Set<String> favoriteFish = setMultimap.get("favoriteFish");

// Getting size

// total entries amount
setMultimap.size(); // 5
// total values amount by key
setMultimap.get("favoriteFish").size(); // 3

// check entry existence
setMultimap.containsEntry("favoriteFish", "Ladyfish");

// and so on ...

It's important to note that in Redisson object type can be any Java type, not limited to only String.

Redis (company) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!