Em muốn sắp xếp các đối tượng thêm vào hs dựa trên thuộc tính name vậy ở phương thức hashCode() em phải viết như thế nào???
public class test {
public static void main(String args[])
{
HashSet hs =new HashSet();
hs.add(new State("dfh", 5));
hs.add(new State("fgh", 7));
hs.add(new State("hkh", 4));
hs.add(new State("we", 9));
hs.add(new State("jfgh", 1));
for(Iterator i=hs.iterator();i.hasNext();)
{
System.out.println(i.next());
}
}
}
public class State
{
private String name;
private int population;
public State(String name, int population)
{
super();
this.name = name;
this.population = population;
}
public String getName()
{
return this.name;
}
public int getPopulation()
{
return this.population;
}
public String toString()
{
return getName() + " - " + getPopulation();
}
@Override
public int hashCode() {
/*String s = name.substring(0, 1);
return Integer.parseInt(s);*/
}
}

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?