Java基础系列之集合

集合

集合的由来?

  学生的面向对象语言,面向对象语言对事物的描述是通过对象体现的,那么需要来存储多个对象.
 要存储多个对象,不能使用基本类型的变量,需要使用容器类型的变量? 学习过哪些容器变量? 数组 ,字符串缓冲区(StringBuffer)
 对于字符串缓冲区来说,在内存中始终存储的是字符串,不能满足要求;数组呢,数组的长度是固定的,不符合长度变化的要求,所有Java提供了一个
 Collection集合;
 面试题:
  数组和集合的区别?
  1)长度区别:
  数组长度固定
  集合长度可变
  2)内容的区别
  数组只能存储同一种类型的元素
  集合可以存储多种类型的元素
  3)存储类型的区别
  数组:可以存储基本类型,也可以存储引用类型 String[] str = {"hello","world","java",100} ; 错误的
  集合:只能存储引用类型
 
 集合:Collection:子接口有两个,两个子接口分别对应多个子实现类,多个集合数据结构不同,但是他们有共性内容,将共性内容

 抽取出来

集合的继承体系图


 数据结构:数据存储方式
 Collection:
  Collection  层次结构中的根接口。Collection表示一组对象,这些对象也称为 collection的元素。一些 collection中允许有重复的元素,而另一些则不允许。
 一些 collection是有序的,而另一些则是无序的。
  JDK不提供此接口的任何直接实现:它提供更具体的子接口,更具体的实现类
 基本功能:
 添加功能:
  boolean add(Object e)
 删除功能:
  void clear() :删除集合中所有元素(暴力删除)
  boolean remove(Object o):删除集合中的指定元素
 判断功能:
  boolean contains(Object o):集合中是否包含指定的元素
 获取功能:
  int size() :获取集合中的元素数
 
 
  boolean retainAll(Collection c) :交集功能:   A集合对B集合取交集元素  :思考
  boolean 表达的是什么意思,交集的元素是去A集合还是去B集合中
  boolean removeAll(Collection c):删除一个集合中所有元素: 思考: 删除一个元素算是删除还是删除所有算是删除?
 
转换功能:

  Object[] toArray() :将集合转换成数组

代码示例

package org.westos;

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

public class RegexDemo {
	public static void main(String[] args) {

		// 创建集合对象
		// Collection c = new Collecton() ; //不能实例化
		Collection c = new ArrayList();
		System.out.println(c);
		System.out.println(c.isEmpty());
		// boolean add(Object e) :添加元素
		/*
		 * boolean flag = c.add("hello") ; System.out.println(flag);
		 */
		/**
		 * add()方法的源码 public boolean add(E e) { ensureCapacityInternal(size + 1); //
		 * Increments modCount!! elementData[size++] = e; return true; //只要添加就返回true }
		 */
		c.add("hello");
		c.add("world");
		c.add("java");
		System.out.println("c:" + c);

		// 删除功能:
		// c.clear();
		System.out.println("remove():" + c.remove("java"));
		System.out.println("c:" + c);

		// boolean contains(Object o):集合中是否包含指定的元素
		System.out.println("contains():" + c.contains("android"));
		System.out.println("contains():" + c.contains("hello"));

		System.out.println(c.size());
		c.add("javaweb");
		System.out.println("c:" + c);
		// boolean isEmpty() :判断集合是否为空
		System.out.println(c.isEmpty());
	}
}

Colleciton的集合的高级功能:
  boolean addAll(Collection c)  :添加一个集合中的所有元素
  boolean removeAll(Collection c):删除的高级功能 删除集合和指定集合的公共元素,如果公共元素只有一个仍然返回true
  boolean containsAll(Collection c):包含所有元素算是包含
        交集功能:

boolean retainAll(Collection c):A集合对B集合取交集,A集合中存储的是交集元素,B集合保持不变。

public class CollectionDemo {
	
