我试图通过创建一个存储前 3 个值的单独变量来过滤数组列表。但是,在集合中出现错误。我对此很陌生,所以任何帮助都会很棒!
public static ArrayList<exercise> exerciseDetail() {
ArrayList<exercise> elist = new ArrayList<>();
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Leg Raises", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bridges", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Burpees", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Side Twists", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Planks", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
elist.add(new exercise("Bicycle Crunches", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
return elist;
ArrayList<exercise> filteredExercises = elist.stream().filter(item-> item.getName().equals("Bicycle Crunches") || item.getName().equals("Leg Raises")|| item.getName().equals("Bridges")).collect(Collectors.toList());
}
}
最佳答案
Collectors.toList()
将收集到有意未指定类型的 List
中。如its documentation说:
There are no guarantees on the type, mutability, serializability, or thread-safety of the
List
returned…
如果您不需要特定的 List
来保证上述特征之一,只需将结果变量的声明更改为
List<exercise> filteredExercises = …
否则,如果您确实需要 ArrayList
,请使用 Collectors.toCollection(ArrayList::new)
而不是 Collectors.toList()
.
另请参阅this exhaustive list of alternatives .
顺便说一句,您应该尝试遵守标准命名约定,并使用大写字母作为类名的开头。
关于android - 如何对ArrayList使用Collectors.toList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60878435/