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 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

Ask A Question

Stats

  • Questions 532k
  • Answers 532k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Storing a plist in the Documents directory is possible. You… May 17, 2026 at 12:14 am
  • Editorial Team
    Editorial Team added an answer Well I'm not familiar with flexigrid but taking a quick… May 17, 2026 at 12:14 am
  • Editorial Team
    Editorial Team added an answer You can use the information_schema special database, which has tables… May 17, 2026 at 12:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Let's say I have a class in my web app called class Foo. It
Say I have a file Foo.php: <?php interface ICommand { function doSomething(); } class
I recently read that Java now sports initialisation blocks like the following: class C
Foo = Class.new Foo.instance_eval do def instance_bar instance_bar end end puts Foo.instance_bar #=> instance_bar
A method I work with that is called tens of thousands of times started
I want to know if I can tell what appdomain a object was created
I'm sure this is covered in the documentation somewhere but I have been unable
How can I create a bean of type Class? I found a way using
I am using Cherrypy in a RESTful web service and server returns XML as
For a php project I use a Collection class to handle my objects and

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.