2

I did a class "People" which has a String name. Now I want to compare two objects using TreeSet.

public class People<T> implements Comparable<T> {

    public TreeSet<People> treeSet;
    public String name;

    public People(String name)
    {
        treeSet =  new TreeSet();
this.name = name;
    }

.....

@Override
    public int compareTo(T y) {

        if(this.name.equals(y.name)) blablabla; //Here I get error 
    }

Error:

Cannot find symbol
symbol: variable name;
location: variable y of type T
where T is a type variable 
T extends Object declared in class OsobaSet

Does anyone know how to solve the problem?

Zoltán
  • 21,321
  • 14
  • 93
  • 134
szufi
  • 219
  • 1
  • 2
  • 9

1 Answers1

5

Generic type in Comparable interface stands for the type of objects that will be compared.

This is correct usage for your example:

public class People implements Comparable<People>

In this case method signature will be

@Override
public int compareTo(People y) {
    if (this.name.equals(y.name))  { ...
}
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76