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

  • Home
  • SEARCH
  • 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 7925095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:07:46+00:00 2026-06-03T18:07:46+00:00

I would like to shuffle values in an HashMap. I have following type of

  • 0

I would like to shuffle values in an HashMap. I have following type of HashMap

Map<Integer,ArrayList<String> > trainDataSet = new HashMap<Integer, ArrayList<String>>();

I would like to shuffle the values in the Map. How would i do it?

Following is my attempt:

    collections.shuffle(trainDataSet.values());

Got an error:

    Values cannot be cast to java.util.List

Yes this make sense, because my values are in Arraylist not in List. Would it be possible to shuffle collections of arraylist?

EDIT

If i have following order::

key1  [aa,bb,cd]
key2  [xx,xy,sfr]

Into something like

key1  [xx,xy,sfr]
key2  [aa,bb,cd]
  • 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-03T18:07:54+00:00Added an answer on June 3, 2026 at 6:07 pm

    You actually want to randomly reassociate keys and values. Here’s an example how that can be achieved:

    final Map<String, Object> x = new HashMap<String, Object>();
    x.put("x", 1); x.put("y", 2); x.put("z", 3); x.put("w", 4);
    System.out.println(x);
    final List<Object> vs = new ArrayList<Object>(x.values());
    Collections.shuffle(vs);
    System.out.println(vs);
    final Iterator<Object> vIter = vs.iterator();
    for (String k : x.keySet()) x.put(k, vIter.next());
    System.out.println(x);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement a shuffle function, I have this piece of code
Would like to know the c# code to actually retrieve the IP type: Static
I would like to parse the values from aspx pages, both what is passed
I'm new to C# and would like to ask for some direction on solving
I would like to play short sounds in my new JavaScript game. It is
I would like to know if the following is possible in any of the
In Python, I have something like the following (although randomly shuffled): l = [('a',
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at
Would like to make anapplication in Java that will not automatically parse parameters used

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.