栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java进阶篇9——TreeSet

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java进阶篇9——TreeSet

文章目录

1. 集合类总纲2. TreeSet

2.1 TreeSet 性质2.2 元素有序

2.2.1 使用默认排序2.2.2 自定义排序

2.2.2.1 自己构造比较器2.2.2.2 在 Student类里面就重写 compareTo()

1. 集合类总纲

2. TreeSet 2.1 TreeSet 性质

TreeSet 是一个有序的Set 集合,可以按一定的规则指定元素的顺序。不能有重复元素。对象重写 equals() 和 hashcode() 失效。无序不重复 2.2 元素有序 2.2.1 使用默认排序

package javaweb.collection;

import java.util.Iterator;
import java.util.TreeSet;

public class MyTreeSet {
	public static void practice1() {
		TreeSet treeset = new TreeSet();
		
		treeset.add("a");
		treeset.add("c");
		treeset.add("e");
		treeset.add("f");
		treeset.add("b");
		
		Iterator it = treeset.iterator();
		while(it.hasNext()) {
			System.out.print(it.next() + " ");
		}
	}

	public static void main(String[] args) {
		practice1();
	}

}
a b c e f 
2.2.2 自定义排序 2.2.2.1 自己构造比较器
package javaweb.collection;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;


class MyCmp implements Comparator {

	@Override
	public int compare(Object obj1, Object obj2) {
		int x = 0;
		Student s1 = (Student) obj1;
		Student s2 = (Student) obj2;

		if (s1.getAge() > s2.getAge())
			x = -1;
		else if (s1.getAge() < s2.getAge())
			x = 1;
		else
			x = s1.getId().compareTo(s2.getId()); // 前者大于后者,返回1

		return x;
	}
}

public class MyTreeSet {

	public static void main(String[] args) {
		TreeSet treeset = new TreeSet(new MyCmp());

		Student s1 = new Student("1001", "chen", 18);
		Student s2 = new Student("1001", "luo", 25);
		Student s3 = new Student("1003", "zhang", 25);
		Student s4 = new Student("1004", "pi", 20);
		Student s5 = new Student("1002", "wang", 25);

		treeset.add(s1);
		treeset.add(s2);
		treeset.add(s3);
		treeset.add(s4);
		treeset.add(s5);

		Iterator it = treeset.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}

}
 

按年龄降序,相同年龄,按学号升序

1001 luo 25
1002 wang 25
1003 zhang 25
1004 pi 20
1001 chen 18

附件:Studen类

package javaweb.collection;

import java.util.HashSet;
import java.util.Iterator;


public class Student {
	String id, name;
	int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

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

	@Override
	public String toString() {
		return id + " " + name + " " + age;
	}

	@Override
	public boolean equals(Object obj) {
		return id.equals(((Student) obj).id);
	}

	@Override
	public int hashCode() {
		return Integer.valueOf(id);
	}

	public static void main(String[] args) {
		HashSet hashset = new HashSet();

		Student student1 = new Student("1001", "chen", 18);
		Student student2 = new Student("1001", "chen", 18);
		hashset.add(student1);
		hashset.add(student2);

		Iterator it = hashset.iterator();
		while (it.hasNext()) {
			System.out.println(it.next() + " ");
		}
	}

}
2.2.2.2 在 Student类里面就重写 compareTo()
package javaweb.collection;

import java.util.Iterator;
import java.util.TreeSet;

public class Student1 implements Comparable {
	String id, name;
	int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

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

	@Override
	public String toString() {
		return id + " " + name + " " + age;
	}

	@Override
	public int compareTo(Object o) {
		int x = 0;
		Student1 s2 = (Student1) o;

		if (getAge() > s2.getAge())
			x = -1;
		else if (getAge() < s2.getAge())
			x = 1;
		else
			x = getId().compareTo(s2.getId()); // 前者大于后者,返回1

		return x;
	}

	public static void main(String[] args) {
		TreeSet treeset = new TreeSet();

		Student1 s1 = new Student1("1001", "chen", 18);
		Student1 s2 = new Student1("1001", "luo", 25);
		Student1 s3 = new Student1("1003", "zhang", 25);
		Student1 s4 = new Student1("1004", "pi", 20);
		Student1 s5 = new Student1("1002", "wang", 25);

		treeset.add(s1);
		treeset.add(s2);
		treeset.add(s3);
		treeset.add(s4);
		treeset.add(s5);

		Iterator it = treeset.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}
 
1001 luo 25
1002 wang 25
1003 zhang 25
1004 pi 20
1001 chen 18
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号