我有两个解决方案来构建一个构成比较器库的枚举:
public enum SongComparator { BY_TITLE(Comparator.comparing(Song::getTitle)), BY_ARTIST(Comparator.comparing(Song::getArtist)), BY_DURATION(Comparator.comparing(Song::getDuration)); private Comparator<Song> comp; private SongComparator(Comparator<Song> comp) { this.comp = comp; } public Comparator<Song> get() { return comp; } }和...
public enum SongComparator2 implements Comparator<Song> { BY_TITLE { public int compare(Song s1, Song s2) { return s1.getTitle().compareTo(s2.getTitle()); }; }, BY_ARTIST { public int compare(Song s1, Song s2) { return s1.getArtist().compareTo(s2.getArtist()); }; }, BY_DURATION { public int compare(Song s1, Song s2) { return Integer.compare(s1.getDuration(), s2.getDuration()); }; }; }如果我想用一个比较器代替枚举值,在第一个解决方案中,我必须说SongComparator.BY_TITLE.get(); 而在第二个解决方案中,我只能说SongComparator2.BY_TITLE。
在这个意义上,第二个更好,但是,我不喜欢为每个枚举值编写public int compare ... etc,而想要像第一种方法那样使用Comparator.comparing。 有没有办法做到这一点?
我几乎想要这样说:BY_TITLE {return Comparator.comparing(Song :: getTitle); };
I have two solutions to an idea of having an enum that forms a library of comparators:
public enum SongComparator { BY_TITLE(Comparator.comparing(Song::getTitle)), BY_ARTIST(Comparator.comparing(Song::getArtist)), BY_DURATION(Comparator.comparing(Song::getDuration)); private Comparator<Song> comp; private SongComparator(Comparator<Song> comp) { this.comp = comp; } public Comparator<Song> get() { return comp; } }and...
public enum SongComparator2 implements Comparator<Song> { BY_TITLE { public int compare(Song s1, Song s2) { return s1.getTitle().compareTo(s2.getTitle()); }; }, BY_ARTIST { public int compare(Song s1, Song s2) { return s1.getArtist().compareTo(s2.getArtist()); }; }, BY_DURATION { public int compare(Song s1, Song s2) { return Integer.compare(s1.getDuration(), s2.getDuration()); }; }; }If I want to substitute an enum value where a Comparator is expected, in the first solution I have to say SongComparator.BY_TITLE.get(); whereas in the second solution I can just say SongComparator2.BY_TITLE.
The second is better in this sense, however, I don't like having to write public int compare... etc for each enum value and wanted instead to make use of Comparator.comparing as in the first approach. Is there a way to achieve this?
I almost wanted to say something like: BY_TITLE { return Comparator.comparing(Song::getTitle); };
最满意答案
有什么问题:
public enum SongComparator implements Comparator<Song> { BY_TITLE(Comparator.comparing(Song::getTitle)), BY_ARTIST(Comparator.comparing(Song::getArtist)), BY_DURATION(Comparator.comparing(Song::getDuration)); private Comparator<Song> comp; private SongComparator(Comparator<Song> comp) { this.comp = comp; } public int compare(Song s1, Song s2) { return comp.compare(s1, s2); }; }What's wrong with:
public enum SongComparator implements Comparator<Song> { BY_TITLE(Comparator.comparing(Song::getTitle)), BY_ARTIST(Comparator.comparing(Song::getArtist)), BY_DURATION(Comparator.comparing(Song::getDuration)); private Comparator<Song> comp; private SongComparator(Comparator<Song> comp) { this.comp = comp; } public int compare(Song s1, Song s2) { return comp.compare(s1, s2); }; }更多推荐
发布评论