步骤1:是否有顺序
步骤2:能否重复
步骤3:练习-不重复的随机数
步骤4:答案-不重复的随机数
示例 1 : 是否有顺序
ArrayList: 有顺序
HashSet: 无顺序
HashSet的具体顺序,既不是按照插入顺序,也不是按照hashcode的顺序。关于hashcode有专门的章节讲解: hashcode 原理。
以下是HasetSet源代码中的部分注释
/**
* It makes no guarantees as to the iteration order of the set;
* in particular, it does not guarantee that the order will remain constant over time.
*/
|
不保证Set的迭代顺序; 确切的说,在不同条件下,元素的顺序都有可能不一样
|
换句话说,同样是插入0-9到HashSet中, 在JVM的不同版本中,看到的顺序都是不一样的。 所以在开发的时候,不能依赖于某种臆测的顺序,这个顺序本身是不稳定的
package collection;
import java.util.ArrayList;
import java.util.HashSet;
public class TestCollection {
public static void main(String[] args) {
ArrayList<Integer> numberList = new ArrayList<Integer>();
//List中的数据按照插入顺序存放
System.out.println( "----------List----------" );
System.out.println( "向List 中插入 9 5 1" );
numberList.add( 9 );
numberList.add( 5 );
numberList.add( 1 );
System.out.println( "List 按照顺序存放数据:" );
System.out.println(numberList);
System.out.println( "----------Set----------" );
HashSet<Integer> numberSet = new HashSet<Integer>();
System.out.println( "向Set 中插入9 5 1" );
//Set中的数据不是按照插入顺序存放
numberSet.add( 9 );
numberSet.add( 5 );
numberSet.add( 1 );
System.out.println( "Set 不是按照顺序存放数据:" );
System.out.println(numberSet);
}
}
|
示例 2 : 能否重复
List中的数据可以重复
Set中的数据不能够重复
重复判断标准是:
首先看hashcode是否相同
如果hashcode不同,则认为是不同数据
如果hashcode相同,再比较equals,如果equals相同,则是相同数据,否则是不同数据
更多关系hashcode,请参考hashcode原理
package collection;
import java.util.ArrayList;
import java.util.HashSet;
public class TestCollection {
public static void main(String[] args) {
ArrayList<Integer> numberList = new ArrayList<Integer>();
//List中的数据可以重复
System.out.println( "----------List----------" );
System.out.println( "向List 中插入 9 9" );
numberList.add( 9 );
numberList.add( 9 );
System.out.println( "List 中出现两个9:" );
System.out.println(numberList);
System.out.println( "----------Set----------" );
HashSet<Integer> numberSet = new HashSet<Integer>();
System.out.println( "向Set 中插入9 9" );
//Set中的数据不能重复
numberSet.add( 9 );
numberSet.add( 9 );
System.out.println( "Set 中只会保留一个9:" );
System.out.println(numberSet);
}
}
|
更多内容,点击了解: https://how2j.cn/k/collection/collection-arraylist-vs-hashset/367.html