Java程序猿必学第十五篇—— Set集合

//1.Collections工具类
主要是为List集合服务,进行相关的List功能处理

//注意: Collection与Collections要区分开

//扩展了解复合泛型
//<T extends Comparable<T>>: 所使用的T类型必须要实现Comparable
//<T extends Comparable<? super T>>:所使用的T类型或T的父亲实现Comparable接口
//例如:此处List存Integer类型,定义复合类型说明Integer或父亲必须实现Comparable

class Student{
	int age;
	public Student(int age) {
		this.age=age;
	}
	
}
public class Test1 {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<>();
		list.add(3);
		list.add(1);
		list.add(2);
		list.add(4);
		Collections.reverse(list);  //集合的反转
		Collections.shuffle(list);  //随机重置集合
		System.out.println(list);
		Collections.sort(list);     //排序
		System.out.println(list);
		
		
		//------扩展思考:List存自定义对象,并进行排序------
		List<Student> list2 = new ArrayList<Student>();
		list2.add(new Student(30));
		list2.add(new Student(25));
		//Collections.sort(list2);  //有约束,Student类必须实现Comparable接口才行
	}
}



//2.Set实现类HashSet(重点)

//2.1. HashSet存储原理
//HashSet实现类: 是Set接口的实现类
//特点:无序,无下标,元素唯一
//使用方式:

//分析HashSet存储原理:通过hash算法进行存储

public class Test1 {
	public static void main(String[] args) {
		Set<Integer> set = new HashSet<>();
		set.add(11);
		set.add(33);
		set.add(22);
		set.add(33);  //唯一的
		System.out.println(set);
		
		
		//因为无下标,所以不能使用基本for遍历
		for(int i=0;i<set.size();i++) {}
		
		for(Integer o:set) {
			System.out.println(o);
		}
	}
}

 




 

//2.2 hashSet验证原理

//验证存储原理:
//HashSet存储自定义对象student,所有对象如何确定唯一性
//例如:      学生对象zs    ls     zs  
//目标: 2个        ?

//思考:假设两个属性的自定义对象存储;

//===================存储单个属性的对象===================

class Student{
	String name;
	public Student(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
}
public class Test2 {
	public static void main(String[] args) {
		Set<Student> set = new HashSet<Student>();
		set.add(new Student("zs"));
		set.add(new Student("ls"));
		set.add(new Student("zs"));
		System.out.println(set);  
		
		//为什么下面的new String,它会根据属性的一致确定唯一性?
		//根据存储原理分析结论得出:
		//只要源码中的hashCode和equals一致,则确定唯一性,可以得出String类型肯定重写过
		//如果存储自定义对象,要想根据属性确定唯一性,需要重写hashCode与equals
		
		Set<String> set2 = new HashSet<String>();
		set2.add(new String("zs"));
		set2.add(new String("ls"));
		set2.add(new String("zs"));
		System.out.println(set2);
	}
}


//===================存储多个属性的对象===================
//思考:假设两个属性的自定义对象存储;
class Teacher{

	String name;
	int    age;
	
	public Teacher(String name, int age) {
		this.name = name;
		this.age  = age;
	}

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

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Teacher other = (Teacher) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
public class Test3 {
	public static void main(String[] args) {
		Set<Teacher> set = new HashSet<Teacher>();
		set.add(new Teacher("laoLi",28));
		set.add(new Teacher("laoHou",29));
		set.add(new Teacher("laoLi",28));
		System.out.println(set);
	}
}


//3.Set实现类LinkedHashSet

//3.1 LinkedHashSet使用特点
//LinkedHashSet: 是Set接口的实现类,也是HashSet的子类
              //存值判断唯一性,交给父类完成的

//使用特点: 有序,唯一

public class Test1 {
	public static void main(String[] args) {
		Set<Integer> set = new LinkedHashSet<Integer>();
		set.add(1);
		set.add(3);
		set.add(2);
		set.add(3);
		System.out.println(set);
	}
}


//3.1 集合的简单源码分析案例

class Super{
	public void add(int a) {
		insertsort();
	}

	public void insertsort() {
		System.out.println("调用的是自己的...");
	}
}

class Son extends Super{
	@Override
	public void insertsort() {
		System.out.println("调用子类的方法..");
	}
}

public class Test2 {
	public static void main(String[] args) {
		//new Super().add(1);
		new Son().add(3);
	}
}


//4. Set实现类TreeSet(重点)

//4.1 TreeSet的使用特点
//TreeSet:是Set接口的实现类
//TreeSet存储特点:可排序,唯一,无下标

//TreeSet的存储原理:通过二叉树进行存储
//细节
public class Test1 {
	public static void main(String[] args) {
		Set<Integer> set = new TreeSet<Integer>();
		set.add(11);
		set.add(33);
		set.add(22);
		set.add(11);
		System.out.println(set);
		
		//基本for:  不能
		for(int i=0;i<set.size();i++) {}
		
		//增强for
		for(Integer o:set) {
			System.out.println(o);
		}
		
		
	}
}

猜你喜欢

转载自blog.csdn.net/m0_62718093/article/details/121042079
今日推荐