zoukankan      html  css  js  c++  java
  • Java 迭代器 工具类

    迭代器:Iterator接口

    //获取集合中的对象
    Iterator<E> iterator()

    interface Iterator
    {
      boolean hasNext();
      Object next();
    }
    //遍历

    Iterator ite = col.iterator(); while(ite.hasNext()) {   //获得一个对象   Object obj = ite.next();   System.out.println(obj); } for(int i = 0;i<col.size();i++) { } for(Iterator ite = col.iterator();ites.hasNext());) { }

    集合:Collection接口

      可以存储不同类型的对象,而且随着存储的对象的增加容量自动扩大

      Collection:
        **iterator():获取集合中的对象
        
    --List:       存储的对象是有序的       集合中对象的顺序和添加对象的顺序是一致的,可以重复的       List特有的方法都是可以操作下标的方法       --ArrayList:         底层使用的数据结构是数组         线程不安全的         查找速度快,增删速度慢       --LinkedList:         底层使用的数据结构是链表         线程不安全的         查找速度慢,增删速度快       --Vector:         底层使用的数据结构是数组         线程安全的(被ArrayList替代)         查找速度快,增删速度慢,被ArrayList替代    --Set:     存储的对象是无序的,不可重复的     --HashSet:       底层使用的数据结构是哈希表       线程不安全     --TreeSet:       底层使用的数据结构是二叉树,可以排序       线程不安全

      Vector: 动态数组

        早期使用Enumeration接口进行枚举

    Enumeration e = v.elements();
    
    while(e.hasMoreElements())
    {
    Object obj = e.nextElements();
    System.out.println(obj);
    }

    LinkedList:

    jdk1.5 jdk1.6之后

    addFirst

    addLast

    offerFirst

    offerLast

    getFirst:获取的元素不存在出现NoSuchElementException

    getLast

    peekFirst:获取的元素不存在返回null

    peekLast

    removeFirst

    removeLast

    linkFirst

    linkLast

     

      ArrayList:

    1 去掉集合中重复的对象
    
      新建一个list2
      遍历旧集合
        如果list2中不包含这个对象,添加
          contains(obj)
        如果list2中包含,不添加

     2 contains判断是否包含某个对象的依据?

       依据boolean equals(Object obj)方法的返回值

     

     3 直接输出List<Class>的话,输出的是Class的hash值

      解决:
        在class中重写toString():

      
        public String toString()
        {
          return ...;
        }

     

     4 去掉List<class>中相同内容的class

      在class中重写equals()

      public boolean equals(Object obj)

      {

        if(!(obj instanceof Student))//如果obj不是Student类型
           throws new RuntimeException("类型错误");

        Student stu = (Student)obj;

        return this.age == stu.age && this.name.equals(stu.name);
      
       }

     

      HashSet:

    1 在使用add方法添加对象时就保证了对象的唯一

     2 HashSet无序的原因

      数据结构是哈希表,根据哈希算法得到位置,导致的无序

    3 HashSet保证对象唯一的方式
     在添加对象时,先比较得到对象的hash值和集合中对象的hash值分别进行比较

     int hashCode()
      如果和所有对象的hash值都不相同,将对象直接添加到集合
     
     boolean equals()

      如果相同,使用equals()方法比较内存地址是否相同

         如果相同,认为是同一对象,不添加

         如果不同,认为是不同的对象,加入集合

     4 去掉Hash<class>中相同内容的class

      在class中重写hashCode 和 equals方法

      public int hashCode()
      {
        return name.hashCode() + age * 33;
      }

      public boolean equals(Object obj)
      {
        if(!(obj instanceof Student))//如果obj不是Student类型
          throws new RuntimeException("类型错误");

        Student stu = (Student)obj;

        return this.age = stu.age && this.name.equals(stu.name);
      }

       TreeSet:

    1 在使用add方法添加对象时会对添加的对象进行排序
      排序方式一:
        集合中对象所属的类实现了Comparable接口中的compareTo()方法
      排序方式二:
        自定义排序方法

    2 TreeSet保证对象唯一的方式:
      集合中对象所属的类实现了Comparable接口中的compareTo()方法

     3 去掉TreeSet<class>中相同内容的class

       在class中重写compareTo()

        public int compareTo(Object obj)
        {

          if(!(obj instanceof Student))//如果obj不是Student类型
            throws new RuntimeException("类型错误");

          Student stu = (Student)obj;

          int num = this.age - stu.age;

          return num == 0 ? this.name.compatrTo(stu.name) : num;
        }

  • 相关阅读:
    redis线程模型
    同步容器和并发容器
    200+面试题
    redis pipeline
    redis事务和脚本
    redis事务
    redis优缺点
    redis持久化策略
    Redis为什么要把所有数据放到内存中?
    redis的过期策略以及内存淘汰机制
  • 原文地址:https://www.cnblogs.com/roxy/p/7339805.html
Copyright © 2011-2022 走看看