	public static void main(String[] args) {
		
		//创建Collection集合1
		Collection c1 = new ArrayList() ;
		
		//添加元素
		c1.add("abc1") ;
		c1.add("abc2") ;
		c1.add("abc3") ;
		c1.add("abc4") ;
		
		
		//创建第二个集合
		Collection c2 = new ArrayList() ;
		c2.add("abc1") ;
		c2.add("abc2") ;
		c2.add("abc3") ;
		c2.add("abc4") ;
//		c2.add("abc5") ;
//		c2.add("abc6") ;
//		c2.add("abc7") ;
		
		System.out.println("c1:"+c1);
		System.out.println("c2:"+c2);
		System.out.println("--------------------------------");
		//boolean addAll(Collection c)  :添加一个集合中的所有元素
		System.out.println("addAll():"+c1.addAll(c2));
		//	boolean removeAll(Collection c):删除的高级功能(思考:删除一个算是删除还是删除所有算是删除?)
		//结论:删除一个算是删除...
//		System.out.println("removeAll():"+c1.removeAll(c2));
		
		//boolean containsAll(Collection c):包含所有元素算是包含,还是包含一个算是包含
		//结论:包含所有算是包含
//		System.out.println("containsAll():"+c1.containsAll(c2));
		
		//	boolean retainAll(Collection c):A集合对B集合取交集,交集的元素到底是去A集合还是去B集合中,
			//返回值boolean表达什么意思?
		/**
		 * 结论:A集合对B集合取交集,交集的元素要去A集合中,boolean返回值表达的A集合的元素是否发生变化,如果发生变化,则返回true,否则,返回false
		 */
		System.out.println("retainAll():"+c1.retainAll(c2));
		
		System.out.println("c1:"+c1);
		System.out.println("c2:"+c2);
	}
}

转换功能:Object[] toArray() :将集合转换成数组

 需求:集合中存储的是String类型的数据,并通过toArray()函数将集合转换成数组进行遍历

代码示例

public class CollectionDemo2 {
	
	public static void main(String[] args) {
		
		//创建一个集合对象
		Collection c = new ArrayList() ;
		
		//给集合中添加元素
		c.add("hello") ;
		c.add("world") ;
		c.add("java") ;
		c.add("JavaEE") ;
		
		//需要去转换
//		Object[] toArray()
		//将集合转成数组
		Object[] objs = c.toArray() ;
		for(int x =0 ; x < objs.length ; x ++) {
//			System.out.println(objs[x]);
			//需求:输出不同的元素的同时,还需要输出字符串元素的长度
//			System.out.println(objs[x]+"----"+objs[x].length());
			
			/**
			 *上面代码有问题
			 * length():属于String类型的特有功能,可以获取字符串长度
			 */
			String str = (String) objs[x] ;  //相当于:向下转型
			System.out.println(str+"----"+str.length());
		}
	}
需求:存储自定义对象并遍历

 有5个学生,学生有姓名,年龄,遍历5个学生,输出5个学生对应的信息(集合改进)
 1)创建一个学生类
 2)在测试类中,创建集合对象
 3)创建5个具体学生对象,并且添加集合中
 4)将集合转换数组
 5)遍历

 6)输出

代码示例

public class CollectionDemo {

	public static void main(String[] args) {

		// 创建一个集合对象
		Collection c = new ArrayList();

		// 创建5个具体学生对象
		Student s1 = new Student("高圆圆", 27);
		Student s2 = new Student("杨桃", 28);
		Student s3 = new Student("王力宏", 35);
		Student s4 = new Student("周星驰", 60);
		Student s5 = new Student("成龙", 55);

		// 添加到集合中
		c.add(s1);
		c.add(s2);
		c.add(s3);
		c.add(s4);
		c.add(s5);

		// 转换数组
		Object[] objs = c.toArray();
		// 遍历
		for (int x = 0; x < objs.length; x++) {
			// System.out.println(objs[x]);

			// 需求:需要的通过getXXX方法获取学生信息

			Student s = (Student) objs[x]; // 向下转型
			System.out.println(s.getName() + "----" + s.getAge());
		}
	}
}

迭代器

Iterator iterator() :集合迭代器方法(获取集合的迭代器)

 集合专有的遍历方式:迭代器遍历

 Iterator :接口中有以下的方法: boolean hasNext() 如果有下一个元素可以迭代,那么返回true,否则返回false 

 Object next() 获取下一个迭代的元素。 存储String类型的元素

图解


迭代器源码分析

interface Iterator{
	public abstract boolean hasNext() ;
	public Object next();
}

interface Iterable{
	Iterator  iterator() ;
}

interface Collection extends Iterable{
	Iterator  iterator() ;
	
}

inteface List extends Collection{
	public abstract boolean hasNext() ;   
	public Object next();
	Iterator  iterator() ;
}

class ArrayList implements List{
	 public Iterator iterator() {
        return new Itr();//Itr这个类就是Iterator的子实现类
    }
    
