Created
December 28, 2015 08:40
-
-
Save matthewjackowski/2d703440ceaa017569bf to your computer and use it in GitHub Desktop.
Refactored For-Loop to Lambda in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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