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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:29:44+00:00 2026-05-27T16:29:44+00:00

So I am trying to put a string of words as a key into

  • 0

So I am trying to put a string of words as a key into a map, and a set of strings as a value into the map. Person1 is just a string of words, and Person2 is a set.

Map<String, Set<String>> newMap = new TreeMap<String, Set<String>>();
Set<String> newSet = new TreeSet<String>();
newSet.add(person2);
map.put(person1, newSet);
//System.out.println(map);

So person1 is:

 Apples
 Apples
 Pears 
 Oranges
 Apples

and person 2 is:

 [Love]
 [Like]
 [Dislike]
 [Hate]
 [OK]

When I put person1 and person2 into the map and run the program, Java updates the value of the key (Apples could be Love, Like and OK, but since the last value is OK, it set it to OK)

{Apples = [OK]}
{Pears = [Dislike]}
{Oranges = [Hate]}

What I want is this:

{Apples = [Love, Like, OK]}
{Pears = [Dislike]}
{Oranges = [Hate]}

Is this possible? If so, how?

  • 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-27T16:29:45+00:00Added an answer on May 27, 2026 at 4:29 pm

    You need to retrieve the Set already associated with the key and add the new word to that set. Only if the key does not have a Set already should you create a new Set.

    Set<String> set = map.get(person1);
    if (set == null) {
        set = new TreeSet<String>();
        map.put(person1, set);
    }
    set.add(person2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse words out of a string and put them into an
Instead of trying to trying to put my problem into words, here's some code
Here's what I'm trying to do: - Retrieve a Map of key-value pairs from
I am trying to parse thru a csv string, put the results into a
I'm trying to put together something to decode a Json string and not having
I am trying to put together a proof of concept of an XSS-safe string
I'm trying put an if statement directly into a select field in rails, with
I'm trying to select the most frequently occurring key words on a table. I
I'm trying to put a key in sequence and am being bombarded with errors.
I have some API instructions I am trying to follow (string) source (string) key

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.