     private class Itr implements Iterator{   //Itr为Iterator的子实现了类 
     	
     	
     	//实现了hasNext()和next()
     	public boolean hasNext(){
     	
     	}
     	
     	public Object next(){
     	
     	}
     }
}
	Iterator it = c.iterator() ;   //接口多态的形式  =左边为接口,右边为接口的子实现类
                                                         //c.iterator()即为 new Itr();  Itr为Iterator的子实现类
                                                         //Iterator it = c.iterator() ;相当于Iterator it= new Itr(); 
while(it.hasNext()) {
//			Object obj = it.next() ;  //Object obj = new Objct();
//			
//			//需求:打印字符串同时,获取长度
			String str = (String)(it.next) ;
			System.out.println(str+"---"+str.length());
		}

代码示例

public class IteratorDemo {
	
	public static void main(String[] args) {
		//创建集合对象
		Collection c = new ArrayList() ;
		
		//添加元素
		c.add("hello");
		c.add("world");
		c.add("java");
		c.add("javaee");
		
		//通过集合获取迭代器
		Iterator it = c.iterator() ;
		Iterator iterator=c.iterator();
		//获取下一个元素
//		System.out.println(it.next()); 
//		System.out.println(it.next());
//		System.out.println(it.next()); 
//		System.out.println(it.next()); 
		//java.util.NoSuchElementException :此处已经没有元素了,但是还要获取,出现了这个异常
//		System.out.println(it.next()); 
		
		//改进:需要判断是否有下一个可以迭代的元素,有,就获取,没有,不管你
		/*if(it.hasNext()) {
			System.out.println(it.next());
		}
		
		if(it.hasNext()) {
			System.out.println(it.next());
		}
		
		if(it.hasNext()) {
			System.out.println(it.next());
		}
		
		if(it.hasNext()) {
			System.out.println(it.next());
		}
		
		if(it.hasNext()) {
			System.out.println(it.next());
		}*/
		
		//上面代码重复到高,所以改进:使用while循环
		while(it.hasNext()) {
//			Object obj = it.next() ;  //Object obj = new Objct();
//			
//			//需求:打印字符串同时,获取长度
//			String str = (String) obj ;
			String str = it.next();
			System.out.println(str+"---"+str.length());
		}
		
	}
}
package org.westos_03;

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

/**
 * 存储自定义对象并遍历(使用专有遍历方式)
 *
 */
public class IteratorDemo2 {

	public static void main(String[] args) {

		// 创建集合对象:
		Collection c = new ArrayList();

		// 创建学生对象
		Student s1 = new Student("张三", 27);
		Student s2 = new Student("李四", 29);
		Student s3 = new Student("王五", 25);

		// 添加集合中
		c.add(s1);
		c.add(s2);
		c.add(s3);

		// 获取迭代器
		Iterator it = c.iterator();
		while (it.hasNext()) {
			Student s = (Student) it.next();
			System.out.println(s.getName() + " " + s.getAge());
		}
	}
}

package org.westos_03;

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

/**
 * 注意: 1)it.next(),只使用一次即可,使用多次,会出现问题(it.next(),每次使用的时候都是返回一个对象)
 * 2)遍历的时候使用的while循环,可不可以使用for循环呢?
 */
public class IteratorTest {

	public static void main(String[] args) {

		// 创建一个集合对象
		Collection c = new ArrayList();

		// 创建学生对象
		Student s1 = new Student("张三", 28);
		Student s2 = new Student("李四", 30);
		Student s3 = new Student("王五", 22);

		// 添加到集合
		c.add(s1);
		c.add(s2);
		c.add(s3);

		// 获取迭代器
		Iterator it = c.iterator();
		// 遍历
		while (it.hasNext()) {// 这种方式才是我们的模板代码
			// System.out.println(((Student) it.next()).getName() + "----" + ((Student)
			// it.next()).getAge());

			Student s = (Student) it.next();
			System.out.println(s.getName() + "---" + s.getAge());

		}

		System.out.println("-----------------------------");
		// 下面很少用
		for (Iterator it2 = c.iterator(); it2.hasNext();) {
			Student s = (Student) it2.next();
			System.out.println(s.getName() + "---" + s.getAge());
		}
	}
}

List集合的特点:

  有序 (存储和取出一致),可以允许重复元素

Set集合的特点:

  无序(不能保证迭代顺序,基于hashMap),并且元素不能重复

代码示例

