文档章节

java.util.Collection学习 - 认识Collection接口

crowley
 crowley
发布于 2015/06/16 19:46
字数 1400
阅读 23
收藏 0

Collection是java集合的超类(List,Set等)

注:以下是依据JDK 7 Collection接口来说明的。

public interface  Collection<E>  extends  Iterable <E>

有如下方法:

int size();
返回集合中元素的总数,如果集合的总数比Integer.MAX_VALUE大,则返回Integer.MAX_VALUE

boolean isEmpty();
集合中没有元素,则返回true

boolean contains(Object o);
集合中包含指定元素,则返回true, 具体的说,如果collection中就一个元素e, 就是比较o.equals(e)

我们可以重写o.equals()方法来定制化 o 和 e 是否相等。

package com.conlection;
import java.util.ArrayList;
import java.util.Collection;
/**
 * 
 * contains 方法例子
 * @author wangsy
 */
public class Collection1 {
	public static void main(String[] args) {
		test1();//return true
		test2();//return true
		test3();//return false
		test4();//return false
		test5();//return true
		test6();//return false
	}
	
	static void test1() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		
		System.out.println(collection.contains(2));
	}
	
	static void test2() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(null);
		collection.add(2);
		
		System.out.println(collection.contains(null));
	}
	
	static void test3() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		
		System.out.println(collection.contains(null));
	}
	
	static void test4() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "aa"));
		collection.add(new Apple("green", "bb"));
		
		Apple apple = new Apple("red", "aa");
		System.out.println(collection.contains(apple));
	}
	static void test5() {
		Collection<Apple> collection = new ArrayList<Apple>();
		Apple apple1 = new Apple("red", "aa");
		Apple apple2 = new Apple("green", "bb"); 
		collection.add(apple1);
		collection.add(apple2);
		
		System.out.println(collection.contains(apple1));
	}
	
	static void test6() {
		Collection<Apple> collection = new ArrayList<Apple>();
		Apple apple1 = new Apple("red", "aa");
		Apple apple2 = new Apple("green", "bb"); 
		collection.add(apple1);
		collection.add(apple2);
		
		System.out.println(collection.contains(null));
	}
	
	static class Apple {
		String color;
		String name;
		
		public Apple() {};
		
		public Apple(String color, String name) {
			this.color = color;
			this.name = name;
		}

		String getColor() {
			return color;
		}

		void setColor(String color) {
			this.color = color;
		}

		String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}
		
		@Override
		public int hashCode() {
			return name.hashCode()*3 + color.hashCode();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(this == obj) {
				return true;
			}
			if(obj == null) {
				return false;
			}
			
			if(this.getClass() != obj.getClass()) {
				return false;
			}
			Apple _obj = (Apple)obj;
			return name.equals(_obj.getName()) && color.equals(_obj.getColor());
		}
		
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("color:").append(color).append(" name:").append(name);
			return sb.toString();
		}
	}
}





Iterator<E> iterator();
返回当前集合的迭代器对象

Object[] toArray();

返回集合所有元素

package com.conlection;
import java.util.ArrayList;
import java.util.Collection;
/**
 * 
 * toArray 方法例子
 * @author wangsy
 */
public class Collection2 {
	public static void main(String[] args) {
		test1();
		test2();
		//test3();//java.lang.ArrayStoreException
		//test4();//java.lang.NullPointerException
		test5();
	}
	
	static void test1() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		
		for(Object obj : collection.toArray()) {
			System.out.println(obj);
			System.out.println(Integer.toString((int)obj));
		}
	}
	
	static void test2() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		
		for(Object obj : collection.toArray()) {
			Apple _obj = (Apple)obj;
			System.out.println(_obj.toString());
		}
	}
	
	static void test3() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		
		for(String obj : collection.toArray(new String[0])) {
			System.out.println(obj.toString());
		}
	}
	
	static void test4() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		
		Apple[] apples = null;
		for(Apple obj : collection.toArray(apples)) {
			System.out.println(obj.toString());
		}
	}
	
	static void test5() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		
		Apple[] apples = new Apple[0];
		for(Apple obj : collection.toArray(apples)) {
			System.out.println(obj.toString());
		}
	}
	
	
	
	static class Apple {
		String color;
		String name;
		
		public Apple() {};
		
		public Apple(String color, String name) {
			this.color = color;
			this.name = name;
		}

		String getColor() {
			return color;
		}

		void setColor(String color) {
			this.color = color;
		}

		String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}
		
		@Override
		public int hashCode() {
			return name.hashCode()*3 + color.hashCode();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(this == obj) {
				return true;
			}
			if(obj == null) {
				return false;
			}
			
			if(this.getClass() != obj.getClass()) {
				return false;
			}
			Apple _obj = (Apple)obj;
			return name.equals(_obj.getName()) && color.equals(_obj.getColor());
		}
		
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("color:").append(color).append(" name:").append(name);
			return sb.toString();
		}
	}
}





<T> T[] toArray(T[] a);
按照T返回对应泛型的所有元素

boolean add(E e);
新增新元素e

boolean remove(Object o);

移除一个和e.equals(o)的元素 e

package com.conlection;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 
 * remove 方法例子
 * @author wangsy
 */
public class Collection3 {
	public static void main(String[] args) {
		//test1();
		//test2();
		test3();
	}
	
	static void test1() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		collection.remove(2);
		
		for(Object obj : collection.toArray()) {
			System.out.println(obj);
		}
	}
	static void test2() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		collection.add(2);
		collection.add(3);
		
		collection.remove(2);
		
		for(Object obj : collection.toArray()) {
			System.out.println(obj);
		}
	}
	
	static void test3() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		collection.add(new Apple("red", "name3"));
		collection.add(new Apple("red", "name3"));
		collection.add(new Apple("red", "name3"));
		
		collection.remove(new Apple("red", "name3"));
		
		for(Apple apple : collection.toArray(new Apple[0])) {
			System.out.println(apple.toString());
		}
	}
	
	static class Apple {
		String color;
		String name;
		
		public Apple() {};
		
		public Apple(String color, String name) {
			this.color = color;
			this.name = name;
		}

		String getColor() {
			return color;
		}

		void setColor(String color) {
			this.color = color;
		}

		String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}
		
		@Override
		public int hashCode() {
			return name.hashCode()*3 + color.hashCode();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(this == obj) {
				return true;
			}
			if(obj == null) {
				return false;
			}
			
			if(this.getClass() != obj.getClass()) {
				return false;
			}
			Apple _obj = (Apple)obj;
			return name.equals(_obj.getName()) && color.equals(_obj.getColor());
		}
		
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("color:").append(color).append(" name:").append(name);
			return sb.toString();
		}
	}
}





boolean containsAll(Collection<?> c);
如果c是当前collection的子集,返回true
package com.conlection;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 
 * containsAll 方法例子
 * @author wangsy
 */
public class Collection4 {
	public static void main(String[] args) {
//		test1();
//		test2();
//		test3();
		test4();
	}
	
	static void test1() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		Collection<Integer> collection1 = new ArrayList<Integer>();
		collection1.add(1);
		
		System.out.println(collection.containsAll(collection1));
	}
	
	static void test2() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		Collection<Integer> collection1 = new ArrayList<Integer>();
		collection1.add(4);
		
		System.out.println(collection.containsAll(collection1));
	}
	
	static void test3() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		Collection<Integer> collection1 = new ArrayList<Integer>();
		collection1.add(2);
		collection1.add(3);
		
		System.out.println(collection.containsAll(collection1));
	}
	
	static void test4() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		Collection<Integer> collection1 = new ArrayList<Integer>();
		collection1.add(2);
		collection1.add(3);
		collection1.add(4);
		
		System.out.println(collection.containsAll(collection1));
	}
	
	static class Apple {
		String color;
		String name;
		
		public Apple() {};
		
		public Apple(String color, String name) {
			this.color = color;
			this.name = name;
		}

		String getColor() {
			return color;
		}

		void setColor(String color) {
			this.color = color;
		}

		String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Apple) {
				Apple _obj = (Apple)obj;
				if(_obj == this) {
					return true;
				} else {
					if(name.equals(_obj.getName()) && color.equals(_obj.getColor())) {
						return true;
					} else 
						return false;
				}
			} else {
				return false;
			}
		}
		
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("color:").append(color).append(" name:").append(name);
			return sb.toString();
		}
	}
}




boolean addAll(Collection<? extends E> c);
把指定集合的所有元素add到当前集合中
The behavior of this operation is undefined if  the specified collection is modified while the operation is in progress.(This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.) 没有验证过。未完待续

boolean removeAll(Collection<?> c);
在当前集合中删除子集c

boolean retainAll(Collection<?> c);
当前集合保留子集c,也就是删除除了子集c的其他元素

void clear();
清空所有元素,此操作后,集合清空

boolean equals(Object o);

int hashCode();

下一篇博文:java.util.Collection学习 - 浅谈ArrayList

© 著作权归作者所有

共有 人打赏支持
crowley
粉丝 0
博文 3
码字总数 1851
作品 0
朝阳
高级程序员
私信 提问
​比较Collection 和Collections的区别

1、java.util.Collection 是一个集合接口。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供了最...

itfanr
2014/11/24
0
0
Java中Collection和Collections的区别

1、java.util.Collection 是一个集合接口(集合类的一个顶级接口)。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是...

ke_ry
2016/08/29
0
0
Java for-each循环解惑

考虑下面这段遍历一个用户自定义的aggregator或collection类的代码,这段代码将会打印出什么,抛出异常还是编译器错误: package javaForEach; /** * Java Class to show how for-each loop ...

markGao
2014/04/25
0
0
collection和collections的区别

查阅API文档会发现, java.util.Collection 是一个集合接口,提供了对集合对象进行基本操作的通用接口方法,它是各种集合接口的父接口,list和set继承自Collection , java.util.Collection...

lCarrieLiul
06/29
0
0
java基础巩固笔记(4)-代理

java基础巩固笔记(4)-代理 标签: java [TOC] 代理 代理是实现AOP(Aspect oriented program,面向编程)的核心和关键技术。 概念 代理是一种设计模式,其目的是为其他对象提供一个代理以控制对...

brianway
2016/02/03
112
0

没有更多内容

加载失败,请刷新页面

加载更多

阿里千万级高性能、高并发架构的经验之谈

架构以及我理解中架构的本质 在开始谈我对架构本质的理解之前,先谈谈对今天技术沙龙主题的个人见解,千万级规模的网站感觉数量级是非常大的,对这个数量级我们战略上 要重 视 它 , 战术上又...

别打我会飞
24分钟前
3
0
Adnroid架构的详细说明

armeabi armeabi是一个非常老的基于ARM的架构。从Android 4.4开始,CDD(compatibility definition)严格要求ARMv7读取CDD文档。 CDD是Google向设备制造商提供的每个Android版本的规范,它包...

CrazyManDF
26分钟前
3
0
微信小程序内嵌网页web-view

web-view 组件是一个可以用来承载网页的容器,会自动铺满整个小程序页面。个人类型与海外类型的小程序暂不支持使用。 客户端 6.7.2 版本开始,navigationStyle: custom 对 <web-view> 组件无...

xiaogg
27分钟前
2
0
单例模式

第一种方式 public class SingletonA { public static final SingletonA INSTANCE = new SingletonA(); private SingletonA(){ //do something }} 第二种方式 public......

wuyiyi
27分钟前
2
0
git: Authentication failed for错误解决

如果push遇到在输入密码是输错后,就会报这个错误fatal: Authentication failed for 解决办法: git config --system --unset credential.helper 之后你在push就会提示输入名称和密码...

落雪飞声
28分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部