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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:28:29+00:00 2026-05-16T11:28:29+00:00

Hi i have a problem populating hashmap with a loop, i need to store

  • 0

Hi i have a problem populating hashmap with a loop, i need to store objects as a value and string as a key and if another element wants to be added to hashmap it should be checked against, if the value already exist, if so it should increment the the string key by 1 for example:

("JButton", JButtonObject);

another element wants to be added if again JButton it should be

("JButton1", JButtonObject);
...
...
...

my code:
Thanks guys

private void CreateInstance(java.awt.event.ActionEvent evt) {                                
    // add code here

    Object object = null;
    if (evt.getSource() == CreateInstance)
    {
        int[] selectedIx = ClassList.getSelectedIndices();
        for (int i=0; i<selectedIx.length; i++) {
                Object sel = ClassList.getModel().getElementAt(selectedIx[i]);
                try {
                      Class classDefinition = Class.forName(sel.toString());
                      object = classDefinition.newInstance();
                      //create name
                      String data = sel.toString();
                      String substring = data.substring(12);
                      //check if name is unique

                      //add to map
                      hm.put(substring, object);----- HERE IS THE PROBLEM
                      System.out.println();
                } 
                catch (InstantiationException e) {
                      System.out.println(e);
                  } 
                catch (IllegalAccessException e) {
                      System.out.println(e);
                  } 
                catch (ClassNotFoundException e) {
                      System.out.println(e);
             }
             if(object instanceof java.awt.Component){
                DesignWindow.add((java.awt.Component)object);
                DesignWindow.validate();
             }
             else{
                System.out.println("Error");
             }
        }
    }
}                        
  • 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-16T11:28:29+00:00Added an answer on May 16, 2026 at 11:28 am

    Maybe you should use another structure as everybody says. But for doing what you want to:

    String substring = data.substring(12);
    //check if name is unique
    
    // add this line
    String uniqueKey = findUniqueKey(hm, substring);
    
    //add to map with the calculated value
    hm.put(uniqueKey, object);----- HERE IS THE PROBLEM
    

    and…

    private String findUniqueKey(Map<String, ?> map, String prefix)
    {
       if (!map.containsKey(prefix))
          return prefix;
       int i = 1;
       while(true) { // or check i against a maximum
          String candidate = prefix + i;
          if (!map.containsKey(candidate))
             return candidate;
          i++;
       }
    }
    

    This way you separate the logic for deciding the key from the rest of the code.

    Sidenote: Try to use Map references. Only use HashMap for creating the instance. So your code is not linked to a particular implementation of what you need: a Map.

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

Sidebar

Related Questions

I have a problem with populating an autocomplete list based on a previous input.
We have accounting software populating the name field on a magento webstore, the problem
Newbie here! My problem is as follows: I have a dynamically populating ul where
I have set up a HashMap like so: Map<String, ArrayList<String>> theAccused = new HashMap<String,
PROBLEM I have a nested PHP array that I need to populate from flat
I have a problem with formating text into TextView fields. I am populating strings
I have a bit of an odd problem. I have been populating a UITextView
Ive come across a nice problem recently, I have a Map LinkedHashMap<String, List<MyCustomObject>> I
I query my SQL database to populate my spinners, I have no problem populating
I have a problem with datagridview. Form 1 : populating data with datagridview ...(when

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.