Filtering list using stream take long time

Viewed 30

I have list of tasks about 700 task or more i am trying to search by taskId using below code. it works but it takes a lot of time about 1 minute. list is not sorted

List<Tasks> tasks = ....;
tasks = tasks
        .stream()
        .filter(task -> task.getTaskId().equals("1000"))
        .collect(Collectors.toList());

looping tasks take the same time almost

How to enhance my code?

Regards

1 Answers

On my system, the following example prints: Filtering 700 records took 10210465ns and found 69 matches. So that's 10ms compared to your 1 minute. (And the JIT compiler wouldn't have a chance to fully kick in.)

Use a profiler to find out where the time is spent in your application. I suspect your getTaskID method is doing more than you think.

public class Filter {
    static Random rng = new Random();

    static record Task(String id){};
    
    public static void main(String[] args) {
        int howMany = 700;
        List<Task> tasks = makeTasks(howMany, 0.1f);
        long before = System.nanoTime();
        tasks = tasks
                .stream()
                .filter(task -> task.id().equals("1000"))
                .collect(Collectors.toList());
        long after = System.nanoTime();
        System.out.println("Filtering "+howMany+" records took "+(after-before)+"ns and found "+tasks.size()+" matches.");
    }
    
    static List<Task> makeTasks(int howMany, float probabilityOfMatch) {
        var list = new ArrayList<Task>(howMany);
        for (int i = 0; i < howMany; i++) {
            var id = Integer.toString((rng.nextFloat() < probabilityOfMatch ? 1000 : i));
            list.add(new Task(id));
        }
        Collections.shuffle(list);
        return list;
    }
}
Related