Simplifying using lambdas

Hi,
I have the following code :
            011 - 42470810
            ongoingAndCompleteTasks = ongoingAndCompleteTasks.stream().filter(new Predicate<Task>() {
                @Override
                public boolean test(Task task) {
                    return !task.getCurrentlyAssignedTo().getUserGuid().equals(workflowUser.getUserGuid());
                }
            }).collect(Collectors.toList());
            List<MyTask> ongoingAndCompleteMyTasks = ongoingAndCompleteTasks.stream().map(new Function<Task, MyTask>() {
                @Override
                public MyTask apply(Task task) {
                    return new MyTask(task, null, dikeService, workflowUser.getUserGuid());
                }
            }).collect(Collectors.toList());
            Set<Integer> workflowIdList = ongoingAndCompleteMyTasks.stream().map(new Function<MyTask, Integer>() {
                @Override
                public Integer apply(MyTask myTask) {
                    return myTask.getWorkflowId();
                }
            }).collect(Collectors.toSet());
            List<GroupTask.GroupProcessTask> groupProcessTaskList = new ArrayList<>();
            workflowIdList.forEach(new Consumer<Integer>() {
                @Override
                public void accept(Integer workflowId) {
                    List<MyTask> myTaskList = ongoingAndCompleteMyTasks.stream().filter(new Predicate<MyTask>() {
                        @Override
                        public boolean test(MyTask myTask) {
                            return myTask.getWorkflowId() == workflowId;
                        }
                    }).collect(Collectors.toList());
                    if (!myTaskList.isEmpty()) {
                        GroupTask.GroupProcessTask groupProcessTask = new GroupTask.GroupProcessTask().setProcessName(myTaskList.get(0).getWorkflowName())
                                .setTaskList(myTaskList);
                        groupProcessTaskList.add(groupProcessTask);
                    }

                }
            });
           




            private void sortGroupTaskList(List<GroupTask> groupTaskList) {
        groupTaskList.sort(new Comparator<GroupTask>() {
            @Override
            public int compare(GroupTask gt1, GroupTask gt2) {
                List<MyTask> myTaskList1 = gt1.getGroupProcessTaskList().stream().flatMap(new Function<GroupTask.GroupProcessTask, Stream<? extends MyTask>>() {
                    @Override
                    public Stream<? extends MyTask> apply(GroupTask.GroupProcessTask groupProcessTask) {
                        return groupProcessTask.getTaskList().stream();

                    }
                }).collect(Collectors.toList());
                List<MyTask> myTaskList2 = gt2.getGroupProcessTaskList().stream().flatMap(new Function<GroupTask.GroupProcessTask, Stream<? extends MyTask>>() {
                    @Override
                    public Stream<? extends MyTask> apply(GroupTask.GroupProcessTask groupProcessTask) {
                        return groupProcessTask.getTaskList().stream();

                    }
                }).collect(Collectors.toList());
                Optional<MyTask> myTask1MaxOptional = myTaskList1.stream().reduce(new BinaryOperator<MyTask>() {
                    @Override
                    public MyTask apply(MyTask myTask, MyTask myTask2) {
                        return myTask.getCreatedOn().compareTo(myTask2.getCreatedOn()) < 0 ? myTask2 : myTask;
                    }
                });
                Optional<MyTask> myTask2MaxOptional = myTaskList2.stream().reduce(new BinaryOperator<MyTask>() {
                    @Override
                    public MyTask apply(MyTask myTask, MyTask myTask2) {
                        return myTask.getCreatedOn().compareTo(myTask2.getCreatedOn()) < 0 ? myTask2 : myTask;
                    }
                });
                Timestamp myTask1CreatedOn = myTask1MaxOptional.get().getCreatedOn();
                Timestamp myTask2CreatedOn = myTask2MaxOptional.get().getCreatedOn();
                int compareValue = 0;
                if (myTask1CreatedOn.compareTo(myTask2CreatedOn) < 0)
                    compareValue = 1;
                if (myTask1CreatedOn.compareTo(myTask2CreatedOn) == 0)
                    compareValue = 0;
                if (myTask1CreatedOn.compareTo(myTask2CreatedOn) > 0)
                    compareValue = -1;
                return compareValue;
            }
        });
    }

Open in new window


I want to use lambdas instead of using these long functions.. Please suggest how to use it here.. And any alternatives to simplify it.

Thanks
Rohit BajajAsked:
Who is Participating?
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Something like this...

            ongoingAndCompleteTasks = ongoingAndCompleteTasks.stream().filter((task) -> !task.getCurrentlyAssignedTo().getUserGuid().equals(workflowUser.getUserGuid())).collect(Collectors.toList());
            List<MyTask> ongoingAndCompleteMyTasks = ongoingAndCompleteTasks.stream().map((task) -> return new MyTask(task, null, dikeService, workflowUser.getUserGuid())).collect(Collectors.toList());
            Set<Integer> workflowIdList = ongoingAndCompleteMyTasks.stream().map((myTask) -> myTask.getWorkflowId()).collect(Collectors.toSet());
            List<GroupTask.GroupProcessTask> groupProcessTaskList = new ArrayList<>();
            workflowIdList.forEach((workflowId) -> {
                    List<MyTask> myTaskList = ongoingAndCompleteMyTasks.stream().filter((myTask) -> myTask.getWorkflowId() == workflowId).collect(Collectors.toList());
                    if (!myTaskList.isEmpty()) {
                        GroupTask.GroupProcessTask groupProcessTask = new GroupTask.GroupProcessTask().setProcessName(myTaskList.get(0).getWorkflowName())
                                .setTaskList(myTaskList);
                        groupProcessTaskList.add(groupProcessTask);
                    }

                });
           




            private void sortGroupTaskList(List<GroupTask> groupTaskList) {
        groupTaskList.sort((gt1, gt2) -> {
                List<MyTask> myTaskList1 = gt1.getGroupProcessTaskList().stream().flatMap(groupProcessTask) -> groupProcessTask.getTaskList().stream()).collect(Collectors.toList());
                List<MyTask> myTaskList2 = gt2.getGroupProcessTaskList().stream().flatMap((groupProcessTask) -> groupProcessTask.getTaskList().stream()).collect(Collectors.toList());
                Optional<MyTask> myTask1MaxOptional = myTaskList1.stream().reduce((myTask, myTask2) -> myTask.getCreatedOn().compareTo(myTask2.getCreatedOn()) < 0 ? myTask2 : myTask);
                Optional<MyTask> myTask2MaxOptional = myTaskList2.stream().reduce((myTask, myTask2) -> myTask.getCreatedOn().compareTo(myTask2.getCreatedOn()) < 0 ? myTask2 : myTask);

                return myTask2MaxOptional.get().getCreatedOn().compareTo(myTask1MaxOptional.get().getCreatedOn());
            });
    }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.