Java基础-引用数据类型之集合(Collection)
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.为什么出现集合类
面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就可以将对象进行存储,集合就是存储对象最常用的一种方式(容器),Java中集合也是类,真正用来存储东西的是某种集合类的实例对象。
二.集合类的特点
数据和集合类都是容器,有何不同?集合类的特点又是什么呢?
1>.数组的特点
a>.长度是固定的;
b>.可以存储基本数据类型;
c>.也可以存储对象的引用;
d>.必须是相同类型的数据;
2>.集合的特点
a>.长度是可变的;
b>.只能用于存储对象的引用;
c>.对象可以是不同类型;
三.Collection接口概述
Collection是集合中的根接口。collection表示一组对象,这个对象也称为collection的元素,一些collection允许有重复的元素,而另一些则不允许,请记住下面这张图,有助你学习Java中的集合。
如上图所示,Collection根接口有两个子接口,分别是List和Set:
1>.List接口
可以存放重复远古三,元素存取是“有序”的。
2>.Set接口
不可以存放重复元素,通常元素存取是“无序”的,也有一些实现类元素是“有序”的。
注意:这里的“有序”,“无序”指的是存放元素是是否会记住元素存放的顺序,并非对元素进行“排序”。
四.Collection接口的基本方法
1>.add()方法【往集合中添加元素】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 System.out.println(coll);21 }22 }23 24 25 /*26 以上代码执行结果如下:27 [yinzhengjie, alex, Big data]28 */
2>.clear()方法【清空集合的内容】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 System.out.println(coll);21 //清空集合内容22 coll.clear();23 System.out.println(coll);24 }25 }26 27 28 /*29 以上代码执行结果如下:30 [yinzhengjie, alex, Big data]31 []32 */
3>.Contains(Object o )方法【判断对象是否存在于集合中】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 //查询集合是否存在“yinzhengjie”这个字符串21 boolean b = coll.contains("yinzhengjie");22 System.out.println(b);23 }24 }25 26 27 /*28 以上代码执行结果如下:29 true30 */
4>.size()方法【查看集合的长度】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 //查看集合的大小21 int length = coll.size();22 System.out.println(length);23 }24 }25 26 27 /*28 以上代码执行结果如下:29 330 */
5>.toArray()方法【返回的是一个存储对象的数组,数组的存储数据类型是Object】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 21 //将集合转换成一个数组22 Object[] array = coll.toArray();23 for (Object object : array) {24 System.out.println(object);25 }26 }27 }28 29 30 /*31 以上代码执行结果如下:32 yinzhengjie33 alex34 Big data35 */
6>.remove方法【移除集合中指定的元素,若有多个想用元素就删除第一个匹配到的元素】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 coll.add("yinzhengjie");21 22 System.out.println(coll);23 24 boolean b = coll.remove("yinzhengjie");25 System.out.println(b);26 System.out.println(coll);27 }28 }29 30 31 /*32 以上代码执行结果如下:33 [yinzhengjie, alex, Big data, yinzhengjie]34 true35 [alex, Big data, yinzhengjie]36 */
7>.addAll(Collection c)方法【将一个集合中的所有元素添加到当前集合中】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 coll.add("yinzhengjie");21 22 System.out.println(coll);23 24 Collection coll2 = new ArrayList();25 coll2.add("1");26 coll2.add("2");27 coll2.add("3");28 System.out.println(coll2);29 boolean res = coll.addAll(coll2);30 System.out.println(res);31 System.out.println(coll);32 }33 }34 35 36 /*37 以上代码执行结果如下:38 [yinzhengjie, alex, Big data, yinzhengjie]39 [1, 2, 3]40 true41 [yinzhengjie, alex, Big data, yinzhengjie, 1, 2, 3]42 */
8>.removeAll(Collection c)方法【删除与传入集合共有的元素】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 coll.add("yinzhengjie");21 22 System.out.println(coll);23 24 Collection coll2 = new ArrayList();25 coll2.add("yinzhengjie");26 coll2.add("alex");27 System.out.println(coll2);28 boolean res = coll.removeAll(coll2);29 System.out.println(res);30 System.out.println(coll);31 }32 }33 34 35 /*36 以上代码执行结果如下:37 [yinzhengjie, alex, Big data, yinzhengjie]38 [yinzhengjie, alex]39 true40 [Big data]41 */
9>.containsAll(Collection c)【判断一个集合是否包含另外一个集合】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 coll.add("alex");19 coll.add("Big data");20 coll.add("yinzhengjie");21 22 Collection coll2 = new ArrayList();23 coll2.add("yinzhengjie");24 coll2.add("alex");25 System.out.println(coll2);26 boolean res = coll.containsAll(coll2);27 System.out.println(res);28 System.out.println(coll);29 }30 }31 32 33 /*34 以上代码执行结果如下:35 [yinzhengjie, alex]36 true37 [yinzhengjie, alex, Big data, yinzhengjie]38 */
10>.isEmpty()方法【判断集合是否为空】
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.ArrayList;10 import java.util.Collection;11 12 public class CollectionDemo {13 public static void main(String[] args) {14 //接口多态的方式调用15 Collectioncoll = new ArrayList ();16 //存储数据17 coll.add("yinzhengjie");18 System.out.println(coll.isEmpty());19 20 }21 }22 23 24 /*25 以上代码执行结果如下:26 false27 */