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