Как использовать Collectors.toList для ArrayList? - PullRequest
0 голосов
/ 27 марта 2020

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());
        }
    }

1 Ответ

2 голосов
/ 27 марта 2020

Collectors.toList() будет собираться в List преднамеренно неопределенного типа. Как указано в его документации :

Нет никаких гарантий относительно типа, изменчивости, сериализуемости или безопасности потоков возвращаемого List ...

Если вам не требуется спецификация c List с гарантиями относительно одной из упомянутых черт, просто измените объявление переменной результата на

List<exercise> filteredExercises = …

В противном случае, если вам действительно нужно ArrayList, используйте Collectors.toCollection(ArrayList::new) вместо Collectors.toList().

См. Также этот исчерпывающий список альтернатив .


В качестве примечания следует попытайтесь придерживаться стандартных соглашений об именах и используйте заглавные буквы для начала имен ваших классов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...