JAVA------TreeSet of Collection

TreeSet collection features

  1. The elements are ordered. The order here does not refer to the order of storage and retrieval, but sorting according to certain rules. The specific sorting method depends on the construction method

  2. TreeSet(): Sort according to the natural ordering of its elements

  3. TreeSet(Comparator comparator): Sort according to the specified comparator

  4. There is no method with index, so normal for loop can not be used to traverse

  5. Since it is a Set collection, it does not contain a collection of repeated elements


Look at a piece of code:

package Set;

import java.util.TreeSet;

public class TreeSetDemo01 {
    
    
	public static void main(String[] args) {
    
    
		
		//集合创建要使用引用,即包装类
		TreeSet<Integer> ts=new TreeSet<>();
		//自然排序	
		ts.add(20);
		ts.add(40);
		ts.add(30);
		ts.add(10);
		ts.add(30);
		
		for(Integer i:ts) {
    
    //输出10 20 30 40
			System.out.println(i);
		}
	}
}

Add 20,40,30,10,30 to the set, and finally output 10 20 30 40, output in natural order.


Natural ordering

Let's look at a case:

Store student objects and traverse, create a collection using no-argument construction method.
Requirement: sort by age from young to oldest. When the age is the same, sort in alphabetical order by name

First create the student class:

package Set;

public class student implements Comparable<student>{
    
    
	private String name;
	private int age;
	
	//构造方法
	public student() {
    
    
		System.out.println("无参构造");
	}
	
	public student(String name,int age) {
    
    
		this.name=name;
		this.age=age;
	}
	
	//提供get/set方法
	public void setAge(int age) {
    
    
		if(age<0||age>100)
		{
    
    
			System.out.println("年龄有误");
		}else {
    
    
			this.age=age;
		}
		
	}
	
	public int getAge() {
    
    
		return age;
	}
	
	public void setName(String name) {
    
    
		this.name=name;
	}
	
	public String getName() {
    
    
		return name;
	}
	
	public void show() {
    
    
		System.out.println(name+","+age);
	}

	@Override
	public int compareTo(student s) {
    
    
		//return 0;//重复元素,不添加
		//return 1;//按照输入升序排序
		//return -1;//按照输入逆序排序
		int num=this.age-s.age;//年龄升序 this为s2,s为s1
		//int num=s.age-this.age;//降序
		
		int num2=num==0?this.name.compareTo(s.name):num;
		return num2;
	}
	
}

Natural ordering means that the class to which the element belongs implements the Comparable interface and overrides the compareTo(To) method

  • return 0;//Repeating elements, do not add
  • return 1;//Sort according to input ascending order
  • return -1;//Sort according to the input reverse order

The following line of code is to judge if two people are the same age, then compare the names, because String is a rewrite of compareTo, so it can be used directly

int num2=num==0?this.name.compareTo(s.name):num;
@Override
	public int compareTo(student s) {
    
    
		//return 0;//重复元素,不添加
		//return 1;//按照输入升序排序
		//return -1;//按照输入逆序排序
		int num=this.age-s.age;//年龄升序 this为s2,s为s1
		//int num=s.age-this.age;//降序
		
		int num2=num==0?this.name.compareTo(s.name):num;
		return num2;
	}

main:

package Set;

import java.util.TreeSet;

public class TreeSetDemo02 {
    
    
	
public static void main(String[] args) {
    
    
		
		//集合创建要使用引用,即包装类
		TreeSet<student> ts=new TreeSet<>();
		
		student s1=new student("小李",20);
		student s2=new student("小李",50);
		student s3=new student("小张",35);
		student s4=new student("小王",18);
		student s5=new student("小康",35);
		
		ts.add(s1);
		ts.add(s2);
		ts.add(s3);
		ts.add(s4);
		ts.add(s5);
		
		for(student s:ts) {
    
    
			System.out.println(s.getName()+","+s.getAge());
		}
	}		
}


Specified comparator sort

Look directly at a case:

Store student objects and traverse, create a collection using the construction method with parameters
*Requirement: sort by age from young to oldest, when the age is the same, sort in alphabetical order by name

According to the sorting of the comparator, it is definitely necessary to implement the Comparator interface in the class.
Here directly use the anonymous inner class to implement the Comparator interface.


Student category:

package Set;

public class student03{
    
    
	private String name;
	private int age;
	
	//构造方法
	public student03() {
    
    
		System.out.println("无参构造");
	}
	
	public student03(String name,int age) {
    
    
		this.name=name;
		this.age=age;
	}
	
	//提供get/set方法
	public void setAge(int age) {
    
    
		if(age<0||age>100)
		{
    
    
			System.out.println("年龄有误");
		}else {
    
    
			this.age=age;
		}
		
	}
	
	public int getAge() {
    
    
		return age;
	}
	
	public void setName(String name) {
    
    
		this.name=name;
	}
	
	public String getName() {
    
    
		return name;
	}
	
	public void show() {
    
    
		System.out.println(name+","+age);
	}
	
}

main:

package Set;

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

public class TreeSetDemo03 {
    
    


public static void main(String[] args) {
    
    
			
		TreeSet<student03> ts=new TreeSet<student03>(new Comparator<student03>() {
    
    
			@Override
			public int compare(student03 s1,student03 s2) {
    
    //匿名内部类实现Comparator接口
				//this.age-s.age
				//s1  s2
				int num=s1.getAge()-s2.getAge();
				int num2=num==0?s1.getName().compareTo(s2.getName()):num;
				return num2;
			}		
		});
	
		student03 s1=new student03("小李",20);
		student03 s2=new student03("小李",50);
		student03 s3=new student03("小张",35);
		student03 s4=new student03("小王",18);
		student03 s5=new student03("小康",35);
		
		ts.add(s1);
		ts.add(s2);
		ts.add(s3);
		ts.add(s4);
		ts.add(s5);
		
		for(student03 s:ts) {
    
    
			System.out.println(s.getName()+","+s.getAge());
		}
	}		
}

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113527294