public class ListDemo {

public static void main(String[] args) {
		
		//创建集合对象
		List list = new ArrayList() ;
		
		//存储重复元素
		list.add("hello") ;
		list.add("world") ;
		list.add("hello") ;
		list.add("java") ;
		list.add("javaee") ;
		list.add("javaee") ;
		list.add("android") ;
		list.add("android") ;
		
		//遍历
		Iterator it = list.iterator() ;
		while(it.hasNext()) {
			String s = (String)it.next() ;
			System.out.println(s);
		}
	}
}
List集合的特有功能:
  添加功能
  void add(int index,Object element):在指定位置处添加指定元素
  获取功能
  Object get(int index)获取指定位置的元素。
  ListIterator listIterator():列表迭代器
  删除功能:
  Object remove(int index):删除指定位置的元素
        修改功能

  Object set(int index, Object element):用指定的element元素替换掉指定位置处的元素

代码示例

public class ListDemo3 {

	public static void main(String[] args) {
		// 创建集合对象
		List list = new ArrayList();

		// 添加元素
		list.add("hello");
		list.add("world");
		list.add("java");

		System.out.println("list:" + list);

		// void add(int index,Object element):在指定位置处添加指定元素
		list.add(1, "javaee");
		System.out.println("list:" + list);

		System.out.println("---------------------");

		// Object get(int index)返回列表中指定位置的元素。
		System.out.println("get():" + list.get(2));
		System.out.println("---------------------");
		// Object remove(int index):删除指定位置处的元素
		System.out.println("remove():" + list.remove(3));

		// Object set(int index, Object element):用指定element元素替换掉指定位置处的元素
		System.out.println("set():" + list.set(2, "android"));
		System.out.println("list:" + list);
	}
}

遍历List集合的方式

代码示例

package org.westos_04;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.SynchronousQueue;

public class ListIteratorDemo {
	
public static void main(String[] args) {
		
		//创建集合对象
		List list  = new ArrayList() ;
		
		//存储元素
		list.add("hello") ;
		list.add("world") ;
		list.add("java") ;
		System.out.println("list:"+list);
		System.out.println("---------------------");
		//遍历(可以是Collection的iterator())
		//使用普通遍历
		System.out.println(list.get(0));
		System.out.println(list.get(1));
		System.out.println(list.get(2));
		System.out.println("---------------------");
		//角标越界:没有元素了
//		System.out.println(list.get(3));
		//循环
		for(int x = 0 ; x < 3 ;x ++) {
			System.out.println(list.get(x));
		}
		
		System.out.println("---------------------");
		
		//size()和get(int index)相集合
		for(int x = 0; x < list.size(); x ++) {
			System.out.print(list.get(x)+" ");
		}
		
		
	}
}
使用List集合存储自定义对象并遍历

代码示例

public class ListTest {
	
	public static void main(String[] args) {
		
		//创建集合
		List list = new ArrayList() ;
		
		//创建学生对象
		Student s1 = new Student("西施", 27) ;
		Student s2 = new Student("杨贵妃", 25) ;
		Student s3 = new Student("王昭君", 22) ;
		Student s4 = new Student("貂蝉", 20) ;
		
		//存储
		list.add(s1) ;
		list.add(s2) ;
		list.add(s3) ;
		list.add(s4) ;
		
		//获取迭代器
		Iterator it = list.iterator() ;
		while(it.hasNext()) {
			Student s = (Student)it.next() ;
			System.out.println(s.getName()+"----"+s.getAge());
		}
	}
}
package org.westos_04;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 存储自定义对象并遍历 size()和get(int index):相结合 普通for循环的方式
 *
 */
public class ListTest2 {

	public static void main(String[] args) {

		// 创建集合对象
		List list = new ArrayList();

		// 存储元素
		list.add("hello");
		list.add("world");
		list.add("java");
		System.out.println("list:" + list);
		System.out.println("---------------------");
		// 遍历(可以是Collection的iterator())
		// 使用普通遍历
		System.out.println(list.get(0));
		System.out.println(list.get(1));
		System.out.println(list.get(2));
		System.out.println("---------------------");
		// 角标越界:没有元素了
		// System.out.println(list.get(3));
		// 循环
		for (int x = 0; x < 3; x++) {
			System.out.println(list.get(x));
		}

		System.out.println("---------------------");

		// size()和get(int index)相集合
		for (int x = 0; x < list.size(); x++) {
			System.out.print(list.get(x) + " ");
		}

	}
}

