Last active
August 29, 2015 14:04
-
-
Save ingramchen/f2ac2c1c96e8a8717c15 to your computer and use it in GitHub Desktop.
展示 Java 8 與 Java 7 兩者撰寫上差異
This file contains 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
package com.liquable.nemo.model; | |
import static java.util.Arrays.asList; | |
import static java.util.stream.Collectors.groupingBy; | |
import static java.util.stream.Collectors.summingLong; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import java.util.Set; | |
import java.util.function.Function; | |
import com.google.common.collect.ImmutableSet; | |
/** | |
* 主要有兩個方法 <code>java7_imperative()</code> 和 <code>java8_functional()</code>,分別展示 | |
* imperative 與 functional 風格的寫法。 | |
* | |
* 程式的目的在求誰是最多人共有的朋友。 | |
* | |
* @author ingram | |
* | |
*/ | |
public class UserTest { | |
/** | |
* Java 7 external iteration, 共 21 行。 | |
*/ | |
public void java7_imperative(List<User> users) { | |
final Map<Friend, Long> friendCounts = new HashMap<>(); | |
for (final User user : users) { | |
for (final Friend friend : user.getFriends()) { | |
if (!friendCounts.containsKey(friend)) { | |
friendCounts.put(friend, 1L); | |
} else { | |
friendCounts.put(friend, friendCounts.get(friend) + 1); | |
} | |
} | |
} | |
Friend topFriend = null; | |
long max = -1; | |
for (final Entry<Friend, Long> entry : friendCounts.entrySet()) { | |
if (entry.getValue() > max) { | |
topFriend = entry.getKey(); | |
max = entry.getValue(); | |
} | |
} | |
if (topFriend != null) { | |
userDao.saveMostCommonFriend(topFriend); | |
} | |
} | |
/** | |
* Java 8 internal iteration, 共 6 行。 | |
*/ | |
public void java8_functional(List<User> users) { | |
users.stream().flatMap(user -> user.getFriends().stream()) | |
.collect(groupingBy(Function.identity(), summingLong(friend -> 1))) | |
.entrySet().stream() // Entry<Friend, Long> friendCount | |
.max(Entry.comparingByValue()) | |
.map(Entry::getKey) | |
.ifPresent(userDao::saveMostCommonFriend); | |
} | |
public static void main(String[] args) { | |
final Friend[] f = { new Friend("a"), new Friend("b"), new Friend("c"), new Friend("d") }; | |
final User[] users = new User[6]; | |
users[0] = new User(f[0], f[1]); | |
users[1] = new User(f[1], f[2]); | |
users[2] = new User(f[3], f[2]); | |
users[3] = new User(f[0], f[1], f[2], f[3]); | |
users[4] = new User(f[1]); | |
users[5] = new User(); | |
new UserTest().java7_imperative(asList(users)); | |
new UserTest().java8_functional(asList(users)); | |
} | |
UserDao userDao = new UserDao(); | |
} | |
class Friend { | |
String name; | |
Friend(String name) { | |
this.name = name; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) return true; | |
if (obj == null) return false; | |
if (getClass() != obj.getClass()) return false; | |
final Friend other = (Friend) obj; | |
if (name == null) { | |
if (other.name != null) return false; | |
} else if (!name.equals(other.name)) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
return ((name == null) ? 0 : name.hashCode()); | |
} | |
@Override | |
public String toString() { | |
return name; | |
} | |
} | |
class User { | |
Set<Friend> friends; | |
User(Friend... friends) { | |
this.friends = ImmutableSet.copyOf(friends); | |
} | |
Set<Friend> getFriends() { | |
return friends; | |
} | |
} | |
class UserDao { | |
// ... do database save... | |
public void saveMostCommonFriend(Friend friend) { | |
System.out.println("saved: " + friend); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment