OOP篇 05集合

1.集合能够对数据进行增加删除修改查询的操作
2.集合能够存储引用类型,如果是基本类型可以是包装类类型
3.集合的长度是可变的
--------------------数据结构------------------------
4.部分集合是有序的,部分集合是无序的 (这里的有序指的是存储有序,并不是排序)
5.部分集合是唯一的,部分集合是可重复 (11, 22 ,33, 33, 22)
6.部分集合是可排序的,部分集合是不可排序的    33 44 55 11 -> 11 33 44 55
7.部分集合是线程安全的,部分集合是线程不安全  (synchronized)
集合应该设计成一个类,还是一个框架?
集合框架应该设计成一个接口,不同的集合的实现不一样,那么效率不一样,
特点不一样,我们可以自由选取
数据结构: 数据的存储方式。
常见的和集合相关的数据结构: 数组,栈,队列,链表,哈希表,二叉树
存储方式不一样决定了该集合的性能效率不一样
3 集合的功能
1.增加功能
boolean add(E e)
boolean addAll(Collection<? extends E> c)
2.删除功能
void clear()
boolean remove(Object o)
boolean removeAll(Collection<?> c)
3.修改功能
Iterator iterator()
Object[] toArray()
4.查询功能
Iterator iterator()
Object[] toArray()
 T[] toArray(T[] a)
5.获取功能
int size()
6.判断功能
boolean contains(Object o)
boolean containsAll(Collection<?> c)
boolean isEmpty()
7.其他功能
boolean retainAll(Collection<?> c)
返回原集合是否发生改变
改变了返回true
没改变返回false

public class CollectionDemo01 {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		c.add("张三");
		c.add("李四");
		c.add("王五");
		c.add("赵六");
		System.out.println(c); // [张三, 李四, 王五, 赵六]
		
		// boolean addAll(Collection c)
		Collection c2 = new ArrayList(); 
		c2.add("曹操");
		c2.add("萨达姆");
		c2.add("本拉登");
		c.addAll(c2);
		
		System.out.println(c);
		
		// boolean remove(Object o) 
		System.out.println("remove: " + c.remove("张三"));
		System.out.println(c);
		// boolean removeAll(Collection<?> c) 
//		System.out.println("removeAll: " + c.removeAll(c2));
//		System.out.println(c);
		
		// void clear() 
//		c.clear();
//		System.out.println(c);
		
		System.out.println(c.size());
		
		System.out.println("contains: " + c.contains("李四"));
		System.out.println("contains: " + c.contains("曹操"));
		System.out.println("contains: " + c.contains(""));
		System.out.println("containsAll: " + c.containsAll(c2));
		System.out.println("isEmpty: " + c.isEmpty());
		
		Collection c3 = new ArrayList();
//		c3.add("张三");
//		c3.add("李四");
//		c3.add("王五");
//		c3.add("赵六");
//		c3.add("曹操");
//		c3.add("萨达姆");
//		c3.add("本拉登");
//		c3.add("秦始皇");
		System.out.println(c);
		System.out.println(c3);
		System.out.println("retainAll:" + c.retainAll(c3));
		System.out.println("c:" + c);
		
	}

}

4 集合的遍历
1    iterator 迭代器
查询功能

查询功能
Iterator<E> iterator() 
 	Object[] toArray() 
遍历 	
 	while (it.hasNext()) {
		Object oj = it.next();
		System.out.println(oj);
	}

 

public class CollectionDemo02 {
	public static void main(String[] args) {
		// Object[] toArray() 
		Collection c = new ArrayList();
		c.add("希特勒");
		c.add("杨贵妃");
		c.add("貂蝉");
		c.add("赛西施");
//		c.add(c);
//		c.add(100);
		System.out.println(c);
//		Object[] objs = c.toArray();
//		for (Object oj : objs) {
//			String s = (String) oj;
//			System.out.println(s);
//		}
		
//		Iterator<E> iterator() 
//		获取迭代器对象
		Iterator it = c.iterator();
		
//		Object oj = it.next();
//		System.out.println(oj);
//		
//		oj = it.next();
//		System.out.println(oj);
//		
//		oj = it.next();
//		System.out.println(oj);
//		
//		oj = it.next();
//		System.out.println(oj);
//		
//		oj = it.next();
//		System.out.println(oj);
		
		while (it.hasNext()) {
			Object oj = it.next();
			System.out.println(oj);
		}
		
	}
}


2  并发修改异常
java.util.ConcurrentModificationException
异常名称:
 并发修改异常
