zoukankan      html  css  js  c++  java
  • LinkedList,HashSet,LinkedHashSet

    1  LinkedList集合

    ArrayList集合数据存储的结构是数组结构。元素增删慢,查找快。

    LinkedList集合数据存储的结构是链表结构。方便元素添加、删除的集合。

    方法:

    public class Demo01 {
        public static void main(String[] args) {
            LinkedList<String>list=new LinkedList<String>();
            list.addLast("a");
            list.addFirst("b");
            list.addFirst("c");
            list.addLast("e");
            //清空集合
            list.clear();
            //if(list.size()!=0)   判空
            if(!list.isEmpty()){
                //获取集合中第一个元素
                System.out.println(list.getFirst());
                //获取集合中最后一个元素
                System.out.println(list.getLast());
            }
            //删除第一个元素
            list.removeFirst();
            list.removeLast();
            
        }
    }

    2 HashSet集合

    1.HashSet集合,采用哈希表结构存储数据,保证元素唯一性的方式依赖于:hashCode()与equals()方法。

    当向哈希表中存放元素时,需要根据元素的特有数据结合相应的算法,这个算法其实就是Object类中的hashCode方法。由于任何对象都是Object类的子类,所以任何对象有拥有这个方法。即就是在给哈希表中存放对象时,会调用对象的hashCode方法,算出对象在表中的存放位置,这里需要注意,如果两个对象hashCode方法算出结果一样,这样现象称为哈希冲突,这时会调用对象的equals方法,比较这两个对象是不是同一个对象,如果equals方法返回的是true,那么就不会把第二个对象存放在哈希表中,如果返回的是false,就会把这个值存放在哈希表中。

    public class Demo02 {
        public static void main(String[] args) {
            /*Person p1=new Person();
            Person p2=new Person();
            System.out.println(p1.hashCode());
            System.out.println(p2.hashCode());*/
            String s1=new String("abc");
            String s2=new String("abc");
            System.out.println(s1.hashCode());
            System.out.println(s2.hashCode());
            HashSet<String> set=new HashSet<String>();
            set.add("abc");
            set.add("bcd");
            for(String s:set){
                System.out.println(s);
            }
        }
    }

    2.HashSet存储和取出元素

    public class Demo03 {
        public static void main(String[] args) {
            Person p1=new Person("a",18);
            Person p2=new Person("b",19);
            Person p3=new Person("c",16);
            Person p4=new Person("a",18);
            HashSet<Person>set=new HashSet<Person>();
            set.add(p1);
            set.add(p2);
            set.add(p3);
            set.add(p4);
            for(Person p:set){
                System.out.println(p);
            }
            Iterator<Person> p=set.iterator();
            while(p.hasNext()){
                System.out.println(p.next());
            }
        }
    }

    3 LinkedHashSet集合

    HashSet下面有一个子类LinkedHashSet,它是链表和哈希表组合的一个数据存储结构。

    public class Demo04 {
        public static void main(String[] args) {
            LinkedHashSet<String>set=new LinkedHashSet<String>();
            set.add("hello");
            set.add("java");
            set.add("nihao");
            for(String s:set){
                System.out.println(s);
            }
        }
    }
  • 相关阅读:
    MongoDB 备份方法
    Wix制作安装包
    OWIN and Katana
    JavaScript的语法要点 4
    JavaScript的语法要点 3
    Docker配置镜像源(windows)
    Centos 7 安装gdal
    centos下forever安装nodejs服务
    Ngix初识
    arcgis支持mongodb
  • 原文地址:https://www.cnblogs.com/quanjunkang/p/10646017.html
Copyright © 2011-2022 走看看