01 深入了解java集合之collection

本文深入讲解Java集合框架的基础概念及核心接口Collection的使用方法,包括添加、删除、判断和长度等功能,并通过示例演示如何存储及遍历字符串与自定义对象。

集合

java提供多种集合,他们的数据结构不同,但是他们肯定有共性的内容(存储、获取、判断等),通过不断的向上提取,我们就能够得到一个集合的继承体系结构图如下图,这个体系的老大是Collection,要想学习集合,先需要掌握Collection。

集合类的特点:   集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象
集合类存放于java.util包中

一 Collection (集合的基本接口,List、Set是其子接口)

          功能:添加、删除、判断、长度

package it.Collection;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionTestDamo1{
	public static void main(String[] args) {
	/*collection的功能:
	   添加
	   boolean add(E e)
	   boolean addAll(Collection c)
	   删除
           boolean remove(Object o)
           boolean removeAll(Collection c)
           void clear()
           判断
           boolean contains(Object o)判断集合中是否包含某个元素
           boolean isEmpty()判断集合是否为空
           boolean containsAll(Collection c)判断集合中是否包含另一个集合
           boolean retainAll(Collection c) 假设A集合 和B集合,A对 B做交集最终的结果保存在A中,B不变,返回值表示的是A中是否发生改变
           长度
           int size()*/
		Collection<String> c = new  ArrayList<String>();
		Collection<String> c1 = new  ArrayList<String>();
            //添加功能可以存多个重复元素
		c.add("唐僧");
		c.add("唐僧");
		c.add("孙悟空");
		c.add("猪八戒");
		c.add("沙和尚");
		System.out.println(c);
		System.out.println("--------------");
		c1.add("白骨精");
		c1.add("红孩儿");
		c1.add("牛魔王");
		c1.add("孙悟空");
		System.out.println(c1);
		System.out.println("--------------");
		c.addAll(c1);
		System.out.println(c);
	       //删除
		c.clear();
		c.removeAll(c1);//注意这里将重复的孙悟空也移除啦
    	        System.out.println(c.remove("沙和尚"));//删除成功返回true,否则false
		System.out.println(c);
		//判断
		System.out.println(c.contains("唐僧"));//包含则返回true,否则false
		System.out.println(c.isEmpty());//包含则返回true,否则false
		System.out.println(c.containsAll(c1));//包含则返回true,否则false
		System.out.println(c.retainAll(c1));//
		System.out.println(c);
		System.out.println(c1);//假设A集合 和B集合,A对 B做交集最终的结果保存在A中,B不变,返回值表示的是A中是否发生改变
	        //长度
	        System.out.println(c.size());
	        System.out.println(c1.size());
		
	}
}





	



     Iterator:迭代器,可以通过迭代器遍历集合中的数据 (获取)

     练习题 :1 存储字符串并遍历  2 存储自定义对象并遍历Student(name age)

     首先创建学生类

package it.Collection;

public class Student {
	private String name;
	private Integer age;
	public Student(){}
	public Student(String name,Integer age){
		this.name =name;
		this.age =age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	

}

练习题实操

package it.Collection;

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

public class CollectionTestDamo2 {
	public static void main(String[] args) {
		/*
		 * collection的获取功能(迭代器:集合专用的遍历方式):
		 * Iterator接口 :对collection进行迭代的迭代器,依赖于集合而存在 练习题: 1 存储字符串并遍历 2 存储自定义对象并遍历
		 * Student(name,age)
		 */
		// 1 存储字符串并遍历
		/*
		 * Collection<String> c = new ArrayList<String>(); c.add("唐僧");
		 * c.add("唐僧"); c.add("孙悟空"); c.add("猪八戒"); c.add("沙和尚");
		 * Iterator<String> i = c.iterator(); while (i.hasNext()) { String s =
		 * i.next(); System.out.println(s); }
		 */
		// 2 存储自定义对象并遍历 Student(name,age)
		Collection<Student> c = new ArrayList<Student>();
		Student s1 = new Student("唐僧", 24);
		Student s2 = new Student("孙悟空", 23);
		Student s3 = new Student("猪八戒", 22);
		Student s4 = new Student("沙和尚", 21);
		c.add(s1);
		c.add(s2);
		c.add(s3);
		c.add(s4);
		Iterator<Student> i = c.iterator();
		while (i.hasNext()) {
			Student s = i.next();
			System.out.println(s.getName() + "-------" + s.getAge());

		}
	}
}


                                                                                                                                                                                                  2018年2月8日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值