如何在Java中向 TreeSet 添加自定义类对象?
TreeSet 是Java中 SortedSet 接口的一个实现,它使用红黑树进行存储。默认情况下,它保持升序。它只包含独特的元素。它不允许空元素。访问和检索时间非常快。要将用户定义的对象添加到 TreeSet 中,我们需要实现一个 Comparable 接口。我们要添加到 TreeSet 中的元素必须是可比较的类型。如果我们不实现 Comparable 接口,那么它将抛出ClassCastException。
示例 1
Java
// Importing util package
import java.util.*;
// Custome class Car implements Comparable interface
class Car implements Comparable {
// attributes
int Modelno;
String name, city;
int stock;
// Car constructor
public Car(int Modelno, String name, String city,
int stock)
{
this.Modelno = Modelno;
this.name = name;
this.city = city;
this.stock = stock;
}
// Override the compareTo() method
public int compareTo(Car c)
{
if (stock > c.stock) {
return 1;
}
else if (stock < c.stock) {
return -1;
}
else {
return 0;
}
}
}
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Define an objects of TreeSet class
TreeSet set = new TreeSet();
// Creating Car objects
Car c1 = new Car(132, "BMW", "Rajkot", 35);
Car c2 = new Car(269, "Audi", "Surat", 20);
Car c3 = new Car(560, "Kia", "Vadodara", 15);
Car c4 = new Car(109, "Creta", "Ahmedabad", 26);
// Adding Car objects to TreeSet
set.add(c1);
set.add(c2);
set.add(c3);
set.add(c4);
// Traversing TreeSet
for (Car c : set) {
System.out.println(c.stock + " " + c.name + " "
+ c.city + " " + c.Modelno);
}
}
}
Java
// Importing util package
import java.util.*;
// Custome class Car implements Comparable interface
class Car implements Comparable {
// attributes
int Modelno;
String name, city;
int stock;
// Car constructor
public Car(int Modelno, String name, String city,
int stock)
{
this.Modelno = Modelno;
this.name = name;
this.city = city;
this.stock = stock;
}
// Override the compareTo() method
public int compareTo(Car c)
{
return name.compareTo(c.name);
}
}
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Define an objects of TreeSet class
TreeSet set = new TreeSet();
// Creating Car objects
Car c1 = new Car(132, "BMW", "Rajkot", 35);
Car c2 = new Car(269, "Audi", "Surat", 20);
Car c3 = new Car(560, "Kia", "Vadodara", 15);
Car c4 = new Car(109, "Creta", "Ahmedabad", 26);
// Adding Car objects to TreeSet
set.add(c1);
set.add(c2);
set.add(c3);
set.add(c4);
// Traversing TreeSet
for (Car c : set) {
System.out.println(c.name + " " + c.stock + " "
+ c.city + " " + c.Modelno);
}
}
}
输出
15 Kia Vadodara 560
20 Audi Surat 269
26 Creta Ahmedabad 109
35 BMW Rajkot 132
我们需要覆盖compareTo()方法,以便它按特定顺序对我们的集合进行排序。
示例 2:让我们采用相同的示例,但现在我们根据汽车名称覆盖 compareTo() 方法。
Java
// Importing util package
import java.util.*;
// Custome class Car implements Comparable interface
class Car implements Comparable {
// attributes
int Modelno;
String name, city;
int stock;
// Car constructor
public Car(int Modelno, String name, String city,
int stock)
{
this.Modelno = Modelno;
this.name = name;
this.city = city;
this.stock = stock;
}
// Override the compareTo() method
public int compareTo(Car c)
{
return name.compareTo(c.name);
}
}
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Define an objects of TreeSet class
TreeSet set = new TreeSet();
// Creating Car objects
Car c1 = new Car(132, "BMW", "Rajkot", 35);
Car c2 = new Car(269, "Audi", "Surat", 20);
Car c3 = new Car(560, "Kia", "Vadodara", 15);
Car c4 = new Car(109, "Creta", "Ahmedabad", 26);
// Adding Car objects to TreeSet
set.add(c1);
set.add(c2);
set.add(c3);
set.add(c4);
// Traversing TreeSet
for (Car c : set) {
System.out.println(c.name + " " + c.stock + " "
+ c.city + " " + c.Modelno);
}
}
}
输出
Audi 20 Surat 269
BMW 35 Rajkot 132
Creta 26 Ahmedabad 109
Kia 15 Vadodara 560