Saturday, May 23, 2015

Fun with Loops - 7 ways to do loops in Java 8

This is more of a coding stretch than a challenge.

Write functions that compute the sum of the numbers in a given list using a for-loop, for each loop, new Java 8 streams, a while-loop, and recursion.



public class Main {

 public static void main(String[] args) throws Exception {
  List<Integer> numbers = new ArrayList<>(
      Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
  
  
  int sumJavaStream = sumJavaStream(numbers);
  int sumForLoop = sumForLoop(numbers);
  int sumForEachLoop = sumForEachLoop(numbers);
  int sumDoWhile = sumWhile(numbers);
  int sumRecursion = sumRecursion(numbers);
  if (sumDoWhile == sumForLoop && sumForLoop == sumForEachLoop && 
    sumForEachLoop == sumDoWhile && sumDoWhile == sumRecursion) {
   System.out.println(sumJavaStream);
  }
  
 }

 private static int sumJavaStream(List<Integer> numbers) {
  return numbers.stream().mapToInt(num -> num.intValue()).sum();
 }
 
 private static int sumForLoop(List<Integer> numbers) {
  int result = 0;
  for (int i = 0; i < numbers.size(); i++) {
   result += numbers.get(i).intValue();
  }
  return result;
 }
 
 private static int sumForEachLoop(List<Integer> numbers) {
  int result = 0;
  for (Integer number : numbers) {
   result += number.intValue();
  }
  return result;
 }

 private static int sumWhile(List<Integer> numbers) {
  int result = 0;
  int i = 0;
     while (i < numbers.size()) {
      result += numbers.get(i).intValue();
      i++;
     }
  return result;  
 }
 
 private static int sumRecursion(List<Integer>> numbers) {
  return sumRecursion(numbers, 0, 0); 
 }
 
 private static int sumRecursion(List<Integer> numbers, int index, int sum) {
  int result = sum;
  if (index < numbers.size()) {
   result += numbers.get(index);
   result = sumRecursion(numbers, index + 1, result);
  }
  return result; 
 }
 
}



Related Content:

7 comments:

  1. if (sumDoWhile == sumForLoop && sumForLoop == sumForEachLoop &&
    sumForEachLoop == sumDoWhile && sumDoWhile == sumRecursion) {
    System.out.println(sumJavaStream);
    }

    IF Do == For && For == ForEach THEN Do == ForEach (this comparison is unnecessary).
    BUT if all four sums (Do, For, ForEach, Recursion) are equal - the FIFTH (JavaStream) is printed: which was NOT compared to them

    ReplyDelete
  2. IMHO more elegant solution is (and true recursive - the "result" parameter is unnecessary):

    private static int sumRecursion(List numbers) {
    return sumRecursion(numbers, 0);
    }

    private static int sumRecursion(List numbers, int index) {
    if (index < numbers.size()) {return numbers.get(index) + sumRecursion(numbers, index + 1); }
    else { return 0;}
    }

    ReplyDelete
  3. It was really fun doing loops by 8 different methods.Thanks for introducing all those methods as i was kept busy in doing so with a single method.

    ReplyDelete
  4. Beauti Queen amazing kolkata escorts agency alwyas ready for you at your doorstep. Beauti Queen provide you high profile independent girl in kolkata, escorts service in kolkata, kolkata escorts. If you are looking for the ultimate encounter in Kolkata then I am your sophisticated young Kolkata escorts that provides upscale companionship for making your night becomes true.
    Escorts Service in Kolkata
    Escorts in Kolkata
    http://www.kolkataqueen.com/

    ReplyDelete
  5. Gurgaon divas ( Gurgaon Escorts ) have most beautiful,attractive and highly educated call girls. she gives 100% satisfactions with best service. Their hot & sexy young females across the city who are 24 hours ready to serve their erotic services for you.Book me now- http://gurgaondivas.co.in
    Escorts in Gurgaon|Escorts Service in Gurgaon| Female Escorts in Gurgaon|Independent Escorts Service in Gurgaon|

    ReplyDelete
  6. Gurgaon divas ( Gurgaon Escorts ) have most beautiful,attractive and highly educated call girls. she gives 100% satisfactions with best service. Their hot & sexy young females across the city who are 24 hours ready to serve their erotic services for you.Book me now- http://gurgaondivas.co.in
    Escorts in Gurgaon|Escorts Service in Gurgaon| Female Escorts in Gurgaon|Independent Escorts Service in Gurgaon|

    ReplyDelete