lambda example

This commit is contained in:
eugenp 2013-12-25 14:22:15 +02:00
parent 1a12a44cb1
commit 9543f1f067
6 changed files with 206 additions and 176 deletions

View File

@ -1,4 +1,3 @@
#Sat Jan 21 23:04:06 EET 2012
eclipse.preferences.version=1
editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
sp_cleanup.add_default_serial_version_id=true
@ -14,6 +13,7 @@ sp_cleanup.always_use_blocks=true
sp_cleanup.always_use_parentheses_in_expressions=true
sp_cleanup.always_use_this_for_non_static_field_access=false
sp_cleanup.always_use_this_for_non_static_method_access=false
sp_cleanup.convert_functional_interfaces=false
sp_cleanup.convert_to_enhanced_for_loop=true
sp_cleanup.correct_indentation=true
sp_cleanup.format_source_code=true
@ -46,8 +46,10 @@ sp_cleanup.remove_unused_private_methods=true
sp_cleanup.remove_unused_private_types=true
sp_cleanup.sort_members=false
sp_cleanup.sort_members_all=false
sp_cleanup.use_blocks=false
sp_cleanup.use_anonymous_class_creation=false
sp_cleanup.use_blocks=true
sp_cleanup.use_blocks_only_for_return_and_throw=false
sp_cleanup.use_lambda=false
sp_cleanup.use_parentheses_in_expressions=false
sp_cleanup.use_this_for_non_static_field_access=true
sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true

View File

@ -1,4 +1,5 @@
<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">
<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>org.baeldung</groupId>
<artifactId>spring-rest</artifactId>
@ -13,7 +14,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
<version>16.0-rc1</version>
</dependency>
<dependency>
@ -22,6 +23,12 @@
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<!-- web -->
<!-- marshalling -->

View File

@ -1,15 +0,0 @@
package org.baeldung.java;
import org.junit.Test;
public class CoreJava8UnitTest {
// tests -
@Test
public final void when_thenCorrect() {
final Runnable r2 = () -> System.out.println("Hello world two!");
r2.run();
}
}

View File

@ -1,52 +0,0 @@
package org.baeldung.java;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.collections4.ListUtils;
import org.junit.Test;
import com.google.common.collect.ImmutableList;
public class CoreJavaUnitTest {
// tests -
@Test
public final void givenUsingTheJdk_whenArrayListIsSynchronized_thenCorrect() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> synchronizedList = Collections.synchronizedList(list);
System.out.println("Synchronized List is: " + synchronizedList);
}
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingTheJdk_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = Collections.unmodifiableList(list);
unmodifiableList.add("four");
}
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingGuava_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = ImmutableList.copyOf(list);
unmodifiableList.add("four");
}
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingGuavaBuilder_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final ImmutableList<Object> unmodifiableList = ImmutableList.builder().addAll(list).build();
unmodifiableList.add("four");
}
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingCommonsCollections_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = ListUtils.unmodifiableList(list);
unmodifiableList.add("four");
}
}

View File

@ -0,0 +1,24 @@
package org.baeldung.java8;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import java.util.Collections;
import java.util.List;
import org.baeldung.java8.entity.Human;
import org.junit.Test;
import com.google.common.collect.Lists;
public class Java8ComparatorUnitTest {
// tests -
@Test
public final void when_thenCorrect() {
final List<Human> humans = Lists.newArrayList(new Human(randomAlphabetic(5)), new Human(randomAlphabetic(5)));
Collections.sort(humans, (final Human h1, final Human h2) -> h1.getName().compareTo(h2.getName()));
System.out.println(humans);
}
}

View File

@ -0,0 +1,64 @@
package org.baeldung.java8.entity;
public class Human {
private String name;
public Human() {
super();
}
public Human(final String name) {
super();
this.name = name;
}
// API
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
//
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Human other = (Human) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Human [name=").append(name).append("]");
return builder.toString();
}
}