在对象的属性</integer,>上对TreeMap <integer,object =“”>进行排序

时间:2013-04-15 14:22:13

标签: java swing jtable

我有Library类创建Books对象或Author对象的地图,我需要能够使用Book对象属性对地图进行排序。该地图用于为书籍创建两个JTable,为作者创建另一个,我需要能够对书表中的书属性列进行排序。我必须使用地图,不能只使用JTable分拣机顺便说一句。使用bookIndex作为键创建地图为bookIndex正确排序地图,但这是自然顺序。

如何对Book对象已创建的其他属性的地图进行排序。例如,如果我希望它根据bookTitle对现有地图进行排序?

我知道我需要使用比较器,但我已经搜索并尝试了,但无法解决这个问题。

Main(创建库对象并创建书籍和作者对象并将其添加到库地图中):

    static Library<Book> bookLibrary = new Library<Book>();
    Book book = new Book();
        book.setBookIndex(Integer.parseInt(ar[1]));
        book.setTitle(ar[2]);
        book.setGenre(ar[3]);
        book.setPrice(ar[4]);
        book.setAuthorIndex(Integer.parseInt(ar[5]));

    bookLibrary.add(book.getBookIndex(), book); //adds the book object to library


    static Library<Author> authorLibrary = new Library<Author>();
    Author author = new Author();
                author.setAuthorIndex(Integer.parseInt(ar[1]));
                author.setName(ar[2]);
                author.setStreetAddress(ar[3]);
                author.setCity(ar[4]);
                author.setState(ar[5]);
                author.setZip(ar[6]);
                author.setPhone(ar[7]);

    authorLibrary.add(author.getAuthorIndex(), author); //adds the author object to the library

书类(作者类对此更为敏感):

    public class Book implements BookInterface {

        private Integer bookIndex;
        private String title, genre, price;
        private Integer authorIndex;

       ....
    }

图书馆班级:

    public class Library<T> implements LibraryInterface<T> {
        private Map<Integer, T> map = new TreeMap<Integer, T>();
        ...
    }

。 。

以下是我将来如何使用它的方法:

    public class Library<T> implements LibraryInterface<T> {
        private Map<String, T> map = new TreeMap<String, T>(new bookComparator());

新课程:

    public class bookComparator implements Comparator<Object> {

@Override
public int compare(Object o1, Object o2) {
    String bookTitle1 = (String) o1;
    String bookTitle2 = (String) o2;
    int res = bookTitle1.compareToIgnoreCase(bookTitle2);;
    return res != 0 ? res : 1;

        }
    }

在主类中创建一个创建新TreeMap的排序方法。我会创建原始地图的副本并替换原始地图,但它现在可以使用:

    /**
 * Sort The Book Table by Book Title
 * 
 * @param bookLibrary2
 */
private void sortByBookTitle() {
    bookRowElement.clear();
    Library<Book> sortBookLibrary = new Library<Book>();
    for ( Entry<String, Book> entry: bookLibrary.entrySet()) {

        String key = entry.getValue().getTitle();  //Additional sorts can be mad by changing .getTitle() to getWhatever()
        Book value = entry.getValue();
        sortBookLibrary.add(key, value);
    }
    for ( Entry<String, Book> entry: sortBookLibrary.entrySet()) {
        bookRowElement.addElement(createBookSearchList(entry.getValue()));
    }
    bookTable.repaint();
}

2 个答案:

答案 0 :(得分:1)

在您的情况下,我将使用给定的比较器创建新的TreeMap并将所有元素复制到新地图。我想你会得到你的结果

答案 1 :(得分:1)

TreeMap has a constructor where you can pass in your custom Comparator。我想这会为你做好工作。

public TreeMap(Comparator<? super K> comparator)

你的课程可以这样:

public class Library<T> implements LibraryInterface<T> {
    private Map<Integer, T> map = new TreeMap<Integer, T>(myComparator);
    // ...
}