9 Replies - 1900 Views - Last Post: 08 December 2011 - 06:56 PM Rate Topic: -----

#1 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

printing random value from hashmap arraylist

Posted 08 December 2011 - 12:09 PM

So I made a Hashmap<String, ArrayList<String>>. there's about 3-4 values per key in the hashmap. I want to be able to print a random value each time I run my program. So far I have
Random generator = new Random();
				Object []values = map.values().toArray();
				randomValue = values[generator.nextInt(values.length)];
				System.out.println(randomValue);

But it won't let me make randomValue a string so it just crashes. It compiles fine however. How would I do this? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: printing random value from hashmap arraylist

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 12:23 PM

If you just want to print one random item, you don't need an array. Just choose a random index and get the item at that index out of the array list.
Was This Post Helpful? 0
  • +
  • -

#3 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 12:28 PM

Is it possible to get a random object from an arraylist?
Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 12:37 PM

Here randomValue looks like an ArrayList, not a String. Yes it is possible, so pretty much get another random number and do as cfoley says, by extracting a value from it.
Was This Post Helpful? 0
  • +
  • -

#5 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 12:38 PM

actually i tried that and it didn't work. i'm not too sure what to do right now.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 12:43 PM

Post what you tried. Maybe we can spot what you did wrong.
Was This Post Helpful? 1
  • +
  • -

#7 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 01:14 PM

HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
//added items to HashMap
Random generator = new Random();
Object []values = map.values().toArray();
randomValue = values[generator.nextInt(values.length)];
System.out.println(randomValue + "")



That's what I have so far.

edit: i can get my program to print it like this
"[aaa,bbb,ccc],[ddd,eee,fff],[ggg,hhh,iii]"
I need to print something random from the first group, 2nd group, and 3rd group. An example of an output would be "[bbb, eee, ggg]"

This post has been edited by mastapro: 08 December 2011 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 01:36 PM

Here's what I came up with. It seems like a lot of work. Are you sure you have to use a HashMap? I'd consider a different data structure.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.Set;

public class DIC {
	public static void main(String[] args) {
		// Create the hashmap
		HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
		ArrayList<String> myValues = new ArrayList<String>();
		myValues.add("One");
		myValues.add("Two");
		myValues.add("Three");
		
		map.put("First", myValues);
		
		// get the keySet and transform it into an indexable array
		Set<String> keys = map.keySet();
		
		String[] keysArray = new String[0];
		keysArray = keys.toArray(keysArray);
		
		// get a random key to get a random value
		Random rand = new Random();
		String randomKey = keysArray[ rand.nextInt(keysArray.length) ];
		
		ArrayList<String> myRandomValues = map.get(randomKey);
		
		// get a random answer from the random value
		String answer = myRandomValues.get( rand.nextInt(myRandomValues.size() )  );
		
		System.out.println(answer);
	}
}

Was This Post Helpful? 0
  • +
  • -

#9 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 02:44 PM

thanks sheph, i'll try it out
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: printing random value from hashmap arraylist

Posted 08 December 2011 - 06:56 PM

View Postmastapro, on 08 December 2011 - 03:28 PM, said:

Is it possible to get a random object from an arraylist?


Object o = al.get(ran.nextInt(al.getSize()));
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1