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 8896201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:58:45+00:00 2026-06-14T23:58:45+00:00

So I run into a casting problem with storing HashMap properties to a Properties

  • 0

So I run into a casting problem with storing HashMap properties to a Properties file because the key of the HashMap is an object that cannot be cast to java.lang.String because properties must be (String, String) and my HashMap is (Object, Long).

I just want to save the contents of the HashMap to a file so I can load those contents back into the HashMap the next time the program is ran.

I have gone through an excessive amount of worthless attempts to try to refill HashMaps with objects they contained before they were saved. I tried to think of an easy way to convert the object to a string which I can do, but since Properties and HashMaps are not indexed, I can not change those strings back to the object they need to be. How to achieve this?

Here is what I am trying to do:

    public File savedHashMaps = new File("SavedHashMaps.list");
// in my case, the object is 'Location' from an implemented API
public Map<Location, Long> map = new HashMap<Location, Long>();

public void saveMaps() {
    Properties prop = new Properties();
    try {
        if (!map.isEmpty()) {
            prop.load(new FileInputStream(savedHashMaps));
            prop.putAll(map);
            prop.store(new FileOutputStream(savedHashMaps), null);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void loadMaps() {
    Properties prop = new Properties();
    try {
        prop.load(new FileInputStream(savedHashMaps));
        if (!prop.isEmpty()) {
            map.putAll((Map)prop);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 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-14T23:58:47+00:00Added an answer on June 14, 2026 at 11:58 pm

    Using an ObjectInput/OutputStream

    public File savedHashMaps = new File("SavedHashMaps.list");
    public Map<Location, Long> map = new HashMap<Location, Long>();
    
    public void saveMaps() {
        try {
            ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream(savedHashMaps));
            oos.writeObject(map);
            oos.close();
        } catch (Exception e) {
            // Catch exceptions
        }
    }
    
    public void loadMaps() {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(savedHashMaps));
            Object readMap = ois.readObject();
            if(readMap != null && readMap instanceof HashMap) {
                map.putAll((HashMap) readMap);
            }
            ois.close();
        } catch (Exception e) {
            // Catch exceptions
        }
    }
    

    This is from memory, so sorry for errors, however this will save and reload all your Location, Long mappings. Also as a note, the Location class must implement Serializable (or have a super-class that implements it)(It’s a marker interface (See java.dzone.com/articles/marker-interfaces-java) so you just add implements Serializable).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using solr's faceting and i've run into a problem that i was hoping
I run into strange problem and I cant find anything on web for that.
Run into a bit of an odd problem that is increasingly frustrating Scenario: I
I run into this problem. I have a textarea which I only want to
I run into follow problem. Did I miss anything? Association.all().count() 1 Association.all().fetch(1) [Association(**{'server_url': u'server-url',
I run into the following errors when i try to store a large file
I run into an interesting problem while was using combos in input form. My
I've run into a known problem, which is when Firefox's default search for some
I just run into another problem with my To-do-like sortable list made with Knockout
I have run into a problem with Eclipse Indigo, attempting to start up my

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.