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

  • Home
  • SEARCH
  • 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 917585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:14:03+00:00 2026-05-15T18:14:03+00:00

I created a class Foo that has the method toArray() that returns an Array<Int>

  • 0

I created a class Foo that has the method toArray() that returns an Array<Int>.

Now, I have a HashMap mapping Strings to HashMaps, which map Objects to Foo. That is:

HashMap<String,HashMap<Object,Foo>>

And I want to create a new object of type:

HashMap<String,HashMap<Object,Array<Int>>>

That is obtained by calling the function toArray() for every element Foo in the original HashMAp.

To do so I normally would do something like:

    public static HashMap<String,HashMap<Object,Array<Int>>> changeMap(Map mpOld) {
        Object key2;
        String key1;
        Iterator it2;
        HashMap<String,HashMap<Object,Array<Int>>> mpNew= 
            new HashMap<String,HashMap<Object,Array<Int>>>()
        Iterator it1 = mpOld.keySet().iterator();
        while (it1.hasNext()) {
            key1=it1.next();
            it2= mpOld.get(key1).keySet().iterator();
            mpNew.put(key1,new HashMap<Object,Array<Int>>())
            while (it2.hasNext()) {
                key2=it2.next();
                mpNew.get(key1).put(key2,mpOld.get(key1).get(key2).toArray());
                //TODO clear entry mpOld.get(key1).get(key2)
            }
            //TODO clear entry mpOld.get(key1)
        }
        return mpNew;
    }

A similar code works just fine, but the Size of the HashMap is too big to hold two of them in memory. As you can see I added two points where I want to clear some entries. The problem is, if I do, I get either a concurrency error, or the iterator loop just terminates.

I wonder if there is a better way to iterate through the Maps and copy the information.

Also, I’m working in a Scala project but here I have to use Java types for some compatibility issues. Although Java.util.HashMap is not an iterator, maybe Scala has some hidden functinality to deal with this?

Thanks,

  • 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-05-15T18:14:04+00:00Added an answer on May 15, 2026 at 6:14 pm

    Iterators offer remove(..) methods that safely removes the previously accessed item. Iterate over the Key/Value entries of the map, converting them and adding them to the new map, and removing the old ones as you go.

    /**
     * Transfers and converts all entries from <code>map1</code> to 
     * <code>map2</code>.  Specifically, the {@link Foo} objects of the 
     * inner maps will be converted to integer arrays via {@link Foo#toArray}.
     * 
     * @param map1 Map to be emptied.
     * @param map2 Receptacle for the converted entries.
     */
    private static void transfer(Map<String, Map<Object, Foo>> map1
            , Map<String, Map<Object, int[]>> map2) {
    
        final Iterator<Entry<String, Map<Object, Foo>>> mapIt
            = map1.entrySet().iterator();
        while (mapIt.hasNext()) {
            final Entry<String, Map<Object, Foo>> mapEntry = mapIt.next();
            mapIt.remove();
            final Map<Object, int[]> submap = new HashMap<Object,int[]>();
            map2.put(mapEntry.getKey(), submap);
            final Iterator<Entry<Object,Foo>> fooIt 
                = mapEntry.getValue().entrySet().iterator();
            while (fooIt.hasNext()) {
                final Entry<Object,Foo> fooEntry = fooIt.next();
                fooIt.remove();
                submap.put(fooEntry.getKey(), fooEntry.getValue().toArray());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Foo that has a method Bar makeBar(String id) . As
Let's say I have created two objects from class foo and now want to
Let's say I have a class Foo in Java that has immutable data: class
if I have a class that has, for instance a HashMap that is initialized
I have a base class that has a method that creates an instance of
I have a class whose new method has been made private because I want
I have a class that has hashes in various stages of completion. This is
I have a class Foo which has several methods like button_0_0 , button_0_1 ,
I have a class Exporter which has a generic method which accepts an IEnumerable<T>
Assume I have a Class Foo which has many internal variables, only one constructor

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.