集合框架-Set集合

目录

一.Set集合的特点

                        无序、对象不能重复(唯一)

二.Set集合的遍历方式

三.Set集合常用实现类


一.Set集合的特点

                        无序、对象不能重复(唯一)

eg:

//Set
		//1.特点:无序,唯一
		//1) 无序:元素添加的顺序与输出的顺序不一致
		//2)唯一:不允许添加重复的元素(被过滤了)
		/*Set<String> set=new HashSet<>();
		set.add("zs");
		set.add("ls");
		set.add("ww");
		set.add("zs");
		set.forEach(System.out::println);*/
		
		Set<Student> set=new HashSet<>();
		set.add(new Student(1, "zs", 100f));
		set.add(new Student(2, "ls", 30f));
		set.add(new Student(3, "ww", 20f));
		set.add(new Student(1, "zs", 120f));
    

判断是过滤还是替换了:       

1)默认使用equals比较,Object类型是引用类型,默认比较的是内存地址
       2)重写hashcode和equals方法,先比较hadhcode值是否相同,在比较equals
       2.1)hashcode值不相同,则不比较equals
       2.2)hashcode值相同,则比较equals 

二.Set集合的遍历方式

                        因为Set集合获取不到下标,所有遍历的方式只有两种

1.foreach

2.迭代器

//获取迭代器
		Iterator<Student> it = set.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}

三.Set集合常用实现类

      HashSet
      TreeSet:根据某种(规则)对里面的元素进行排序
        自然比较接口: java.lang.Comparable 
        比较器: java.util.Comparator
        String以AscII码进行比较,返回差值

实例:

entity包中的Student实体类

package com.zking.Connection.entity;

import java.io.Serializable;
import java.util.Comparator;

public class Student implements Serializable,Comparable<Student>{
	private Integer sid;
	private String sname;
	private Float score;
	public Student(Integer sid, String sname, Float score) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.score = score;
	}
	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public Float getScore() {
		return score;
	}
	public void setScore(Float score) {
		this.score = score;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", score=" + score + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
//		result = prime * result + ((score == null) ? 0 : score.hashCode());
		result = prime * result + ((sid == null) ? 0 : sid.hashCode());
		result = prime * result + ((sname == null) ? 0 : sname.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		//true代表已存在 false代表允许添加
		if (this == obj)
			return true;
		//obj对象是否为空
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if(this.getSid()!=other.getSid())
			return false;
		return true;
	}
	
	@Override
	public int compareTo(Student o) {
		//比较者大于被比较者 返回1
		//比较者等于被比较者 返回0
		//比较者小于被比较者 返回-1
		return this.getSid()-o.getSid();
	}
	

}
        //TreeSet:以某种特定的规则对集合中的元素进行排序(升序和降序)
		//java.lang.Comparable:自然比较接口
		//java.util.Comparator:比较器(推荐)
		Set<String> set1=new TreeSet<>();
		set1.add("zs");
		set1.add("ls");
		set1.add("ww");
		
		Set<Student> set=new TreeSet<>();
		set.add(new Student(1, "zs", 100f));
		set.add(new Student(2, "ls", 30f));
		set.add(new Student(3, "ww", 20f));
        set.forEach(System.out::println);

自定义比较器

以名字排序,创建一个java文件,编写代码:

public class NameComparator implements Comparator<Student> {

	@Override
	public int compare(Student o1, Student o2) {
		// TODO Auto-generated method stub
		return o1.getSname().hashCode()-o2.getSname().hashCode();
	}

}

最后去调用就可以了

        //调用自定义
		Set<Student> set=new TreeSet<>(new NameComparator());
		set.add(new Student(1, "zs", 100f));
		set.add(new Student(2, "ls", 30f));
		set.add(new Student(3, "ww", 20f));
		set.forEach(System.out::println);

 总结:学习Set集合框架,同时也可以方便开发。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值