This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes java.util.Map - remove random elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.util.Map - remove random elements" Watch "java.util.Map - remove random elements" New topic

java.util.Map - remove random elements

Mike Nason

Joined: Apr 19, 2006
Posts: 20
I have SortedMap and need to remove a given number of elements from there in random order (i.e. it doesn't matter which elements will be removed, but they should be random)
Do you have any idea of how to do that in effective way?


Wanna code??
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Dump all the keys into a List; Pick a random element from the List; remove the corresponding mapping fromt he Map

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
I agree. Here's the link:
subject: java.util.Map - remove random elements
jQuery in Action, 3rd edition