Java开发必备点14_集合

集合

昨日回顾

包装类
	byte---Byte
	short---Short
	int---Integer
    long---Long
    float---Float
    double---Double
    char---Character
    boolean---Boolean
    
Integer
    	int---Integer
    	Integer---Stringint----String
    	
    	自动装箱:简单类型----包装类型
    	自动拆箱:包装类型----简单类型
    反转方法
包装类的应用
	区别分数中  0  和 缺考的同学

String类
	创建字符串  
		1String s = "ahkjghkla"; 将字符串放进串池共享
		2String s = new String("jgsikgj"); 创建了两个对象  一个在堆中new  一个在串池
	可变字符串
	StringBuffer:线程安全 效率低
	StringBuilder:线程不安全 效率高
	
	方法:
	String string = "1239586838923173478943890234092";
		
	boolean b = string.startsWith("123");
	System.out.println(b);
大数据运算:解决浮点类型数据 计算不精确问题

	加减乘除 的方法

集合的由来

存储数据的容器
变量  只能存储一个值 不能进行批量的处理
数组  存放多个相同类型的数据 内存是连续的  一旦定义容量就不可变 
集合 

集合的概念

概念:就是通过一些数据结构封装成的一些类,具有长度可变 可以存放任意数据的容器

学习集合
	特点
	方法上 
	遍历

集合的结构

单列集合
	Collection接口 是单列集合的根接口
		----List子接口
		----Set子接口
双列集合
	Map 

Collection根接口

特点:存储的数据类型是对象类型,是单列集合的根接口,没有实现类,其下有两个子接口ListSet,所以在Collection中定义的方法在它的子接口中也存在

方法:
- public boolean add(E e):  把给定的对象添加到当前集合中 。
- public boolean add(Colllection c):将集合c中的元素添加到当前集合中。
- public void clear() :清空集合中所有的元素。
- public boolean remove(E e): 把给定的对象在当前集合中删除。
- public boolean removeAll(Colllection c): 从当前集合中删除c集合中的元素。
- public boolean contains(E e): 判断当前集合中是否包含给定的对象。
- public boolean containsAll(Colllection c): 判断c是否是当前集合的子集。
- public boolean isEmpty(): 判断当前集合是否为空。
- public int size(): 返回集合中元素的个数。
- public Object[] toArray(): 把集合中的元素,存储到数组中。

List子接口

List集合的特点
List集合中存放的数据都为Object类型
特点:
	1)有序
	2)可以重复
	3)可以索引(通过下标可以访问元素)
List集合的实现类
ArrayList: 底层数组实现       线程不安全   效率高  
Vector:jdk1.0  数组实现的   但是线程安全 效率低
LinkedList:底层双向链表实现



案例1:从根接口中继承并实现的方法
package cn.baizhi.day15;

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


public class Demo {
	public static void main(String[] args) {
		List list1 = new ArrayList();
		//add()
		list1.add("桑邦豪");
		list1.add("霍冠霖");
		list1.add("古遇");
		list1.add("齐美君");
		list1.add("刘洋");
		
		Object[] objects = list1.toArray();
		//遍历数组
		for (int i = 0; i < objects.length; i++) {
			System.out.print(objects[i]+"  ");
		}
		/*List list2 = new ArrayList();
		list2.add("桑邦豪");
		list2.add("霍冠霖");
		list2.add("梁帅");
		boolean b = list1.containsAll(list2);
		System.out.println(b);
		list2.clear();
		System.out.println(list2.isEmpty());*/
		//list.remove("霍冠霖");
		//list.removeAll(list);
		//list.clear();
		/*boolean b = list.contains("霍冠霖");
		System.out.println(b);
		System.out.println(list.size());*/
	}	
}

案例2: 用索引操作数组的方法
add(index,元素)   将指定的元素插入到指定下标的位置
addAll(index,集合)  将指定的集合插入到另一个集合的指定下标位置
get(index)   获取指定下标的元素
remove(index) 移除指定下标位置的元素
set(index,元素)  将指定下标的元素替换为其他的元素

代码:
package cn.baizhi.day15;

import java.util.ArrayList;

public class Demo {
	public static void main(String[] args) {
		ArrayList arrayList1 = new ArrayList<>();
		
		//在集合中添加一些元素
		arrayList1.add("苍井");
		arrayList1.add("范冰冰");
		arrayList1.add("小泽");
		arrayList1.add("桑邦豪");
		arrayList1.add("霍冠霖");
		
		arrayList1.add(1, "古煜");
		arrayList1.set(4, "许甲楠");
		//arrayList1.remove(4);
		/*ArrayList arrayList2 = new ArrayList();
		arrayList2.add("小波");
		arrayList2.add("孙米洛");
		arrayList2.add("高健");
		arrayList2.add("许甲楠");
		arrayList2.add("肖肃羽");
		
		arrayList1.addAll(2,arrayList2);*/
		
		//遍历
		for (int i = 0; i < arrayList1.size(); i++) {
			System.out.print(arrayList1.get(i)+"  ");
		}
	}	
}


List集合的遍历
1.通过for循环遍历  
案例:
		package cn.baizhi.day15;

import java.util.ArrayList;

public class Demo {
	public static void main(String[] args) {
		ArrayList arrayList1 = new ArrayList<>();
		
		//在集合中添加一些元素
		arrayList1.add("苍井");
		arrayList1.add("范冰冰");
		arrayList1.add("小泽");
		arrayList1.add("桑邦豪");
		arrayList1.add("霍冠霖");
		
		//遍历
		for (int i = 0; i < arrayList1.size(); i++) {
			System.out.print(arrayList1.get(i)+"  ");
		}
		
		
	}	
}


注意:
	length属性是 数组
	length()是字符串的
	size() 是集合的、

2.foreach遍历:和for无关  底层是用迭代器实现的
语法:
for(集合中元素的类型引用 : 集合的名字)System.out.print(引用);
}
案例:
package cn.baizhi.day15;

import java.util.ArrayList;

public class Demo {
	public static void main(String[] args) {
		ArrayList arrayList1 = new ArrayList<>();
		
		//在集合中添加一些元素
		arrayList1.add("苍井");
		arrayList1.add("范冰冰");
		arrayList1.add("小泽");
		arrayList1.add("桑邦豪");
		arrayList1.add("霍冠霖");
		
		//遍历
		/*for (int i = 0; i < arrayList1.size(); i++) {
			System.out.print(arrayList1.get(i)+"  ");
		}*/
		
		//foreach遍历
		
		for (Object object : arrayList1) {
			System.out.println(object);
		}
	}	
}


List接口的其他实现类
ArrayList 集合:底层用数组实现 查询快  增删慢
LinkedList 集合:用双向链表实现 链表的机构在内存中是分散的 查询慢 但是增删快
Vector:线程安全 和ArrayList一样都是用数组实现 
根据视频看原理:
LinkedList:当通过下标去查找一个元素的时候 会进行一个加速操作 要查找的下标和集合的size的一半进行对比 如果小于size/2 从头查找 反之从尾查找(了解)

ArrayListLinkedList效率的对比

案例:证明插入速度
package cn.baizhi.day15;

import java.util.ArrayList;
import java.util.LinkedList;

public class Demo {
	public static void main(String[] args) {
		// 1 分别创建集合
		ArrayList a1 = new ArrayList();
		LinkedList l1 = new LinkedList();
		// 2 先在每个集合中放入5个元素
		for (int i = 0; i < 5; i++) {
			a1.add(i);
			l1.add(i);
		}
		// 3 先测试插入元素 在下标为1的位置插入1000000 个元素 计算各自的时间
		// a1
		// 时间
		long start1 = System.nanoTime();
		for (int i = 0; i < 10000; i++) {
			a1.add(1, i);
		}
		long end1 = System.nanoTime();
		System.out.println("ArrayList中插入1000个数据所用时间:" + (end1 - start1));

		// l1
		long start2 = System.nanoTime();
		for (int i = 0; i < 10000; i++) {
			l1.add(1, i);
		}
		long end2 = System.nanoTime();
		System.out.println("LinkedList中插入1000个数据所用时间:" + (end2 - start2));
	}
}

案例:证明查找元素的速度
package cn.baizhi.day15;

import java.util.ArrayList;
import java.util.LinkedList;

