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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:20:12+00:00 2026-05-27T12:20:12+00:00

Putting Hashmaps by reference and putting hashmaps by copy. How do i do the

  • 0

Putting Hashmaps by reference and putting hashmaps by copy. How do i do the latter?
The other issue is the number of String[] types is not really pre-known, so creating multiple instances of Multiset<String> textAndCount = TreeMultiset.create(); isn’t very helpful.
I’ve the following code, but my output for both types are the same.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.Multiset;
import com.google.common.collect.TreeMultiset;


public class TestIterator {

private static String[] foobarness  = {"foo", "bar", "ness", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "foo", "bar", "foo", "ness", "bar", "ness", "foo", "bar", "foo", "ness"};

private static String[] foobarness2  = {"bar", "ness", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "foo", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "bar", "foo", "ness", "ness", "bar", "foo", "ness"};

private static String[] types = {"type::1", "type::2"};


public static void main(String[] args) {
    Map<String, Multiset<String>> typeTextCount = 
        new HashMap<String, Multiset<String>>();

    Multiset<String> textAndCount = TreeMultiset.create();
    for (int i = 0; i < types.length; i++) {
        if ("type::1".equals(types[i])) {
            for (String text : foobarness)
                textAndCount.add(text, 1);
        }
        if ("type::2".equals(types[i])) {
            for (String text : foobarness2) 
                textAndCount.add(text, 1);
        }
        typeTextCount.put(types[i], textAndCount);
    }

    Iterator<Entry<String, Multiset<String>>> itTTC = 
        typeTextCount.entrySet().iterator();

    while (itTTC.hasNext()) {
        Map.Entry textCt = (Map.Entry) itTTC.next();
        System.out.println(textCt.getKey() + " :\t" + textCt.getValue());
        itTTC.remove();
    }
}

My outputs are from the above code:

type::2 :   [bar x 13, foo x 17, ness x 14]
type::1 :   [bar x 13, foo x 17, ness x 14]

The correct outputs should be:

type::1 :   [bar x 6, foo x 8, ness x 6]
type::2 :   [bar x 7, foo x 9, ness x 8]
  • 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-27T12:20:13+00:00Added an answer on May 27, 2026 at 12:20 pm

    Move Multiset<String> textAndCount = TreeMultiset.create() inside your for-loop. This same multiset is being shared by both “types”, so your counts are doubled.

    Your for-loop might then look like this:

        for (int i = 0; i < types.length; i++) {
            Multiset<String> textAndCount = TreeMultiset.create();
            if ("type::1".equals(types[i])) {
                for (String text : foobarness)
                    textAndCount.add(text, 1);
            }
            if ("type::2".equals(types[i])) {
                for (String text : foobarness2)
                    textAndCount.add(text, 1);
            }
            typeTextCount.put(types[i], textAndCount);
        }
    

    While you’re at it, you could improve your iteration of the map as well by using a for-each style loop. If you’re keen on removing each entry as you iterate over it, you could wrap your entrySet in a consumingIterable for the same functionality.

        for (Entry<String, Multiset<String>> textCt : Iterables.consumingIterable(typeTextCount
                .entrySet())) {
            System.out.println(textCt.getKey() + " :\t" + textCt.getValue());
        }
    

    This yields the output:

    type::2 :   [bar x 7, foo x 9, ness x 8]
    type::1 :   [bar x 6, foo x 8, ness x 6]
    

    If you don’t like that order, I’d suggest using an Ordering to get a sorted list of your entries.

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

Sidebar

Related Questions

Whilst putting together a T4 template I threw in a simple lambda expression: <#=string.Join(,,
I understand that the String class' hashCode() method is not guarantied to generate unique
I have one problem HashMap<String, List<AppPrjMilestone>> dateMilestoneMap = new HashMap<String, List<AppPrjMilestone>>(); I am putting
Putting this at the top of your CSS file: * { margin: 0; padding:
Putting code at aspx files is slower? The code is recompiled every access? At
Update Putting this at the top because it is crazy :) So some users
I putting wmd on my website. After putting code in and giving it a
When putting a favicon on your site, you can apparently use an animated gif,
I putting together a page that will display a set of stored values. I
I like putting shortcuts of the form g - google.lnk in my start menu

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.