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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.