19. java之set集合

1.Set接口:存储无序,唯一的单一对象.

2.HashSet:存储无序,唯一的单一对象.底层采用HashMap的Key存值.

注意:HashSet集合根据值去重,它的泛型类型中一定要重写hashCode()和equals()方法,
	除非泛型类型是系统数据类型,我们不用写,底层已经重写了.
唯一性(去重):通过hashCode()和equals()方法来保证.
优点:去除重复元素.

在这里插入图片描述

	eg:public static void main(String[] args) {
		//创建集合对象
		HashSet<Integer> set1=new HashSet<>();
		//向集合中添加元素
		set1.add(66);
		set1.add(88);
		set1.add(55);
		set1.add(77);
		set1.add(66);
		
		//遍历集合
		for (Integer s : set1) {
			System.out.println(s);
		}
		
		System.out.println("----------------------------------------");
		//删除集合中元素
		set1.remove(55);
		
		//遍历集合
		//获得集合迭代器
		Iterator<Integer> it1=set1.iterator();
		//边判断边迭代
		while (it1.hasNext()) {
			Integer num1=it1.next();
			System.out.println(num1);
		}
	}

   //Student类如果不重写hashCode()和equals()方法,根据内存地址去重和比较;
	如果重写hashCode()和equals()方法,根据对象属性值来去重和比较
   public static void main(String[] args) {
		//创建集合对象
		HashSet<Student> set1=new HashSet<>();
		//向集合中添加元素
		set1.add(new Student("张三", 66));
		set1.add(new Student("王二麻子", 88));
		set1.add(new Student("李四", 55));
		set1.add(new Student("王二麻子", 88));
		
		//遍历集合
		for (Student s : set1) {
			System.out.println(s);
		}
		
		System.out.println("----------------------------------------");
		//删除集合中元素
		set1.remove(new Student("李四", 55));
		
		//遍历集合
		//获得集合迭代器
		Iterator<Student> it1=set1.iterator();
		//边判断边迭代
		while (it1.hasNext()) {
			Student num1=it1.next();
			System.out.println(num1);
		}
	}

3.LinkedHashSet:存储有序,唯一的单一对象.底层采用哈希表和链表结构存值.

eg:public static void main(String[] args) {
	//创建集合对象
	HashSet<Student> set1=new LinkedHashSet<>();
	//向集合中添加元素
	set1.add(new Student("张三", 66));
	set1.add(new Student("王二麻子", 88));
	set1.add(new Student("李四", 55));
	set1.add(new Student("王二麻子", 88));
	
	//遍历集合
	for (Student s : set1) {
		System.out.println(s);
	}
	
	System.out.println("----------------------------------------");
	//删除集合中元素
	set1.remove(new Student("李四", 55));
	
	//遍历集合
	//获得集合迭代器
	Iterator<Student> it1=set1.iterator();
	//边判断边迭代
	while (it1.hasNext()) {
		Student num1=it1.next();
		System.out.println(num1);
	}
}

4.TreeSet:存储无序可排序的唯一的单一对象.底层采用TreeMap的key存值.

注意:TreeSet一定要排序器,如果调用TreeSet无参构造,默认用自然排序器
如果想用自定义排序,TreeSet构造方法中必须传自定义排序器对象作为参数.
优点:可排序, 去重.
可排序性:通过排序器返回负数排在前面,返回正数排在后面.
去重:通过排序器返回0,来去重.

TreeMap存值原理:添加Key-value对时,看二叉树根节点是否为null,如果为null,当前Key-value就作为根节点;如果根节点不为空,取出当前Key-value的Key调用排序器的排序方法与根节点比较,返回正数,负数和0. 如果返回0,就表示当前节点与根节点相同,Key不存,value覆盖.如果返回负数(-1),就看根节点有无左子节点,如果根节点无左子节点,当前Key-value就存在根节点的左子节点处,如果有左子节点,将当前Key与左子节点继续,直到找到存值处;如果返回正数(1),就看根节点有无右子节点,如果无,直接存在右子节点处,如果有就与右子节点继续比较,直到找到存值的位置.

在这里插入图片描述

	eg:public static void main(String[] args) {
		//创建集合对象
		TreeSet<Integer> tset1=new TreeSet<>();
		//向添加中添加元素
		tset1.add(66);
		tset1.add(44);
		tset1.add(88);
		tset1.add(11);
		tset1.add(55);
		tset1.add(77);
		tset1.add(99);
		tset1.add(88);
		
		//遍历集合
		for (Integer t : tset1) {
			System.out.println(t);
		}
		
		System.out.println("************************");
		//删除集合中元素
		tset1.remove(44);
		
		//遍历集合
		//获得集合迭代器对象
		Iterator<Integer> it1=tset1.iterator();
		//每判断一次跌代一次
		while (it1.hasNext()) {
			Integer i=it1.next();
			System.out.println(i);
		}
	}

