• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I'm trying to create a Hashmap using the random numbers generated from the code, any ideas?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.ArrayList
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so, what happens when you try to compile it and run it?  Do you get any error message?
 
Bartender
Posts: 242
27
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is an ArrayList, not a HashMap; they are very different.

Also when creating anything that accepts generics (such as Lists and Maps), it's best to specify the type you are going to store; and also to declare the variable as the most generic type possible. For example:


 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there will also be problems with the import statement, and the lack of any containing class.  And, what's "pick", anyway?
 
Nex Wat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, so I adjusted it and it worked

 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you still want a List not a Map, you can create a Stream of ints with the Random#ints() method.
You can turn that Stream of ints to a Stream of Integers with its boxed() method, and you can use a Collector to collect all the elements into something else.
To make the use of a Collector easier, the Collectors class supplies one ready made that does just what you want with this method.
 
Nex Wat
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure And this time without a spelling error!
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nex Wat wrote:


Not sure how important that is in your case, but that comment might be a bit ambiguous.

This is how the JavaDocs (portion of it) for this method (Random.nextInt(...)) reads:

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)...


Verify whether your implementation is what you actually want.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nex Wat, please don’t alter the posts (by fixing pointed out issues) which were commented already. Note: I have reverted your latest edit.

The reason being, Those who read the thread could think that people (in this case I) comment about the things which don’t exist. And who likes to look silly
 
Nex Wat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, ok
Thank you for pointing it out.

I made a mistake in the comment, it should be:

 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nex Wat wrote:I made a mistake in the comment, it should be


Don't feel bad about it. It is less a mistake, but more an ambiguity.

When you write such type of comments or speak with your friend, it is always a good idea to state it explicitly. For example: 0 (inclusive) to 44 (exclusive), now that is not questionable.

I'm sure in the sense of correct english, 0 to 100 or similar might actually mean 100 (exclusive), but why to leave that for the reader to parse if could be stated explicitly.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic