Skip to content

Instantly share code, notes, and snippets.

@ashleywxwx
Created May 12, 2014 23:44
Show Gist options
  • Select an option

  • Save ashleywxwx/b98c9b584b90adc45fa6 to your computer and use it in GitHub Desktop.

Select an option

Save ashleywxwx/b98c9b584b90adc45fa6 to your computer and use it in GitHub Desktop.
jReddit and Java 8 Wordcount
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