由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 要随机返回一个Set的里的元素, 如何操作呢?
相关主题
问一道关于Vector的题ConcurrentModificationException
哪位大哥总结一下Iterator这些数据集合Simple question: delete element from collection on condition?
需要一个动态的List,不要ConcurrentModificationExceptionwhat is Agile development methodology?
Why java.lang.Iterable depends on java.util.Iterator改写(migrate) stored proc 问题
HashMap 怎样循环用更快?文件分割的问题
怎样才能使一个算法用于不同的数据结构?我说说两个曾经被人EMBARASS的问题
MySQL JDBC 问题Java代码,老是compile出错,大家帮我看看哪错了。。。
Client-side application development-HELPC# is light-years ahead of Java now (转载)
相关话题的讨论汇总
话题: set话题: random话题: keep话题: 元素话题: 随机
进入Java版参与讨论
1 (共1页)
c*********n
发帖数: 128
1
比如有Set set
现在要在set里随机取一个元素出来, 如何取呢?
目前只想到一个比较笨的办法:
int i = (int) (random() * set.toArray().length);
return ( set.toArray() )[i];
这个方法有没有什么问题? 有更好的方法么?
s*****e
发帖数: 21415
2
i have dealt with this before.
i think an even better way is to keep a linear interface.
use an array to store the items.
use a set to store key-->array_index mappings.
then you can support insert/delete/random_access.
time complexity is the same as the set. just a constant overhead.
p***p
发帖数: 559
3
and do not forget synchrnoized this method
s*****e
发帖数: 21415
4
won't work.
check key uniqueness and insert will be O(n)
set + array is the best. hehe..

【在 p***p 的大作中提到】
: and do not forget synchrnoized this method
p***p
发帖数: 559
5
use arraylist for random access
for add/remove LinkedList

【在 s*****e 的大作中提到】
: i have dealt with this before.
: i think an even better way is to keep a linear interface.
: use an array to store the items.
: use a set to store key-->array_index mappings.
: then you can support insert/delete/random_access.
: time complexity is the same as the set. just a constant overhead.

p***p
发帖数: 559
6
a general question
when is good to use SET?

【在 s*****e 的大作中提到】
: won't work.
: check key uniqueness and insert will be O(n)
: set + array is the best. hehe..

r*****l
发帖数: 2859
7
Why don't you just iterator through the Set
and exit the iteration at the random number?
Keep in mind that toArray() in Set does not
guarantee the order.

【在 c*********n 的大作中提到】
: 比如有Set set
: 现在要在set里随机取一个元素出来, 如何取呢?
: 目前只想到一个比较笨的办法:
: int i = (int) (random() * set.toArray().length);
: return ( set.toArray() )[i];
: 这个方法有没有什么问题? 有更好的方法么?

c*********n
发帖数: 128
8
en, good idea

Why don't you just iterator through the Set
and exit the iteration at the random number?
Keep in mind that toArray() in Set does not
guarantee the order.

【在 r*****l 的大作中提到】
: Why don't you just iterator through the Set
: and exit the iteration at the random number?
: Keep in mind that toArray() in Set does not
: guarantee the order.

1 (共1页)
进入Java版参与讨论
相关主题
C# is light-years ahead of Java now (转载)HashMap 怎样循环用更快?
C# is light-years ahead of Java now (转载)怎样才能使一个算法用于不同的数据结构?
请教:软件开发流程MySQL JDBC 问题
问个ArrayList的问题Client-side application development-HELP
问一道关于Vector的题ConcurrentModificationException
哪位大哥总结一下Iterator这些数据集合Simple question: delete element from collection on condition?
需要一个动态的List,不要ConcurrentModificationExceptionwhat is Agile development methodology?
Why java.lang.Iterable depends on java.util.Iterator改写(migrate) stored proc 问题
相关话题的讨论汇总
话题: set话题: random话题: keep话题: 元素话题: 随机