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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:55:35+00:00 2026-05-25T14:55:35+00:00

Why does java.util.Map.values() allow you to delete entries from the returned Collection when it

  • 0

Why does java.util.Map.values() allow you to delete entries from the returned Collection when it makes no sense to remove a key value pair based on the value? The code which does this would have no idea what key the value(and hence a key) being removed is mapped from. Especially when there are duplicate values, calling remove on that Collection would result in an unexpected key being removed.

  • 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-25T14:55:36+00:00Added an answer on May 25, 2026 at 2:55 pm

    it makes no sense to remove a key value pair based on the value

    I don’t think you’re being imaginative enough. I’ll admit there probably isn’t wide use for it, but there will be valid cases where it would be useful.

    As a sample use case, say you had a Map<Person, TelephoneNumber> called contactList. Now you want to filter your contact list by those that are local.

    To accomplish this, you could make a copy of the map, localContacts = new HashMap<>(contactList) and remove all mappings where the TelephoneNumber starts with an area code other than your local area code. This would be a valid time where you want to iterate through the values collection and remove some of the values:

    Map<Person, TelephoneNumber> contactList = getContactList();
    Map<Person, TelephoneNumber> localContacts = new HashMap<Person, TelephoneNumber>(contactList);
    
    for ( Iterator<TelephoneNumber> valuesIt = localContacts.values().iterator(); valuesIt.hasNext(); ){
        TelephoneNumber number = valuesIt.next();
        if ( !number.getAreaCode().equals(myAreaCode) ) {
            valuesIt.remove();
        }
    }
    

    Especially when there are duplicate values, calling remove on that Collection would result in an unexpected key being removed.

    What if you wanted to remove all mappings with that value?

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

Sidebar

Related Questions

val m: java.util.Map[String, Int] = ... m.foreach { entry => val (key, value) =
does anyone know if the following java Method in the java.util.concurrent Package ScheduledExecutorService.html#scheduleAtFixedRate() absolutely
I find it funny that Java (or the java.util library) does not have a
This is probably a quicky. Why does this code not return anything? import java.util.Scanner;
Is there any Java function or util class which does rounding this way: func(3/2)
How does scala.collection.JavaConversions supercede the answers given in Stack Overflow question Iterating over Java
Somewhat related to How to convert XML to java.util.Map and vice versa , only
Does java.util.Calendar supports automatic winter/summer time changes?
Why does the java.util.Set interface lack get(int Index) , or any similar get() method?
I'm looking for an on-disk implementation of java.util.Map . Nothing too fancy, just something

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.