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:
if (sumDoWhile == sumForLoop && sumForLoop == sumForEachLoop &&
ReplyDeletesumForEachLoop == 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
IMHO more elegant solution is (and true recursive - the "result" parameter is unnecessary):
ReplyDeleteprivate 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;}
}
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.
ReplyDeleteBeauti 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.
ReplyDeleteEscorts Service in Kolkata
Escorts in Kolkata
http://www.kolkataqueen.com/
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
ReplyDeleteEscorts in Gurgaon|Escorts Service in Gurgaon| Female Escorts in Gurgaon|Independent Escorts Service in Gurgaon|
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
ReplyDeleteEscorts in Gurgaon|Escorts Service in Gurgaon| Female Escorts in Gurgaon|Independent Escorts Service in Gurgaon|