Created
May 12, 2014 23:44
-
-
Save ashleywxwx/b98c9b584b90adc45fa6 to your computer and use it in GitHub Desktop.
jReddit and Java 8 Wordcount
This file contains hidden or 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.List; | |
| import com.github.jreddit.user.Comment; | |
| import com.github.jreddit.user.User; | |
| public class Main { | |
| public static void main(String[] args) throws Exception { | |
| // Provide user account for credentials | |
| User user = new User("username","password"); | |
| user.connect(); | |
| // Fetch list of 25 recent comments | |
| List<Comment> comments = user.comments(); | |
| // Java 8 \o/ | |
| int count = comments.stream() // open as stream | |
| .map(i -> i.getComment().split(" ").length) // for each instance of a comment (i), | |
| // split into string array, by " " delimiter, and return the count as a new stream | |
| .reduce(0, (x,y) -> x + y); // reduce by adding x (n) and y (n+1) | |
| // Display your results! | |
| System.out.println("Word count: " + count); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment