Java使用looklance比较器对列表进行排序

时间:2015-04-16 14:49:32

标签: java arrays sorting comparator

我试图按照与标准列表相对应的灵感来对音乐列表进行排序。

public class Music implements Comparable<CriteriaList> {
    private String genre, artist, album, titre, price, note; 
    // getters, setters
    public int compareTo(CriteriaList list) {
        boolean title, album, genre, artist, note;
        title = !list.getTitle().isEmpty() && this.getTitre().equals(list.getTitle());
        album = !list.getAlbum().isEmpty() && this.getAlbum().equals(list.getAlbum());
        genre = !list.getGenre().isEmpty() && this.getGenre().equals(list.getGenre());
        artist = !list.getArtist().isEmpty() && this.getArtist().equals(list.getArtist());
        note = !list.getNote().isEmpty() && (Integer.parseInt(this.getNote()) >= Integer.parseInt(list.getNote()));
        return ((title ? 1 : 0) + (album ? 1 : 0) + (genre ? 1 : 0) + (artist ? 1 : 0) + (note ? 1 : 0));
    }
}

我的函数compareTo返回与条件列表匹配的字段数,并测试输入是否为空。

public class MusicProvider extends Agent {
    public List<Music> getMusicsByCL(CriteriaList list) {
        ArrayList<Music> res = new ArrayList<Music>();
        int[] revelanceTab = new int[res.size()];
        int i = 0, revelance;
        for (Music music : musicListAvailable) {
            revelance = music.compareTo(list);
            if (revelance > 1) {
                res.add(music);
                revelanceTab[++i] = revelance;
            }
        }
        // sort res with revelanceTab
        return res;
    }
}

在这里,我想要以最小的1的灵感来检索音乐,并通过灵感来对它们进行排序。我怎么能这样做?

3 个答案:

答案 0 :(得分:4)

假设您已经创建了实际计算相关性的功能,我会这样做。

创建一个简单的类来保存音乐和计算的相关性分数,只需传入条件并存储计算结果。

public class ScoredMusic {
    private int relevanceScore;
    public ScoredMusic(Music m) { ... }
    public void calculateRelevance(Criteria criteria) { ... }
    public Music getMusic() { ... }
    public int getRelevanceScore() { ... }
}

然后我会对你拥有的所有音乐实例进行评分,将它们存储在列表中,并执行一个非常简单的compareTo()实现,只需比较每个relevanceScore实例之间的ScoredMusic。 / p>

答案 1 :(得分:1)

可比较用于将两个音乐实例相互比较。如果要与外部实体进行比较,请使用Comparator实现并将其传递给Collections.sort(List, Comparator)。比较器需要使用CriteriaList进行初始化,如果第一个元素应该排在更高位置,则compare方法将返回正数,如果第二个元素应该排在更高位置,则返回负数,如果它们是等效的,则返回0。 在您的示例中,您将使用compareTo方法并从第一个元素中减去第二个元素的分数并返回该分数。

这样的事情:

import java.util.Comparator;

public class MusicComparator implements Comparator<Music> {

    private final CriteriaList criteria;

    public MusicComparator(CriteriaList criteria) {
        this.criteria = criteria;
    }
    @Override
    public int compare(Music o1, Music o2) {
        return score(o1) - score(o2);
    }

    private int score(Music music) {
        boolean title, album, genre, artist, note;
        title = criteria.getTitle().isEmpty() || criteria.getTitle().equals(music.getTitle());
        album = criteria.getAlbum().isEmpty() || criteria.getAlbum().equals(music.getAlbum());
        genre = criteria.getGenre().isEmpty() || criteria.getGenre().equals(music.getGenre());
        artist = criteria.getArtist().isEmpty() || criteria.getArtist().equals(music.getArtist());
        note = criteria.getNote().isEmpty() || (!music.getNote().isEmpty() && Integer.parseInt(music.getNote()) >= Integer.parseInt(criteria.getNote()));
        return ((title ? 1 : 0) + (album ? 1 : 0) + (genre ? 1 : 0) + (artist ? 1 : 0) + (note ? 1 : 0));
    }
}

顺便说一句,isEmpty()方法不会保护您免受空指针异常的影响。如果允许字段为空,则需要更好的处理方法。

答案 2 :(得分:0)

这是我的最后一堂课,

public class ScoredMusic implements Comparable<ScoredMusic> {
    private int revelanceScore = 0;
    private Music music;

    public ScoredMusic(Music music, CriteriaList crit) {
        this.music = music;
        calculateRevelance(crit);
    }

    private void calculateRevelance(CriteriaList list) {
        boolean title, album, genre, artist, note;
        title = !list.getTitle().isEmpty() && music.getTitre().equals(list.getTitle());
        album = !list.getAlbum().isEmpty() && music.getAlbum().equals(list.getAlbum());
        genre = !list.getGenre().isEmpty() && music.getGenre().equals(list.getGenre());
        artist = !list.getArtist().isEmpty() && music.getArtist().equals(list.getArtist());
        note = !list.getNote().isEmpty() && (Integer.parseInt(music.getNote()) >= Integer.parseInt(list.getNote()));
        revelanceScore = ((title ? 1 : 0) + (album ? 1 : 0) + (genre ? 1 : 0) + (artist ? 1 : 0) + (note ? 1 : 0));
    }

    public Music getMusic() {
        return music;
    }

    public int getRevelanceScore() {
        return revelanceScore;
    }

    public int compareTo(ScoredMusic other) {
        return Integer.compare(this.getRevelanceScore(), other.getRevelanceScore());
    }
}

和我的第二堂课

public List<ScoredMusic> getMusicsScoredByCL(CriteriaList list) {
    ArrayList<ScoredMusic> scoredMusics = new ArrayList<ScoredMusic>();
    ScoredMusic sc;
    for (Music music : musicListAvailable) {
        sc = new ScoredMusic(music, list);
        scoredMusics.add(sc);
    }
    // sort by revelance and descending order
    Collections.sort(scoredMusics, Collections.reverseOrder());
    return scoredMusics;
}