Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

Performance issue while iterating with streams

Hi Experts,

I have written the below program using java7 and java8

Collections are giving me result quickly when i compared with streams
Is any thing wrong in my code.
can some one suggest me what exactly the issue with streams iteration.

Collections is taking only 16 milliseconds
FROM STREAMS
localCaloricDishesName  [EggCurry, BeatRootCurry, LadyFinger]
109 millis

package com.java8;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class StreamAndFilter  {

	public static void main1(String[] args) {
		Long startTime = System.currentTimeMillis();	
		List<Dish> menu = new ArrayList<>();
		menu.add(new Dish("CarrotCurry",405));
		menu.add(new Dish("BeatRootCurry",215));
		menu.add(new Dish("PotatosCurry",450));
		menu.add(new Dish("TamatoCurry",500));
		menu.add(new Dish("LadyFinger",300));
		menu.add(new Dish("EggCurry",200));
		
		List<Dish> localCaloricDishesName = new ArrayList<>();
		
		for (Dish dish : menu) {
			if(dish.getCalories()<400){
				localCaloricDishesName.add(dish);
			}
		}
		
		Collections.sort(localCaloricDishesName,new Comparator<Dish>() {

			@Override
			public int compare(Dish o1, Dish o2) {
				// TODO Auto-generated method stub
				return Integer.compare(o1.getCalories(), o2.getCalories());
			}
			
		});
		
		List<String> curryNames = new ArrayList<String>();
		for (Dish dish : localCaloricDishesName) {
			curryNames.add(dish.getDishName());
		}
		
		System.out.println("curryNames"+curryNames);;
		System.out.println(System.currentTimeMillis()-startTime);
	}
	
	public static void main(String[] args) {
		System.out.println("FROM STREAMS");
		Long startTime = System.currentTimeMillis();
		List<Dish> menu = new ArrayList<>();
		menu.add(new Dish("CarrotCurry",405));
		menu.add(new Dish("BeatRootCurry",215));
		menu.add(new Dish("PotatosCurry",450));
		menu.add(new Dish("TamatoCurry",500));
		menu.add(new Dish("LadyFinger",300));
		menu.add(new Dish("EggCurry",200));
		
		List<String> localCaloricDishesName = 
				menu.stream()
				.filter(d->d.getCalories()<400)
				.sorted(Comparator.comparing(Dish :: getCalories))
				.map(Dish::getDishName)
				.collect(Collectors.toList());
		
		System.out.println("localCaloricDishesName  "+localCaloricDishesName);
		System.out.println(System.currentTimeMillis()-startTime);		
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srikotesh
srikotesh

ASKER

Hi CPColin,

IF possible can you show me a sample example which improves performance.
Nope. To reiterate what I said in my last comment: Your code is fine; streams just have more overhead.
Memory wise streams are better than collection
Performance wise stream slower than collection
AM I correct?
It depends entirely on what you're doing. My standard advice is that you should focus on writing code that clearly shows your intent and worry about performance if and when it becomes a problem.