[英]Removing specific elements from ArrayList by enum value
I have to remove elements from an ArrayList, but I could not make it work.我必须从 ArrayList 中删除元素,但我无法使其工作。 I have to collect the oldest book name per each genre.我必须为每种类型收集最古老的书名。
List<BookItem> list = new ArrayList<>();
list.add(new BookItem("TitleName1", 1950, "NOVEL"));
list.add(new BookItem("TitleName2", 1970, "HORROR"));
list.add(new BookItem("TitleName3", 1954, "NOVEL"));
list.add(new BookItem("TitleName4", 1989, "HORROR"));
list.add(new BookItem("TitleName5", 1990, "HUMOR"));
list.add(new BookItem("TitleName6", 1995, "HUMOR"));
For example, in case above the desired result would be "TitleName1,TitleName2,TitleName5".例如,在上述情况下,所需的结果将是“TitleName1,TitleName2,TitleName5”。
I tried the following code to achieve that, but I did not receive the desired result.我尝试了下面的代码来实现这一点,但我没有收到想要的结果。 BookGenre is of enum type. BookGenre 是枚举类型。
public List<BookItem> getOldestBookPerGenre(List<BookItem> bookItems){
List<BookItem> genreList = new ArrayList<>();
List<BookItem> returnList = new ArrayList<>();
for(int i=0; i < BookGenre.values().length; i++){
for(BookItem item: bookItems) {
if (item.getBookGenre().ordinal() == i) {
genreList.add(item);
}
}
if (genreList.size() > 0) {
genreList.sort(compareByYear);
returnList.add(genreList.get(0));
}
genreList.clear();
}
return returnList;
}
To get the oldest element from the ArrayList
you can use Java streams ( stream()
, filter(...)
, min(...)
, etc).要从ArrayList
中获取最旧的元素,您可以使用 Java 流( stream()
、 filter(...)
、 min(...)
等)。
It isn't exactly your script but you can easily see how the logic works and therefore adapt it with your code.它不完全是你的脚本,但你可以很容易地看到逻辑是如何工作的,因此可以用你的代码来调整它。
This example searchs for the oldest NOVEL book:此示例搜索最古老的 NOVEL 书籍:
import java.util.*;
import java.util.Arrays;
import java.util.List;
public class test
{
public static void main(String[] args)
{
List<BookItem> list = new ArrayList<>();
list.add(new BookItem("TitleName1", 1950, "NOVEL"));
list.add(new BookItem("TitleName2", 1970, "HORROR"));
list.add(new BookItem("TitleName3", 1954, "NOVEL"));
list.add(new BookItem("TitleName4", 1989, "HORROR"));
list.add(new BookItem("TitleName5", 1990, "HUMOR"));
list.add(new BookItem("TitleName6", 1995, "HUMOR"));
BookItem oldestNovel = list.stream()
.filter(element -> element.genre.equals("NOVEL"))
.min((el1, el2) -> { if(el1.year == el2.year) return 0; return el1.year < el2.year ? -1 : 1; })
.get();
System.out.println(oldestNovel.title);
}
static class BookItem
{
String title = "";
int year = 0;
String genre = "";
public BookItem(String title, int year, String genre)
{
this.title = title;
this.year = year;
this.genre = genre;
}
}
}
Output: Output:
TitleName1
It is easy with java stream, but as you say you can't use Java8 here you have another solution:使用 java stream 很容易,但正如您所说,您不能在这里使用 Java8,您还有另一种解决方案:
public List<BookItem> getOldestBookPerGenre(List<BookItem> bookItems) {
Map<String, BookItem> oldestBookByGenre = new HashMap<>();
for (BookItem bookItem : bookItems) {
oldestBookByGenre.putIfAbsent(bookItem.getGenre(), bookItem);
if (Integer.parseInt(bookItem.getYear()) < Integer.parseInt(oldestBookByGenre.get(bookItem.getGenre()).getYear())) {
oldestBookByGenre.put(bookItem.getGenre(), bookItem);
}
}
return new ArrayList<>(oldestBookByGenre.values());
}
Output list, given the question input: Output 列表,给定问题输入:
BookItem{title='TitleName2', year='1970', novel='HORROR'}
BookItem{title='TitleName1', year='1950', novel='NOVEL'}
BookItem{title='TitleName5', year='1990', novel='HUMOR'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.