Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.Map - remove random elements

 
Mike Nason
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dump all the keys into a List; Pick a random element from the List; remove the corresponding mapping fromt he Map
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic