文档章节

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
2018/06/29
0
0
Collection 和 Map 遍历过程的区别

1:Collection 和 Map 都是Java体系为我们提供的强大集合工具,他们的遍历是最多的操作,那么他们有什么不同呢?请看如下代码 import java.util.ArrayList;import java.util.Collection;imp...

刘付kin
2016/12/07
4
0

没有更多内容

加载失败,请刷新页面

加载更多

iOS个人中心渐变动画、微信对话框、标签选择器、自定义导航栏、短信验证输入框等源码

iOS精选源码 简单的个人中心页面-自定义导航栏并予以渐变动画 程序员取悦女票的正确姿势---Tip1(iOS美容篇) iOS 前台重启应用和清除角标的问题 微信原生提醒对话框3.0 JHLikeButton - 有趣...

Android爱开源
5分钟前
0
0
Yii2使用驼峰命名的形式访问控制器

yii2在使用的时候,访问控制器的时候,如果控制器的名称是驼峰命名法,那访问的url中要改成横线的形式。例如: public function actionRoomUpdate(){//}//访问的时候就要www.test.co...

dragon_tech
8分钟前
0
0
Navicat使用教程:使用Navicat Query Analyzer优化查询性能(第2部分)

下载Navicat Monitor最新版本 Navicat Monitor 是一套安全、简单而且无代理的远程服务器监控工具。它具有强大的功能使你的监控发挥最大效用。受监控的服务器包括 MySQL、MariaDB 和 Percona ...

电池盒
14分钟前
0
0
Python3 读写utf-8文本文件

with open('testRead.txt', 'r', encoding='utf-8') as f: for each_line in f: Passwith open('testWrite.txt', 'w', encoding='utf-8') as f: f.write('写入的内容'......

编程老陆
17分钟前
0
0
Linux syslog相关函数详解

介绍 syslog是Unix系统的日志系统。可以将日志记录在本地系统中。 一个完整的syslong日志包含如下信息:程序模块 | 严重性 | 时间 | 主机名 | 进程名 | 进程ID | 正文。 syslong相关函数 1....

RongJinhui0
22分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部