android - 如何对ArrayList使用Collectors.toList?

标签 android arraylist collections

enter image description here 我试图通过创建一个存储前 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/

相关文章:

Android facebook SDK,使用不同语言的配置文件将图片上传到墙上?

java - 在 Material Chipgroup (Android) 中检查 Animate Chip

javascript - 如何使用 JS 获取不同的值并对 JSON 的总数求和

java - 在android中的BroadcastReceiver类的onReceive方法中访问ArrayList项目

java - 在 Java 字符串集合(map、hash.etc)中为同一个键存储 10 个值

java - 不确定 ConcurrentModificationException 的原因

java - 从 Android Git 创建 Eclipse 项目

Android appcompact 不显示上下文操作栏中的文本选择复制操作

java - ArrayList:java.lang.IndexOutOfBoundsException:索引:283,大小:283

java - 反转 Queue<Integer> 并将其转换为 int 数组