Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:55:53+00:00 2026-06-07T23:55:53+00:00

I am using Apache Commons Collections to create a MultiKeyMap that will store two

  • 0

I am using Apache Commons Collections to create a MultiKeyMap that will store two keys with one corresponding value and then using MapIterator to walk through the map. The problem I’ve got is I need to break the keys returned by the MapIterator back into the individual keys rather than a single “composite”. Whilst I could split the string containing the “composite” key or use reflections, neither of these options seem very elegant.

To model the problem, I’ve created the following sample code

MultiKeyMap multiKeyMap = new MultiKeyMap();

multiKeyMap.put("Key 1A","Key 1B","Value 1");
multiKeyMap.put("Key 2A","Key 2B","Value 2");
multiKeyMap.put("Key 3A","Key 3B","Value 3");

MapIterator it = multiKeyMap.mapIterator();

while (it.hasNext()) {
    it.next();
    System.out.println(it.getKey());
    System.out.println(it.getValue());
}

it.getKey() returns MultiKey[Key 3A, Key 3B] but what I want to do is assign the keys to individual variables, something like myKey1 = it.getKey().keys[0] and myKey2 = it.getKey().keys[1] but I cannot find anything in the JavaDoc to achieve this.

Is it possible break the keys returned by the MapIterator into the individual keys without using reflections or manipulating the string returned by it.getKey()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:55:55+00:00Added an answer on June 7, 2026 at 11:55 pm

    I checked the source code for commons 3.2, and MultiKeyMap keys are instances of MultiKey, which has a getKey(int index) method. There is also a getKeys() method which returns Object[]. It requires a cast, but you could do:

    MultiKeyMap multiKeyMap = new MultiKeyMap();
    
    multiKeyMap.put("Key 1A","Key 1B","Value 1");
    multiKeyMap.put("Key 2A","Key 2B","Value 2");
    multiKeyMap.put("Key 3A","Key 3B","Value 3");
    
    MapIterator it = multiKeyMap.mapIterator();
    
    while (it.hasNext()) {
        it.next();
    
        MultiKey mk = (MultiKey) it.getKey();
    
        // Option 1
        System.out.println(mk.getKey(0));
        System.out.println(mk.getKey(1));
    
        // Option 2
        for (Object subkey : mk.getKeys())
          System.out.println(subkey);
    
        System.out.println(it.getValue());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using apache commons-net for downloading a file from an FTP server. That
I'm using Apache Commons Configuration library to store my app properties. I can monitor
In apache.commons.collections there is a class called MapUtils which have these two methods to
I have a java application on Websphere that is using Apache Commons FTPClient to
I am using apache commons IO for copying files and have imported that as
Using Apache Commons VFS , how do I monitor the progress of a file
I'm using Apache Commons FTP to upload a file. Before uploading I want to
I'm using Apache Commons Logging and SLF4J with log4j, but I also want to
I am using Apache Commons Email library to send emails, but I am not
I'm using Apache Commons e-mail validator and it refuses to accept email address like:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.