• Java自学-集合框架 ArrayList和HashSet的区别


    Java ArrayList和HashSet的区别

    示例 1 : 是否有顺序

    ArrayList: 有顺序
    HashSet: 无顺序

    HashSet的具体顺序,既不是按照插入顺序,也不是按照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相同,则是相同数据,否则是不同数据
    能否重复

    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);
               
        }
    }
    

    练习不重复的随机数

    生成50个 0-9999之间的随机数,要求不能有重复的

    答案 :

    package collection;
     
    import java.util.HashSet;
    import java.util.Set;
     
    public class TestCollection {
        public static void main(String[] args) {
            Set<Integer> numbers =new HashSet<>();
            while(numbers.size()<50){
                int i = (int) (Math.random()*10000);
                numbers.add(i);
            }
            System.out.println("得到50个不重复随机数:");
            System.out.println(numbers);
        }
    }
    
  • 相关阅读:
    python学习 05 函数switch功能
    python学习 04 函数参数
    python学习 03 函数 (只会执行一次return就不会往下执行)
    【转】Jenkins+Ant+Jmeter接口自动化集成测试实例
    【转】python做一个http接口测试框架
    python学习 02 元组
    【转】使用 Python Mock 类进行单元测试
    【转】使用Python学习selenium测试工具
    【转】利用Python中的mock库对Python代码进行模拟测试
    【转】python测试开发面试题
  • 原文地址:https://www.cnblogs.com/jeddzd/p/12112663.html
Copyright © 2020-2023  润新知