5.排序器

5.1:自然排序器(Comparable)
		eg:public class Teacher implements Comparable<Teacher>{
	/**
	 * 自然排序器的排序方法
	 * @param o代表的是从根节点开始每个要比较节点对象,this指要添加的元素
	 * @return int 返回0表示是相同元素不存;返回负数表示排在前面;返回正数表示排在后面
	 * 排序规则:根据姓名按字典顺序来排序,姓名相同,按年龄由大到小
	 */
	@Override
	public int compareTo(Teacher o) {
		if (this.tname.compareTo(o.tname)>0) {
			return 1;
		}else if (this.tname.compareTo(o.tname)<0) {
			return -1;
		}else {//姓名相同,按年龄比
			if (this.tage>o.tage) {
				return -1;
			}else if(this.tage<o.tage) {
				return 1;
			}else {//姓名年龄相同
				return 0;
			}
		}
	}
	
	public String tname;
	public Integer tage;
	
	public Teacher() {
		
	}
	
	public Teacher(String tname, Integer tage) {
		super();
		this.tname = tname;
		this.tage = tage;
	}

	@Override
	public String toString() {
		return "Teacher [tname=" + tname + ", tage=" + tage + "]";
	}
}

public static void main(String[] args) {
		//创建集合对象
		TreeSet<Teacher> tset1=new TreeSet<>();
		//向添加中添加元素
		tset1.add(new Teacher("hh", 66));
		tset1.add(new Teacher("dd", 44));
		tset1.add(new Teacher("tt", 88));
		tset1.add(new Teacher("aa", 34));
		tset1.add(new Teacher("kk", 54));
		tset1.add(new Teacher("dd", 54));
		
		//遍历集合
		for (Teacher t : tset1) {
			System.out.println(t);
		}
		
		System.out.println("************************");
		//删除集合中元素
		tset1.remove(new Teacher("tt", 88));
		
		//遍历集合
		//获得集合迭代器对象
		Iterator<Teacher> it1=tset1.iterator();
		//每判断一次跌代一次
		while (it1.hasNext()) {
			Teacher i=it1.next();
			System.out.println(i);
		}
	}

	5.2:自定义排序器(Comparator)
		eg:public class MyComparator implements Comparator<Teacher3>{
	/**
	 * 自定义排序器的排序方法
	 * @param o1代表当前对象
	 * @param o2代表从根节点开始每个要比较节点对象
	 * @return int 返回0表示是相同元素不存;返回负数(-1)表示排在前面;返回正数(1)表示排在后面
	 * 排序规则:根据姓名按字典顺序来排序,姓名相同,按年龄由大到小
	 */
	@Override
	public int compare(Teacher3 o1, Teacher3 o2) {
		if (o1.tname.compareTo(o2.tname)!=0) {
			return o1.tname.compareTo(o2.tname);
		}else {//姓名相同,按年龄由大到小
			if (o1.tage.compareTo(o2.tage)!=0) {
				return -o1.tage.compareTo(o2.tage);
			}else {//姓名年龄相同
				return 0;
			}
		}
	}
}

		public static void main(String[] args) {
		//创建自定义排序器对象
		MyComparator mc=new MyComparator();
		//创建集合对象,将自定义排序器对象作为TreeSet构造方法参数
		TreeSet<Teacher3> tset1=new TreeSet<>(mc);
		//向添加中添加元素
		tset1.add(new Teacher3("hh", 66));
		tset1.add(new Teacher3("dd", 44));
		tset1.add(new Teacher3("tt", 88));
		tset1.add(new Teacher3("aa", 34));
		tset1.add(new Teacher3("kk", 54));
		tset1.add(new Teacher3("dd", 54));
		
		//遍历集合
		for (Teacher3 t : tset1) {
			System.out.println(t);
		}
		
		System.out.println("************************");
		//删除集合中元素
		tset1.remove(new Teacher3("tt", 88));
		
		//遍历集合
		//获得集合迭代器对象
		Iterator<Teacher3> it1=tset1.iterator();
		//每判断一次跌代一次
		while (it1.hasNext()) {
			Teacher3 i=it1.next();
			System.out.println(i);
		}
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值