Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java collections with comparable

Tags:

java

I try to create a collection. Just a small collection with 3 names and i want to sort them by Alphabetically order. and it hits me always this error: Exception in thread "main" java.lang.AbstractMethodError: collection.compareTo(Ljava/lang/Object;)I. in the line: Collections.sort(names); what should i do to overcome my problem?

 public  class collection implements Comparable<collection> {

private String name;

public collection(String name){
    this.name= name;

}

public String getName(){

    return name;

}

public int compareΤο(collection c){
    return this.getName().compareTo(c.getName());

}

}


public class collectionList {

private ArrayList <collection> names;

public collectionList(){
    names = new ArrayList <collection>();
}
public void populate() {
    collection c1 = new collection("Monica Rows");
    names.add(c1);
    collection c2 = new collection("Peter Walker");
    names.add(c2);
    collection c3 = new collection("Jack Miller");
    names.add(c3);


  }

public void sortBy(){
    Collections.sort(names);
}

public String names(){
    String s="";
    for(collection c: names){
        s+=c.getName()+ "\n";
    }

    return s;
}



 }

public class collectionMain {


public static void main(String[] args){
    collectionList c = new collectionList();
    c.populate();
    System.out.println(c.names());

    c.sortBy();
    System.out.println(c.names());
}

}

like image 380
arnold leki Avatar asked Mar 27 '26 23:03

arnold leki


2 Answers

The name of your compareTo method is using unicode characters. The "T" is unicode 0x03A4 and the "o" is 0x03BF. They should be 0x0054 and 0x006F. You can check your characters using the following link.

http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=%26%23932%3B&mode=char

like image 67
Neil Essy Avatar answered Apr 02 '26 22:04

Neil Essy


ignoring the style problems, you're compareTo method is using invalid characters:

compare%CE%A4%CE%BF

copy and paste it into an online url encoder http://meyerweb.com/eric/tools/dencoder/

like image 37
BikeHikeJuno Avatar answered Apr 02 '26 21:04

BikeHikeJuno