1
产生原因:
表示在使用迭代器的同时,使用原集合修改了元素
1
解决办法:

		1.只操作原集合,使用原集合修改
			toArray
			普通for  如下
			Object[] objs = c.toArray();
			for (Object oj : objs) {
			String s = (String) oj;
		
		2.只操作迭代器修改
			使用ListIterator


注意:
    foreach遍历集合底层也是使用了迭代器不能够解决并发修改异常

public class CollectionDemo03 {
   public static void main(String[] args) {
   	Collection c = new ArrayList();
   	c.add("希特勒");
   	c.add("杨贵妃");
   	c.add("貂蝉");
   	c.add("赛西施");
   	
//		Iterator it = c.iterator();
//		while (it.hasNext()) {
//			Object oj = it.next();
//			String s = (String) oj;
//			if (s.equals("杨贵妃")) {
//				c.remove("杨贵妃");
//			}
//		}
//		Object[] objs = c.toArray();
//		for (Object oj : objs) {
//			String s = (String) oj;
//			if (s.equals("杨贵妃")) {
//				c.remove("杨贵妃");
//			}
//		}
   	
//		for (Object oj : c) {
//			String s = (String) oj;
//			if (s.equals("杨贵妃")) {
//				c.remove("杨贵妃");
//			}
//		}
   	
//		for (Iterator iterator = c.iterator(); iterator.hasNext();)
//		{
//			Object oj = iterator.next();
//			String s = (String)oj;
//			if (s.equals("杨贵妃"))
//				c.remove("杨贵妃");
//		}
   	
//		for(Iterator iterator = c.iterator();iterator.hasNext();) System.out.println(iterator.next());
   		
   }
}

 


2 泛型
1 泛型的引入
问题一: 安全隐患问题,如果集合存储的是任意类型,那么我们需要对Object的所有子类做判断,显然安全隐患永远存在
问题一: 就算我们判断了类型,如果新增一个不同类型的元素,那么我们不能遍历完全

没有直接解决方案
模仿数组和方法

1.数组在编译的时候就必须确定类型,如果确定了类型再存储不同类型会编译报错,不存在类型转换问题
2.参数化类型
12345678
2 泛型的概念:
泛型属于一种独立的技术,泛型是JDK1.5之后引入的新特性,是一种将元素的数据类型在编译的时候  就确定的类型,
同时是一种参数化类型,一旦确定类型,泛型相关的类,接口,方法所有的类型都会被统一
3 泛型的格式
<E,H,T,K,V>
1. <>里面可以是任意的字母,一般泛型类会使用E,泛型方法会使用T
2. 这里只能够定义引用类型,不能够定义基本书类型
3. <>里面既可以定义一个泛型,也可以定义多个泛型
4  泛型的分类
泛型类
泛型接口
泛型方法
5 泛型的好处:
1. 简化了代码
2. 取消了黄色警告线
3. 取消了强制类型转换,提高了程序的效率
4. 提高了程序的安全性
5. 提高了程序的扩展性和可维护性,满足了开闭原则【对扩展开放,对修改关闭】
二.泛型类
把泛型定义在类上
泛型接口或者泛型类在使用的时候必须确定类型
代码如下:
 

public class GernericDemo02 {
	public static void main(String[] args) {
		GenericClass gc = new GenericClass();
		gc.setObj("张三");
		
		Object oj = gc.getObj();
		String s = (String) oj;
		System.out.println(s + "|" + s.length());
	}
}

class GenericClass {
	private Object obj;

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}
	
}


3泛型接口
把泛型定义在接口上
1.泛型接口实现类的方式
2.泛型接口匿名内部类的方式

泛型接口或者泛型类在使用的时候必须确定类型
泛型接口代码如下:

interface GenericInterface<E, T> {
	
	void test(E e);
	
	T add(T t);
}


泛型接口的使用方式有如下三种:

实现类确定泛型类型

   // 1.实现类确定泛型类型
   class GenericInterfaceImpl implements GenericInterface<String, Integer> {
   
   	@Override
   	public void test(String e) {
   		System.out.println(e);
   	}
   
   	@Override
   	public Integer add(Integer t) {
   		return t;
   	}
   	
   }

实现类不确定泛型,在调用的时候确定泛型

   // 1 实现类不确定泛型

   // 1.实现类确定泛型类型
   class GenericInterfaceImpl: GenericInterface<String, Integer> {
   
   	@Override
   	public void test(String e) {
   		System.out.println(e);
   	}
   
   	@Override
   	public Integer add(Integer t) {
   		return t;
   	}
   	
   }


   // 2 在调用的时候确定泛型

   // 1 实现类不确定泛型
   class GenericInterfaceImpl<E,T>:GenericInterface<E, T> {
   
   	@Override
   	public void test(E e) {
   		System.out.println(e);
   	}
   
   	@Override
   	public T add(T t) {
   		return t;
   	}
   	
   }


4 泛型方法
泛型方法: 把泛型定义在方法上,泛型方法可以理解为局部泛型,独立于泛型类
泛型方法的特点:

泛型方法独立于泛型类或者泛型接口
泛型方法在方法调用的时候确定类型
一个泛型接口或者泛型类中可以有多个泛型方法
一个泛型方法也可以定义多个泛型泛型方法示例代码如下:

public class GenericDemo04 {
	public static void main(String[] args) {
		GenericMethod<String, Integer> gm = new GenericMethod<String, Integer>();
        // 2.泛型方法在方法调用的时候确定类型
		gm.show(20.5);
		
		Character c = gm.test('c');
		System.out.println(c);
		
		gm.method(25, 2.5);
	}
}

class GenericMethod<E,H> {
	
	private E e;
	private H h;
	
	// 1.泛型方法独立于泛型类或者泛型接口
	public <T> void show(T t) {
		System.out.println(t);
	}
	// 3.一个泛型接口或者泛型类中可以有多个泛型方法
	public <K> K test(K K) {
		return K;
	}
	// 4.一个泛型方法也可以定义多个泛型
	public <V, U> void method(V v, U u) {
		System.out.println(v);
		System.out.println(u);
	}
	
	public E getE() {
		return e;
	}
	public void setE(E e) {
		this.e = e;
	}
	public H getH() {
		return h;
	}
	public void setH(H h) {
		this.h = h;
	}
	
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值