Skip to content

Instantly share code, notes, and snippets.

@matthewjackowski
Created December 28, 2015 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjackowski/2d703440ceaa017569bf to your computer and use it in GitHub Desktop.
Save matthewjackowski/2d703440ceaa017569bf to your computer and use it in GitHub Desktop.
Refactored For-Loop to Lambda in Java
package com.brilliantcoding;
public class Message {
int type;
public Message(int type){
this.type = type;
}
public String toString() {
switch (this.type) {
case 0:
return("This is Message A");
case 1:
return("This is Message B");
case 2:
return("This is Message C");
}
return "Type not found!";
}
}
import java.util.*;
import com.brilliantcoding.Message;
public class RefactoredForList {
public static void main(String[] args) {
Message[] messageArray = {new Message(0), new Message(1), new Message(2)};
List<Message> messages = Arrays.asList(messageArray);
messages.forEach(System.out::println); // method reference
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment