java常用类/Collection/Iterator/List

1(1)、Collection类:集合
  (2)、高级功能:
              boolean addAll(Collection c)  :添加一个集合中的所有元素
              boolean removeAll(Collection c):删除的高级功能(思考:删除一个算是删除还是删除所有算是删除?)
              boolean containsAll(Collection c):包含所有元素算是包含,还是包含一个算是包含
         交集功能:
              boolean retainAll(Collection c):A集合对B集合取交集,交集的元素到底是去A集合还是去B集合中,返回值boolean
             结论:A集合对B集合取交集,交集的元素要去A集合中,boolean返回值表达的A集合的元素是否发生变化,如果发生变化,则                 返回true,否则,返回false
         转换功能:Object[] toArray() :将集合转换成数组
2(1)、Iterator iterator() :集合的迭代器方法(获取集合的迭代器)--->集合的专有遍历方式:迭代器遍历
  (2)、 Iterator :接口中有以下的方法:
          boolean hasNext() :如果有元素可以迭代,那么返回true,否则返回false
          Object next()返回迭代的下一个元素。 存储String类型的元素
  (3)、注意:
   A、 it.next(),只使用一次即可,使用多次,会出现问题(it.next(),每次使用的时候都是返回一个对象)
   B、遍历的时候使用的while循环,可不可以使用for循环呢?  可以,但是不常用,使用while模板代码
                // 创建一个集合对象
		Collection c = new ArrayList();
		// 创建学生对象
		Student s1 = new Student("张三", 28);
		Student s2 = new Student("李四", 30);
		Student s3 = new Student("王五", 22);

		// 添加到集合
		c.add(s1);
		c.add(s2);
		c.add(s3);

		// 获取迭代器
		Iterator it = c.iterator();
		// 遍历
		while (it.hasNext()) {//这种方式才是我们的模板代码
			//System.out.println(((Student) it.next()).getName() + "----" + ((Student) it.next()).getAge());
			Student s =(Student)it.next() ;
			System.out.println(s.getName()+"---"+s.getAge());
		}

迭代器的源码分析:

interface Iterator{
	public abstract boolean hasNext() ;
	public Object next();
}
interface Iterable{
	Iterator  iterator() ;
}
interface Collection extends Iterable{
	Iterator  iterator() ;	
}
inteface List extends Collection{
	public abstract boolean hasNext() ;
	public Object next();
	Iterator  iterator() ;
}
class ArrayList implements List{
	 public Iterator iterator() {
        return new Itr();//Itr这个类就是Iterator的子实现类
    }
     private class Itr implements Iterator{
     	//实现了hasNext()和next()
     	public boolean hasNext(){
     	}
     	public Object next(){
      		}
     }
}

		Iterator it = c.iterator() ;   //接口多态的形式
while(it.hasNext()) {
//			Object obj = it.next() ;  //Object obj = new Objct();
//			
//			//需求:打印字符串同时,获取长度
//			String str = (String) obj ;
			String str = (String)(it.next) ;
			System.out.println(str+"---"+str.length());
		}

3(1)、List--->Collection有两个子接口:List  Set

(2)、List集合的特点:

  有序的 (存储和取出一致),可以允许重复元素
       Set集合的特点:
 无序性(不能保证迭代的顺序,基于hashMap),并且元素不能重复
(4)、List集合的特有功能:
  添加功能: void add(int index,Object element):在指定位置处添加指定元素
  获取功能: Object get(int index)返回列表中指定位置的元素。
                    ListIterator listIterator():列表迭代器
  删除功能:    Object remove(int index):删除指定位置处的元素
  修改功能: Object set(int index, Object element):用指定element元素替换掉指定位置处的元素
4(1)、ListIterator listIterator():列表迭代器 (List集合的专有迭代遍历:列表迭代器)
 ListIterator接口中:
      boolean hasNext()  :判断是否有下一个可以迭代的元素(正向遍历)
      Object next():获取下一个元素
      boolean hasPrevious():判断是否有上一个可以迭代 元素(逆向遍历)
      Object previous():返回上一个元素
      逆向迭代(遍历),单独使用没意义,前提,要先正向遍历
5、存储自定义对象并遍历
 (1)、size()和get(int index):相结合  普通for循环的方式
 (2)、Iterator iterator();
 (3)、size()和get()方法相结合
6、List集合有三个子实现类:
   ArrayList
         底层数据结构式数组结构,查询块,增删慢
         从内存角度考虑:线程不安全的,不同步的,执行效率高
         多线程:synchronized :同步的意思  解决线程安全问题
         sychronized(锁对象){ 同步代码
               共享数据; 
          }
         解决线程安全问题,通过同步可以解决,但是效率低了...
   
   LinkedList
            底层数据结构式链表结构,查询慢,增删块
           从内存角度考虑:线程不安全,不同步,执行效率高
   
   Vector:
         这是一个线程安全的类,
         底层数据结构是数组:查询快,增删慢
         线程安全的,同步,执行效率低!

猜你喜欢

转载自blog.csdn.net/liuxiaofan_/article/details/80180091