Java Comparator 在 SortedSet 中使用

x33g5p2x  于2022-09-23 转载在 Java  
字(3.6k)|赞(0)|评价(0)|浏览(301)

Java Comparator 可用于控制 SortedSet 数据结构的顺序。 SortedSet 的实现类是 TreeSetConcurrentSkipListSet。我们可以将 Comparator 实例传递给 TreeSetConcurrentSkipListSet 类的构造函数来控制其顺序。 SortedSet 提供了 comparator() 方法,该方法返回用于对该集合中的元素进行排序的比较器。如果 SortedSet 使用其元素的自然排序,则 comparator() 方法返回 null。在此页面上,我们将提供示例以将比较器与 TreeSetConcurrentSkipListSet 类一起使用。

Comparator 与 TreeSet

TreeSet 根据元素的自然顺序或在集合创建时提供的比较器对元素进行排序,具体取决于使用的构造函数。我们可以通过使用以下构造函数传递 Comparator 来实例化 TreeSet 类。

TreeSet(Comparator<? super E> comparator)

它构造一个新的空树集,根据指定的比较器进行排序。当我们不通过比较器时,TreeSet 根据元素的自然顺序对元素进行排序。对于自然排序,一个类需要实现 Comparable 接口并重写 compareTo 方法。
为了获得我们的 TreeSet 对象使用的比较器,SortedSet 提供了 comparator() 方法。
现在找到使用带有 TreeSet 类的比较器来控制其元素顺序的示例。
TreeSetDemo.java

package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo {
  public static void main(String[] args) {
	Student s1 = new Student("Shyam", 18);
	Student s2 = new Student("Mohan", 20);
	Student s3 = new Student("Ram", 22);
	
	System.out.println("---TreeSet Order With Comparator---");
	
	Comparator<Student> ageComparator = Comparator.comparing(Student::getAge);
	TreeSet<Student> myTreeSet = new TreeSet<>(ageComparator);
	myTreeSet.addAll(Arrays.asList(s1, s2, s3));
	myTreeSet.forEach(s -> System.out.println(s));	
	//System.out.println("Comparator: "+ myTreeSet.comparator());
	
	System.out.println("---TreeSet Natural Order (With Comparable)---");
	
	myTreeSet = new TreeSet<>();
	myTreeSet.addAll(Arrays.asList(s1, s2, s3));
	myTreeSet.forEach(s -> System.out.println(s));
  }
}

Student.java

package com.concretepage;
public class Student implements Comparable<Student> {
  private String name;
  private int age;
  public Student(String name, int age) {
	this.name = name;
	this.age = age;
  }
  public String getName() {
	return name;
  }
  public int getAge() {
	return age;
  }
  @Override
  public int compareTo(Student s) {
	return name.compareTo(s.getName());
  }
  @Override  
  public String toString(){
	return name + "-" + age; 
  }
}

输出

---TreeSet Order With Comparator---
Shyam-18
Mohan-20
Ram-22
---TreeSet Natural Order (With Comparable)---
Mohan-20
Ram-22
Shyam-18

Comparator 与 ConcurrentSkipListSet

ConcurrentSkipListSet 根据元素的自然顺序或在集合创建时提供的比较器对元素进行排序,具体取决于使用的构造函数。我们可以通过使用以下构造函数传递 Comparator 来实例化 ConcurrentSkipListSet 类。

ConcurrentSkipListSet(Comparator<? super E> comparator)

它构造一个新的空集,根据指定的比较器对其元素进行排序。当我们不通过比较器时,ConcurrentSkipListSet 根据元素的自然顺序对元素进行排序。对于自然排序,一个类需要实现 Comparable 接口并覆盖 compareTo 方法。
为了获得我们的 ConcurrentSkipListSet 对象使用的比较器,SortedSet 提供了 comparator() 方法。
现在找到使用带有 ConcurrentSkipListSet 类的比较器来控制其元素顺序的示例。
ConcurrentSkipListSetDemo.java

package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.ConcurrentSkipListSet;
public class ConcurrentSkipListSetDemo {
  public static void main(String[] args) {
	Student s1 = new Student("Shyam", 18);
	Student s2 = new Student("Mohan", 20);
	Student s3 = new Student("Ram", 22);
	
	System.out.println("---ConcurrentSkipListSet Order With Comparator---");
	
	Comparator<Student> ageComparator = Comparator.comparing(Student::getAge);
	ConcurrentSkipListSet<Student> myConcurrentSkipList = new ConcurrentSkipListSet<>(ageComparator);
	myConcurrentSkipList.addAll(Arrays.asList(s1, s2, s3));
	myConcurrentSkipList.forEach(s -> System.out.println(s));	
	//System.out.println("Comparator: "+ myConcurrentSkipList.comparator());
	
	System.out.println("---ConcurrentSkipListSet Natural Order (With Comparable)---");
	
	myConcurrentSkipList = new ConcurrentSkipListSet<>();
	myConcurrentSkipList.addAll(Arrays.asList(s1, s2, s3));
	myConcurrentSkipList.forEach(s -> System.out.println(s));
  }
}

输出

---ConcurrentSkipListSet Order With Comparator---
Shyam-18
Mohan-20
Ram-22
---ConcurrentSkipListSet Natural Order (With Comparable)---
Mohan-20
Ram-22
Shyam-18

相关文章

微信公众号

最新文章

更多