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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:33:32+00:00 2026-06-05T19:33:32+00:00

If map is defined as private Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Updating

  • 0

If map is defined as

private Map<Integer, Integer>   map = new HashMap<Integer, Integer>();

Updating the key works fine as shown below

public void increaseCountInFile(Integer hashCode) {
    if (mapContains(hashCode)) {
        increaseCount(hashCode);
    } else {
        map.put(hashCode, 1);
    }
}

private void increaseCount(Integer fileHashCode) {
    Integer key = map.get(fileHashCode);
    map.remove(fileHashCode);
    map.put(fileHashCode, ++key);
}

private boolean mapContains(Integer fileHashCode) {
    return map.containsKey(fileHashCode);
}

However, i wonder, can the same be done without having to remove the element from the map? (map.remove(fileHashCode);)

++(map.get(fileHashCode)); Looks nice but Java does not like it

  • 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-05T19:33:34+00:00Added an answer on June 5, 2026 at 7:33 pm

    You don’t need

    map.remove(fileHashCode);
    

    put() will automatically overwrite the entry. You can shorten your increaseCount() method like this:

    private void increaseCount(Integer fileHashCode) {
        map.put(fileHashCode, map.get(fileHashCode)+1);
    }
    

    Honestly though, I think you’re adding too much abstraction in your code. It really should just be one method unless you have a very good reason otherwise:

    public void increaseCountInFile(Integer hashCode) {
        if(map.containsKey(hashCode)) {
            map.put(hashCode, map.get(hashCode)+1);
        } else {
            map.put(hashCode, 1);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dispatch map defined as such: private Dictionary<string, Func<DynamicEntity, DynamicEntity, IEnumerable<DynamicEntity>, string>>
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
I've got this snippet of code: (define-key lisp-interaction-mode-map (kbd C-c C-e) (lambda () (let
I've got a snippet I want to bind to a key: (define-key (lisp-interaction-mode-map) (kdb
If you had two snippets: (global-set-key \C-d delete-char) and (define-key global-map \C-d delete-char) Is
I have this mapping defined in my Application Layer: public IList<ProfessionDTO> GetAllProfessions() { IList<Profession>
I'm trying to map my Hashmap in Hibernate. All examples I can find are
The classical programmer in me is in love with the public/private paradigm of OO
How do I map a String to a statically defined array of ints? I
public class foo { private String _name; private String _bar; public String getName() {

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.