存储自定义对象并遍历,两种方式 方式1:Iterator iterator(); 方式2:size()和get()方法相结合

代码示例

public class ListTest3 {

	public static void main(String[] args) {

		// 创建集合对象
		List list = new ArrayList();

		// 创建4个学生对象
		Student s1 = new Student("高圆圆", 27);
		Student s2 = new Student("赵又廷", 30);
		Student s3 = new Student("文章", 29);
		Student s4 = new Student("马伊琍", 39);

		list.add(s1);
		list.add(s2);
		list.add(s3);
		list.add(s4);

		// 方式1:获取迭代器的方式
		Iterator it = list.iterator();
		while (it.hasNext()) {
			Student s = (Student) it.next();

			System.out.println(s.getName() + "---" + s.getAge());
		}

		System.out.println("--------------------------");

		// 方式2:size()和get(int index)结合: 普通for循环
		for (int x = 0; x < list.size(); x++) {
			Student s = (Student) list.get(x);
			System.out.println(s.getName() + "---" + s.getAge());
		}
	}

}

ListIterator listIterator(): 列表迭代器

代码示例

package org.westos;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class IteratorDemo {

	public static void main(String[] args) {

		// 创建集合对象
		List list = new ArrayList();
		// 添加元素
		list.add("hello");
		list.add("world");
		list.add("java");
		// ListIterator listIterator():列表迭代器
		ListIterator listIterator=list.listIterator();
		// //正向遍历
		// //遍历
		while (listIterator.hasNext()) {
			String s = (String) listIterator.next();
			System.out.println(s + "---" + s.length());
		}
		System.out.println("--------------------------");
		// 逆向遍历
		// boolean hasPrevious():判断是否有上一个可以迭代的元素(逆向遍历)
		// Object previous():获取上一个元素
		while (listIterator.hasPrevious()) {
			String s = (String) listIterator.previous();
			System.out.println(s + "---" + s.length());
		}
	}
}

java.util.ConcurrentModificationException   并发修改异常,当方法检测到对象的并发修改,但是方法不允许进行此并发修改,所以抛出此异常

需求:

我有一个集合,如下List,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现
假设:List集合 的元素:"hello","world","java"

 按照正常的思路:完成了需求,发现出现异常:
 java.util.ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
 
   产生的原因:按照正常的需求,给集合添加元素,遍历,遍历同时,判断集合中是否有需要的元素,有就添加新元素,
  遍历的时候,使用迭代器遍历,但是添加元素,使用集合添加元素.
   集合添加了一个新的元素,迭代器不知道...
  
   使用迭代器遍历元素,不能使用集合添加元素!
  
  解决方案:
   方式1:要么使用迭代器遍历,使用迭代器添加元素!

  方式2:要么使用集合遍历,使用集合添加元素!

Iterator接口没有添加功能
ListIterato接口中有add(object e)方法,将指定元素添加到指定的位置

package org.westos;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ConcurrentModificationExceptionDemo {
	public static void main(String[] args) {

		// 创建一个集合对象
		List list = new ArrayList();

		// 添加元素
		list.add("hello");
		list.add("world");
		list.add("java");

		// 获取迭代器
		/*
		 * Iterator it = list.iterator() ; while(it.hasNext()) { String s =
		 * (String)it.next() ; //判断集合是否有"world"元素,有就添加一个新的元素 if("world".equals(s)) {
		 * //添加元素 list.add("javaee") ; } }
		 */

		// 方式1:使用迭代器遍历,迭代器添加元素解决并发修改异常
		// Iterator接口没有添加功能呢
		// ListIterator:列表迭代器中有add(object e):方法
		ListIterator it = list.listIterator();
		while (it.hasNext()) {
			String s = (String) it.next();
			if ("hello".equals(s)) {
				// 迭代器添加
				it.add("javaee"); // 指定元素后面插入元素
			}
			if ("world".equals(s)) {
				it.add("JavaWeb");
			}

		}

		// 方式2:使用集合遍历,使用集合添加元素(普通for循环)
		// for(int x =0 ;x < list.size() ; x ++) {
		// String s = (String)list.get(x) ;
		// //判断
		// if("world".equals(s)) {
		// list.add("javaee") ;
		// }
		// }

		System.out.println("list:" + list);

	}
}






猜你喜欢

转载自blog.csdn.net/qq_40727767/article/details/80245667