Home  >  Article  >  Java  >  Detailed explanation of examples of using Collection in java

Detailed explanation of examples of using Collection in java

零下一度
零下一度Original
2017-07-20 19:07:431855browse

一.Collection(java.util)
1.概述:具有相同性质的一类事物的汇聚的整体,称为集合.任何集合都包含三块内容:对外的接口/接口的实现/对集合运算的算法.
         java中使用Collection来表示单列集合顶层的接口.
         public interface Collection1a4db2c2c2313771e5742b6debf617a1 extends Itretaor1a4db2c2c2313771e5742b6debf617a1{}
         注意:接口不能直接实例化,得需要其子类
2.特点及体系:Collection为顶层接口,仅描述集合的共性内容.常用的子接口为 List 和 Set.
     共性内容:
         (1)集合容器的的长度可以改变;
         (2)存储的是同一个类型的数据;
         (3)只能存引用数据类型(基本数据类型需进行装箱操作);
     List : 有序/允许重复;
     Set : 无序/不允许重复;
     Queue:队列接口;
     SortedSet:对集合中的数据进行排序;
3.常用方法:

  public boolean add(E e){}:向集合中插入对象;
     public boolean addAll(Collection<? extends E> c){}:插入一个集合的内容;
     public void clear(){}:清除此集合中的所有内容,但是保留该集合容器;
     public boolean contains(Object o){}:判定某一对象是否存在该集合中;
     public boolean containsAll(Collection<?> c){}:判断一组对象是否存在该集合中;
     public boolean equals(Object o){}:对象比较;
     public int hashCode(){}:哈希值;
     public boolean isEmpty(){}:判断集合是否为空;
     public Iterator<E> iterator(){}:为Iterator接口实例化;返回迭代器实例化对象;
     public boolean remove(Object o){}:删除指定对象;
     public boolean removeAll(Collection<?> c){}:删除一组对象;
     public boolean retainAll(Collection<?> c){}:保存指定内容;
     public int size(){}:求出集合大小;
     public Object[] toArray(){}:将一个集合变为对象数组;

代码:

  1 import java.util.Collection;  2 	import java.util.ArrayList;  3   4 	public class Coll{  5 		public static void main(String[] args){  6 			//通过子类实例化接口对象,因为接口全为抽象无法直接实例化  7 			Collectionf7e83be87db5cd2d9a8a0b8117b38cd4 c = new ArrayListf7e83be87db5cd2d9a8a0b8117b38cd4();  8 			//添加元素add()  9 			c.add("Rose"); 10 			c.add("Jack"); 11 			//查看集合内容 12 			System.out.println(c);//[Rose, Jack] 13 			//查看集合长度 14 			System.out.println(c.size());//2 15 			//查看是否包含tom 16 			System.out.println(c.contains("tom"));//false 17 			//查看是否包含Tom 18 			c.add("Tom"); 19 			System.out.println(c.contains(new String("Tom")));//true--new String("Tom")返回一个"Tom"字符串 20 			//删除指定对象remove() 21 			c.remove("Rose"); 22 			System.out.println(c);//[Jack, Tom] 23 			//删除所有对象-保留集合 24 			c.clear(); 25 			System.out.println(c);//[] 26 			//判断是否为空 27 			System.out.println(c.isEmpty());//true 28 		} 29 	}

二.Iterator(java.util)
1.定义:专门操作集合的工具类,只要碰到集合的输出操作就一定使用Iterator接口
     pbulic interface Iterator1a4db2c2c2313771e5742b6debf617a1
2.常用方法:
     public boolean hasNext(){}:判断是否存在下一个值;
     public E next(){}:取出当前元素;
     public void remove(){}:移除当前元素;
3.注意事项:
(1)使用迭代器只能删除数据,不能添加数据;
(2)迭代器迭代数据的过程中,不能使用集合自带的方法,改变集合的长度!否则会报异常:ConcurrentModificationException 并发修改异常!
(3)注意在构建迭代器的时候,其后a8093152e673feb7aba1828c43532094内指定类型(如:Iteratorf7e83be87db5cd2d9a8a0b8117b38cd4 it = Collection.iterator();),否则运算会出错,仅打印没问题;

  1 代码1://需求:利用Iterator输出集合内容  2 	import java.util.Collection;  3 	import java.util.ArrayList;  4 	import java.util.Iterator;  5   6 	public class IteratorDemo{  7 		public static void main(String[] args){  8 			//通过子类实例化对象实例化Collection对象  9 			Collectionf7e83be87db5cd2d9a8a0b8117b38cd4 c = new ArrayListf7e83be87db5cd2d9a8a0b8117b38cd4(); 10 			//向集合c中添加元素 11 			c.add("Green"); 12 			c.add("Smith"); 13 			c.add("Philip"); 14 			//通过Collection的iterator方法,创建iterator对象 15 			Iteratorf7e83be87db5cd2d9a8a0b8117b38cd4 it =c.iterator();	//注意在构建迭代器的时候,指定类型(此处为f7e83be87db5cd2d9a8a0b8117b38cd4),否则运算会出错,仅打印没问题 16 			//输出集合c中的所有元素 17 			while(it.hasNext()){ 18 				System.out.print(it.next()+"\t");//注意,建议判断一次仅调用一次next()方法,由于next()在输出的时候也会使指针向前移动,容易发生"NoSuchElementException" 19 				//System.out.println("i like"+it.next());//java.util.NoSuchElementException 20 			} 21 		} 22 	}
  1 代码2://需求:删除元素  2 import java.util.Collection;  3 import java.util.ArrayList;  4 import java.util.Iterator;  5   6 public class IteratorDemo{  7 	public static void main(String[] args){  8 		//通过子类实例化对象实例化Collection对象  9 		Collectionf7e83be87db5cd2d9a8a0b8117b38cd4 c = new ArrayListf7e83be87db5cd2d9a8a0b8117b38cd4(); 10 		//向集合c中添加元素 11 		c.add("Green"); 12 		c.add("Smith8"); 13 		c.add("Philip"); 14 		//foreach迭代 15 		for(String s : c){ 16 			System.out.print(s+"\t")	//Green Smith8 Philip 17 		} 18 		System.out.println("====================") 19 		//通过Collection的iterator方法,创建iterator对象 20 		Iteratorf7e83be87db5cd2d9a8a0b8117b38cd4 it =c.iterator();//注意在构建迭代器的时候,指定类型(此处为f7e83be87db5cd2d9a8a0b8117b38cd4),否则运算会出错,仅打印没问题 21 		//输出集合c中的所有元素 22 		while(it.hasNext()){ 23 			//取出集合中的元素 24 			String str = it.next(); 25 			//判断是否含有数字--正则(字符串最后一位是数字的) 26 			if(str.matches("\\w+\\d")){ 27 				it.remove(); 28 			}else{ 29 				System.out.print(str+"\t");// Green Philip 30 			} 31 		} 32 		System.out.print("删除带数字的名称后为:"+c+"\t");//删除带数字的名称后为:[Green, Philip] 33 	} 34 } 35

三.for--each
1.作用:增强for是JDK1.5之后出现的新特性,可以用于迭代(遍历/循环)集合或数组!可以使用增强for替代迭代器,获取集合的元素内容!
2.格式:
     for(数据类型 变量名 : 对象名或数组名){
             集合或数组中的每一个元素!
         }

The above is the detailed content of Detailed explanation of examples of using Collection in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn