1

Consider this function which sums the results of a range of values passed to a function...

int totalColumnWidth = 0;
for(int columnIndex = 0; columnIndex < columnCount; columnIndex++)
    totalColumnWidth += getWidthForColumn(columnIndex);

It just seems so verbose! In other languages, I can do that in one line, which has the added benefit of allowing me to make totalColumnWidth read-only. Is there any such thing in Java 7 (we're doing Android development for KitKat API 19).

My guess is 'no' since I understand Java 7 doesn't support lambdas which are needed for this type of functionality (you'd have to pass in the getWidthForColumn() call), but I'd love to be mistaken.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • 1
    You're correct. This is easy to create in **Java 8** but not in **Java 7**. – Zabuzard Oct 16 '17 at 18:49
  • I'm not convinced 3 lines of code is verbose (but I guess you are showing us a simplified version of your 'real' code)! Anyway, to answer your question, Sorry no - you need Java 8 for this – Stormcloud Oct 16 '17 at 18:50
  • or Kotlin, which is quite popular on Android, for that reason among others, and runs on Java 6. – JB Nizet Oct 16 '17 at 18:51
  • There are some libraries that can do that see https://stackoverflow.com/questions/30055585/java-streams-in-java-7 and you can find some more with a simple google 'java 7 streams' – Oleg Oct 16 '17 at 18:51
  • Someone please put this in an answer so I can mark it as closed. :) – Mark A. Donohoe Oct 16 '17 at 18:52
  • I flagged as duplicate, you can close it with that if you like. An answer would be link only. – Oleg Oct 16 '17 at 18:54

1 Answers1

0

You are correct. Java 7 does not support such functionalities by default. It can easily be created with Java 8 though.

However there are some libraries which provide similar methods or emulate lambdas. Therefore also see SO: java streams in Java 7 (as stated by @Oleg).

Zabuzard
  • 25,064
  • 8
  • 58
  • 82