public class Demo {
	public static void main(String[] args) {
		// 1 分别创建集合
		ArrayList a1 = new ArrayList();
		LinkedList l1 = new LinkedList();
		// 2 先在每个集合中放入5个元素
		for (int i = 0; i < 5; i++) {
			a1.add(i);
			l1.add(i);
		}
		// 3 先测试插入元素 在下标为1的位置插入1000000 个元素 计算各自的时间
		// a1
		// 时间
		
		for (int i = 0; i < 100000; i++) {
			a1.add(1, i);
		}
		long start1 = System.nanoTime();
		//查找
		System.out.println(a1.get(50000));
		long end1 = System.nanoTime();
		System.out.println("ArrayList中查找下标为5000数据所用时间:" + (end1 - start1));

		// l1
		
		for (int i = 0; i < 100000; i++) {
			l1.add(1, i);
		}
		long start2 = System.nanoTime();
		System.out.println(l1.get(50000));
		long end2 = System.nanoTime();
		System.out.println("LinkedList中查找下标为5000的数据所用时间:" + (end2 - start2));
	}
}


如果一个集合增删 查询都比较频繁  优先选用ArrayList

泛型

泛型用于解决一个集合中添加数据时会出现非法的数据 导致在运行一些代码时出现类型转换异常的问题,有了泛型就可以限定一个集合的数据类型 一个接口 一个方法 的参数类型
案例:
package cn.baizhi.day15;

import java.util.ArrayList;


public class Demo {
	public static void main(String[] args) {
		ArrayList<Student> students = new ArrayList<Student>();
		
		//在集合中添加元素
		students.add(new Student("桑邦豪",45 ));
		students.add(new Student("苍老师",18 ));
		students.add(new Student("霍冠霖",250 ));
		students.add(new Student("许甲楠",120 ));
		students.add(new Student("古煜",290 ));
		students.add(new Student("肖肃羽",180 ));
		students.add(new Student("刘洋",17 ));
		students.add(new Student("小吉",34 ));
		students.add(new Student("小泽",45 ));
		
		//脑子进水的人  霍冠霖 
		//students.add("樊黎明");//错误
		
		
		//遍历
		for (Object object : students) {
			//向下转型 学生对象的 name 和 age
			Student student = (Student)object;
			System.out.println(student.name+"---"+student.age);
		}
	}
}

class Student{
	String name;
	int age;
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Student() {
		super();
	}
	
	
}

泛型集合
案例:
package cn.baizhi.day15;

import java.util.ArrayList;




public class Demo {
	public static void main(String[] args) {
		//定义一个集合的同时 可以限定集合中元素的数据类型
		ArrayList<Student> arrayList = new ArrayList<>();
		
		arrayList.add(new Student());
		
	}
}

class Student{
	String name;
	int age;
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Student() {
		super();
	}
	
	
}

泛型类(了解)
好处:当一个类用泛型  可以实现一些方法的通用性 
案例:
package cn.baizhi.day15;
public class Demo {
	public static void main(String[] args) {
		MyClass<String> myClass = new MyClass<>();
		MyClass<Student> myClass2 = new MyClass<>();
		
	}
}

class MyClass<T>{
	
	public void m(T t) {
		
	}
	
	
}

class Student{
	
}
泛型接口(了解)
好处:可以实现模版化编程
案例:
package cn.baizhi.day15;

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

interface IA<T>{
	void m1(T t);
	T m2();
}

class A implements IA<Student>{

	@Override
	public void m1(Student t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Student m2() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
class Student{
	
}

Collections工具类

数组---Arrays    sort()

集合中----Collections 工具类

案例:
package cn.baizhi.day15;

import java.util.ArrayList;
import java.util.Collections;


public class Demo {
	public static void main(String[] args) {
		ArrayList<Student> students = new ArrayList<>();
		//添加学生
		students.add(new Student("许甲女", 33));
		students.add(new Student("古煜",55 ));
		students.add(new Student("火罐林",45 ));
		students.add(new Student("刘洋",17 ));
		students.add(new Student("桑邦豪",20 ));
		students.add(new Student("刘源",17 ));
		//反转
		Collections.reverse(students);
		//随机显示
		Collections.shuffle(students);
		//排序  
		Collections.sort(students);
		
		//遍历
		for (Student student : students) {
			System.out.println(student);
		}
	}
}

class Student implements Comparable<Student>{
	private String name;
	private int age;
	public Student() {
		super();
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public int compareTo(Student s) {
		
		//排序的规则
		return this.age-s.age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	
}













		
		//遍历
		for (Student student : students) {
			System.out.println(student);
		}
	}
}

class Student implements Comparable<Student>{
	private String name;
	private int age;
	public Student() {
		super();
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public int compareTo(Student s) {
		
		//排序的规则
		return this.age-s.age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值