I have requirement that to read data from properties file which is command separated which will be obviously in string form. So after that I am splitting it into string array.
I have requirement to convert that string array to integer list.
I have tried 2 ways:
- Traditional for each loop with casting,
- use java 8 stream.
Then I think I have to calculate the time which one is good as per performance. So I have some rough data.
Code:
public class PropertyLoadCSV { private static Properties properties; public static void main(String[] args) { try { properties = new Properties(); properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/csvfile.properties")); String ids = properties.getProperty("ids"); String[] splitedIDs = ids.split(","); long startTime = System.nanoTime(); convertUsingJava7(splitedIDs); long endTime = System.nanoTime(); long duration = (endTime - startTime); System.out.println(duration); long startTime1 = System.nanoTime(); convertUsingJava8(splitedIDs); long endTime1 = System.nanoTime(); long duration1 = (endTime1 - startTime1); System.out.println(duration1); } catch (Exception e) { e.printStackTrace(); } } static List<Integer> convertUsingJava7(String[] splited){ List<Integer> list = new ArrayList<>(); for(String s: splited){ list.add(Integer.valueOf(s)); } return list; } static List<Integer> convertUsingJava8(String[] splited){ return Stream.of(splited).map(Integer::parseInt).collect(Collectors.toList()); } } got some unexpected result in console:
131601
254094088
so really java 8 stream convert and cast slowly then tradition way ?
Which one I have to use as performance concern ?
Here's working example for it.