Commits

Micha Kops committed 9f0894e

lambdaj examples added.

Comments (0)

Files changed (3)

-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <groupId>com.hascode.tutorial</groupId>
-  <artifactId>functionaljava-example</artifactId>
-  <version>0.0.1</version>
-  <dependencies>
-  	<dependency>
-  		<groupId>org.functionaljava</groupId>
-  		<artifactId>functionaljava</artifactId>
-  		<version>3.1</version>
-  	</dependency>
-  	<dependency>
-  		<groupId>com.google.guava</groupId>
-  		<artifactId>guava</artifactId>
-  		<version>12.0</version>
-  	</dependency>
-  </dependencies>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>functionaljava-example</artifactId>
+	<version>0.0.1</version>
+	<dependencies>
+		<dependency>
+			<groupId>org.functionaljava</groupId>
+			<artifactId>functionaljava</artifactId>
+			<version>3.1</version>
+		</dependency>
+		<dependency>
+			<groupId>com.google.guava</groupId>
+			<artifactId>guava</artifactId>
+			<version>12.0</version>
+		</dependency>
+		<dependency>
+			<groupId>com.googlecode.lambdaj</groupId>
+			<artifactId>lambdaj</artifactId>
+			<version>2.3.3</version>
+		</dependency>
+	</dependencies>
 </project>

src/main/java/com/hascode/tutorial/Book.java

+package com.hascode.tutorial;
+
+import java.util.Date;
+
+class Book {
+	private final String title;
+	private final Date published;
+	private float price;
+
+	public Book(final String title, final Date published) {
+		this.title = title;
+		this.published = published;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	public Date getPublished() {
+		return published;
+	}
+
+	public float getPrice() {
+		return price;
+	}
+
+	public void setPrice(final float price) {
+		this.price = price;
+	}
+}

src/main/java/com/hascode/tutorial/LambdajExamples.java

+package com.hascode.tutorial;
+
+import static ch.lambdaj.Lambda.forEach;
+import static ch.lambdaj.Lambda.joinFrom;
+import static ch.lambdaj.Lambda.on;
+import static ch.lambdaj.Lambda.sort;
+import static java.util.Arrays.asList;
+
+import java.util.Date;
+import java.util.List;
+
+public class LambdajExamples {
+	public static void main(final String... args) {
+		List<Book> books = asList(new Book("Some book", new Date(123)),
+				new Book("Another book", new Date(456)), new Book(
+						"I luv codin", new Date(789)));
+		// treating a collection like a single object
+		forEach(books).setPrice(10.0f); // sets the price on all objects
+		System.out.println(books.get(0).getPrice()); // output: 10.0
+		System.out.println(books.get(1).getPrice()); // output: 10.0
+		System.out.println(books.get(2).getPrice()); // output: 10.0
+
+		// sorting the books by publication date
+		List<Book> booksByDate = sort(books, on(Book.class).getPublished());
+		System.out.println(booksByDate.get(0).getTitle()); // "output: Some book"
+		System.out.println(booksByDate.get(1).getTitle()); // "output: Another book"
+		System.out.println(booksByDate.get(2).getTitle()); // "output: I luv coding"
+
+		// sorting the books by title
+		List<Book> booksByTitle = sort(books, on(Book.class).getTitle());
+		System.out.println(booksByTitle.get(0).getTitle()); // "output: Another book"
+		System.out.println(booksByTitle.get(1).getTitle()); // "output: I luv coding"
+		System.out.println(booksByTitle.get(2).getTitle()); // "output: Some book"
+
+		// create a comma separated list of titles from the books collection
+		String titles = joinFrom(books).getTitle();
+		System.out.println(titles); // "Some book, Another book, I luv codin"
+	}
+}