Java随机集合 [英] Java random collection

查看:124
本文介绍了Java随机集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种Java集合,我的提取顺序是随机的吗?例如,我将整数1,2,3放入集合中,当我尝试打印它们时,所有结果都可以是1 2 3,3 2 1或1 3 2?

Is there a kind of Java collection that the order of my fetching is random? For example, I put integer 1, 2, 3 into the collection and when I try to print them all the result can be "1 2 3","3 2 1" or "1 3 2"?

推荐答案

如果您只想要一个随机序列,您可以使用Collections.shuffle

If you just want a random sequence you could use Collections.shuffle

    List<Integer> list = new LinkedList();
    //Add elements to list
    Collections.shuffle(list);

这篇关于Java随机集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