set集合

目录

1.set集合特点&遍历方式

2.set集合去重原理

3.set集合自然排序接口

4.set集合比较器排序


---------------------------------------------------实际操作------------------------------------------------------------------

1.set集合特点&遍历方式

set集合的特点:不重复:基本的数据类型加上string

public static void main(String[] args) {
	HashSet set=new HashSet<>();
	set.add("zs");
	set.add("ls");
	set.add("ww");
	set.add("ll");
	set.add("zs");
	System.out.println(set.size()); 
  

set集合_第1张图片

 

set集合的遍历方式就是fore

for (Object object : set) {
		System.out.println(object);
}
	
System.out.println("========================");
	
Iterator it = set.iterator();
	while(it.hasNext()) {
		System.out.println(it.next());
} 
  

------------------------------------------------------------------------------------------------------------------------------

2.set集合去重原理

1.set去重底层原理是与对象的hashCode以及equals相关
 2.判断重复元素的时候,是比较hashCode值,在调用equals比较内容

public static void main(String[] args) {
		HashSet set=new HashSet<>();
		set.add(new Person("zs", 12));
		set.add(new Person("ls", 11));//distrinct
		set.add(new Person("ww", 15));
		set.add(new Person("ll", 19));
		set.add(new Person("zs", 12));
		System.out.println(set.size());
	}
}
class Person /*implements Comparable*/{
	private String name;
	private int age;
	private int level;
	
	public Person(String name, int age, int level) {
		super();
		this.name = name;
		this.age = age;
		this.level = level;
	}
	int getLevel() {
		return level;
	}
	void setLevel(int level) {
		this.level = level;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Person() {
		// TODO Auto-generated constructor stub
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	@Override
	public int hashCode() {
		System.out.println("========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) {
		System.out.println("========equals========");
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) 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;
	}
	/*@Override
	public int compareTo(Person o) {
		// TODO Auto-generated method stub
		int levelRes=this.level - o.level;
		return levelRes ==0 ?this.age-o.age:levelRes;*/
//	} 
  

---------------------------------------------------------------------------------------------------------------------------------

3.set集合自然排序接口

public static void main2(String[] args) {
		/**
		 * 假设用户
		 * 
		 * 需求1:从xxx公司,拿到用户数据,需要根据用户的级别,进行会议的排序
		 * 张三:部门总监1
		 * 李四:普通员工 3
		 * 王五:部门经理
		 * 。。
		 * xxxservice.list(1)
		 * 
		 * 现象:
		 * 1.String默认是能够排序的
		 * 2.自定义的对象无法排序,报类型转换异常
		 * 
		 * 需求2
		 * 按照年龄升序,打印出人员信息
		 * 
		 * 需求3
		 * 按照用户的首字母排序 
		 */
//		报错
//		Exception in thread "main" java.lang.ClassCastException: 
//		com.hz.set.Person cannot be cast to java.lang.Comparable

		TreeSet set=new TreeSet<>();
		set.add(new Person("zs", 12,1));
		set.add(new Person("ls", 11,4));//distrinct
		set.add(new Person("ww", 15,2));
		set.add(new Person("ll", 19,3));
		set.add(new Person("bj", 14,3));
		for (Object object : set) {
			System.out.println(object);
		}

	}

}
class levelCompartor implements Comparator{

	@Override
	public int compare(Person o1, Person o2) {
		int leveRes= o1.getLevel() - o2.getLevel();
		return leveRes == 0 ? o1.getLevel() - o2.getLevel():leveRes;
	}
	
}

-------------------------------------------------------------------------------------------------------------------------------

4.set集合比较器排序

set集合排序
 java.lang.Comparable:自然排序    排序的规则是单一的,不能够应对复杂的变化的需求
 java.util.comparator:比较器排序

/**
		 * 假设客户
		 * 
		 * 需求1:从xxx公司,拿到用户数据,需要根据用户的级别,进行会议的排序
		 * 
		 * 需求2
		 * 按照年龄升序,打印出人员信息
		 * 
		 * 需求3
		 * 按照用户的首字母排序 
		 */ 
//		报错
//		Exception in thread "main" java.lang.ClassCastException: 
//		com.hz.set.Person cannot be cast to java.lang.Comparable
//		TreeSet  set=new TreeSet<>(new levelCompartor());
//		TreeSet  set=new TreeSet<>((x,y)-> x.getName().compareTo(y.getName()));
		TreeSet  set=new TreeSet<>((x,y)-> {
			int ageRes=x.getAge() - y.getAge();
			int levelRes=0;
			if(ageRes==0) {
				levelRes =x.getLevel()-y.getLevel();
			}else {
				return ageRes;
			}
			return  levelRes;
		});
		set.add(new Person("zs", 12,1));
		set.add(new Person("ls", 11,4));//distrinct
		set.add(new Person("ww", 15,2));
		set.add(new Person("ll", 19,3));
		set.add(new Person("bj", 14,3));
		for (Object object : set) {
			System.out.println(object);
		}

	}

你可能感兴趣的:(蓝桥杯,职场和发展)