troubleshooting Question

Simplifying using lambdas

Avatar of Rohit Bajaj
Rohit BajajFlag for India asked on
Java
1 Comment1 Solution116 ViewsLast Modified:
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;
            }
        });
    }

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
ASKER CERTIFIED SOLUTION
mccarl
IT Business Systems Analyst / Software Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2015

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros