zoukankan      html  css  js  c++  java
  • Java-LinkedHashSet

    如下:

    package 集合类.Set类;  
      
    import java.util.Arrays;  
    import java.util.HashSet;  
    import java.util.LinkedHashSet;  
    import java.util.Set;  
    /** 
     *  LinkedHashSet与HashSet的区别就是数据严格按照插入的顺序存放。 
     *  删除之后会去掉那个位置,新增的数据将在集合的末尾。 
     *  HashSet 内部使用HashMap实现<br> 
     *  而LinkedHashSet内部使用LinkedHashMap实现。  
     */  
    public class LinkedHashSet类 {  
        public static void main(String[] args) {  
            test(new HashSet<Integer>());  
            test(new LinkedHashSet<Integer>());  
        }  
      
        public static void test(Set<Integer> set) {  
            System.out.println(set.getClass().getName());  
            // 增加10个数据  
            for (int i = 100; i <= 110; i++) {  
                set.add(i);  
            }  
            // 看看里面数据的情况  
            showSet(set);  
      
            // 删除一个数据  
            set.remove(101);  
            // 看看删除后的情况  
            showSet(set);  
      
            // 增加三个数据,看结果  
            set.add(98);  
            set.add(101);  
            set.add(118);  
            showSet(set);  
        }  
      
        /** 
         * 显示Set里面的数据。 
         *  
         * @param set 
         */  
        private static void showSet(Set<Integer> set) {  
            System.out.println(Arrays.toString(set.toArray(new Integer[0])));  
        }  
    }  

    结果如下

    java.util.HashSet
    [102, 103, 100, 101, 110, 108, 109, 106, 107, 104, 105]
    [102, 103, 100, 110, 108, 109, 106, 107, 104, 105]
    [102, 103, 100, 101, 98, 110, 108, 109, 106, 107, 104, 105, 118]
    java.util.LinkedHashSet
    [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
    [100, 102, 103, 104, 105, 106, 107, 108, 109, 110]
    [100, 102, 103, 104, 105, 106, 107, 108, 109, 110, 98, 101, 118]

    linkedHashset有严格的顺序,而hash的顺序为根据hashcode排列的

  • 相关阅读:
    ASP.NET编程中非常有用的例子
    打包样式资源
    9.使用类的2个注意点
    面向对象案例
    super必须放到子类this之前
    PHP:根据二维数组中的某个字段进行排序
    Vuex的五个核心属性
    利用按钮控制listview的当前选择项,滚动条跟随动
    c#通过进程名字获取进程路径
    判断客户端是否安装realplayer
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/4510599.html
Copyright © 2011-2022 走看看