set集合

set集合也是用来村塾数据

set集合父接口 Collection接口

set存储数据的时候的效果是无序的 不可重复

set接口下面又两个实现类:

        HashSet :底层是hash值进行存储的。如果hash值一样的的就无法存到到集合中

        TreeSet:底层是二叉树,对存入的数据进行自然排序

HashSet

/*
 * HashSet方法
 */
package com.s.wc;

import java.util.HashSet;
import java.util.Set;

public class Demo2 {
	public static void main(String[] args) {
		Set<String>  s1 = new HashSet<String>();
		s1.add("a");
		s1.add("a");
		s1.add("b");
		s1.add("c");
		
		s1.add("c");
		System.out.println(s1);//[a,b,c]相同元素只会打印一次
		
		//for循环遍历  需要强转
		Object[] arr = s1.toArray();
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
		//增强for循环遍历
		for (String string : s1) {
			System.out.println(string);
		}
	}
}

set集合中自定义对象

package com.s.wc;

import java.util.HashSet;
import java.util.Set;

class People{
	String name;
	int age;
	public People(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "People [name=" + name + ", age=" + age + "]";
	}
	@Override
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}if(obj instanceof People) {
			People people = (People) obj;
			return this.age == people.age && this.name.equals(people.name);
		}
		return false;
	}
	@Override
	public int hashCode() {
		
		return name.hashCode();
	}
}
public class Demo3 {
	public static void main(String[] args) {
		Set<People> set = new HashSet<People>();
		People people1 = new People("张三", 34);
		People people2 = new People("李四", 24);
		People people3 = new People("张三", 34);
		set.add(people1);
		set.add(people2);
		set.add(people3);
		System.out.println(set);
	}
}

总结:如果将对象存入到hashset中的时候,必须重写但该鸟类的equals和hashCode方法,为了保证对象的内容不重复

TreeSet

也是Set接口的实现类,可以保证数据唯一型,存储也是无序的

同时对存入数据会进行自然排序

/*
 * TreeSet
 */
package com.s.wc;

import java.util.Set;
import java.util.TreeSet;

public class Demo4 {
	public static void main(String[] args) {
		Set<Integer> set = new TreeSet<Integer>();
		set.add(56);
		set.add(47);
		set.add(67);
		set.add(25);
		System.out.println(set);//自然排序;
	}

}

TreeSet存自定义对象

存对象需要在需要在类的实现Comparable这个接口,该接口对实现他的每个类的对象强加一个整体排序,这个排序被称为类的自然排序,类的compareTo方法被称为其自然比较方法

讲此对象与指定的对象进行比较以进行排序

返回一个负整数,零或正整数,表示该对象小于,等于或大于指定对象

/*
 * TreeSet存自定义对象
 */
package com.s.wc;

import java.util.Set;
import java.util.TreeSet;

class Student implements Comparable<Student>{
	String name;
	int age;
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	@Override
	public int compareTo(Student o) {
		int num = this.age - o.age;//按照年龄进行排序的
		return num;
	}
	
}
public class Demo5 {
	public static void main(String[] args) {
		Set<Student> set = new TreeSet<Student>();
		set.add(new Student("张三", 28));
		set.add(new Student("李四", 38));
		set.add(new Student("王五", 48));
		set.add(new Student("麻子", 18));
		set.add(new Student("小二", 28));//年龄相同num=0,存不进去;
		System.out.println(set);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值