Skip to content

Instantly share code, notes, and snippets.

@mikaelhg
Created April 30, 2014 16:02
Show Gist options
  • Save mikaelhg/4c1543ecfdd88362f46b to your computer and use it in GitHub Desktop.
Save mikaelhg/4c1543ecfdd88362f46b to your computer and use it in GitHub Desktop.
Java 8 lambda streams come in handy when parsing HTML with Jsoup and Lombok @builders
Document doc = Jsoup.parse(...);
doc.select("form#login").stream()
.map(e -> e.attr("action"))
.map(action -> find(action, LINK_ID_PATTERN).group(1))
.filter(Objects::nonNull)
.findFirst()
.ifPresent(builder::id);
content.select("div#datePublished").stream()
.map(Element::text)
.map(SiteScraper::parseLocalDateTime)
.filter(Optional::isPresent).map(Optional::get)
.findFirst().ifPresent(builder::published);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment