java--集合概述

java--集合概述

一、集合框架总览图

二、为什么需要集合

 

  • 为什么出现集合类?

面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就要对对象进行存储,集合就是存储对象最常用的一种方式。

  • 数组和集合类同是容器,有何不同?

数组虽然也可以存储对象,但长度是固定的;集合长度是可变的。数组中可以存储任意数据类型,集合只能存储对象。

  • 集合类的特点

集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。

三、集合分类概述

四、集合的两大接口

Java集合类主要由两个接口派生出来:
Collection

 

Set :不能存放重复对象

 

List :可存放重复对象,有序

 

Queue :队列

 

SortedSet :可对集合数据排序

 

Map

SortedMap :可对集合数据排序

Collection和Iterator接口实例

package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;


/**
 * 在集合里的存储,永远存的是  一个引用地址
 * @author will
 *
 */
public class CollectionDemo {
	public static void main(String[] args) {
		
		Collection c = new ArrayList();
		/*
		 *  boolean add(E e) 
			          确保此 collection 包含指定的元素(可选操作)。 
			          
			 boolean addAll(Collection c) 
			          将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。 
			 void clear() 
			          移除此 collection 中的所有元素(可选操作)。 

		 * */
		c.add("A");
		c.add("A");
		//c.add(1);//java5之前不允许,Java5之前没有自动装箱
		c.add(new Integer(1));//
		c.add(23);
		
		
		StringBuilder sb = new StringBuilder("SB");
		
		c.add(sb);
		boolean b = c.add("A");
		System.out.println(b);
		
		System.out.println(c);
		
		sb.append("OOXX");
		System.out.println(c);
		
		Collection c2 = new ArrayList();
		c2.add(new Date());
		c2.add("春哥");
		
		
		c.addAll(c2);
		
		System.out.println(c);
		
		//c.clear();
		System.out.println(c);
		
		/*
		 * boolean contains(Object o) 
		          如果此 collection 包含指定的元素,则返回 true。 
		 boolean containsAll(Collection<?> c) 
		          如果此 collection 包含指定 collection 中的所有元素,则返回 true。 

		 * */
		
		System.out.println(c.contains("春哥"));
		System.out.println(c.containsAll(c2));
		System.out.println(c2.containsAll(c));
		
		/*
		 *  boolean isEmpty() 
          			如果此 collection 不包含元素,则返回 true。 
		 * */
		
		System.out.println("c.isEmpty()=  "+c.isEmpty());
		
		/*
		 * boolean remove(Object o) 
		          从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。 
		 boolean removeAll(Collection<?> c) 
		          移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。 
		 * */
		System.out.println(c.remove("凤姐"));
		System.out.println(c.remove("春哥"));
		System.out.println(c);
		
		c.removeAll(c2);
		System.out.println(c);
		/*
		 *  boolean retainAll(Collection<?> c)  其实就是求两个集合的交集
          		仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。 
		 * */
		
		c2.add(23);
		c2.add("A");
		c.retainAll(c2);
		System.out.println(c);
		/**
		 *  int size()  返回此 collection 中的元素数。 
		 */
		
		System.out.println(c.size());
		/**
		 * Object[] toArray() 
          			返回包含此 collection 中所有元素的数组。 
		 */
		
		
		Object[] os = c.toArray();
		
		for (Object o : os) {
			System.out.println("-->" + o);
		}
		/**
		 * for-each使用的场合, 数组和 Iterable对象
		 */
		System.out.println("------------------");
		
		for (Object object : c) {
			System.out.println(object);
		}
		
		/**
		 *  Iterator iterator()    返回在此 collection 的元素上进行迭代的迭代器。 
		 *  
		 *  Iterator类:
		 *  boolean hasNext() 
		          		如果仍有元素可以迭代,则返回 true。 
		 	Object next() 
		          		返回迭代的下一个元素。 
		    void remove() 
		          		从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。 

		 */
		System.out.println("==================");
		Iterator it = c.iterator();
		
		//每调用next方法后,指针就会向后移动一个位置
		System.out.println(it.next());
		System.out.println(it.next());
		System.out.println(it.next());
		System.out.println(it.next());
		//System.out.println(it.next());//java.util.NoSuchElementException指针已到最后,没有下一个元素了
		
		System.out.println(it.hasNext());
		if(it.hasNext()){
			System.out.println(it.next());
		}
		
		it = c.iterator();
		while(it.hasNext()){
			System.out.println("----->"+it.next());
		}
		
		/*
		 * for(it = c.iterator();it.hasNext();){
			System.out.println("for----->"+it.next());
			}
			
			老外喜欢用,就是因为性能高一低;此时it在for语句里创建,声明周期短一些
		 * 
		 * */
		
		for(it = c.iterator();it.hasNext();){
			System.out.println("for----->"+it.next());
		}
		
		/**
		 * for-each
		 */
		
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章