Skip to content

Instantly share code, notes, and snippets.

@ingramchen
Last active August 29, 2015 14:04
Show Gist options
  • Save ingramchen/f2ac2c1c96e8a8717c15 to your computer and use it in GitHub Desktop.
Save ingramchen/f2ac2c1c96e8a8717c15 to your computer and use it in GitHub Desktop.

Revisions

  1. ingramchen revised this gist Jul 18, 2014. 1 changed file with 29 additions and 20 deletions.
    49 changes: 29 additions & 20 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -14,31 +14,20 @@
    import com.google.common.collect.ImmutableSet;

    /**
    * 主要有兩個方法 java7Imperative() 和 java8() ,分別展示 imperative 與 functional 風格的寫法。
    * 主要有兩個方法 <code>java7_imperative()</code> 和 <code>java8_functional()</code>,分別展示
    * imperative 與 functional 風格的寫法。
    *
    * 程式的目的在求誰是最多人共有的朋友。
    *
    * @author ingram
    *
    */
    public class UserTest {

    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().java7Imperative(asList(users));
    new UserTest().java8(asList(users));
    }

    private final UserDao userDao = new UserDao();

    public void java7Imperative(List<User> users) {
    /**
    * 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()) {
    @@ -62,14 +51,34 @@ public void java7Imperative(List<User> users) {
    }
    }

    public void java8(List<User> users) {
    /**
    * 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 {
    @@ -121,4 +130,4 @@ class UserDao {
    public void saveMostCommonFriend(Friend friend) {
    System.out.println("saved: " + friend);
    }
    }
    }
  2. ingramchen created this gist Jul 18, 2014.
    124 changes: 124 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    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;

    /**
    * 主要有兩個方法 java7Imperative() 和 java8() ,分別展示 imperative 與 functional 風格的寫法。
    * 程式的目的在求誰是最多人共有的朋友。
    *
    * @author ingram
    *
    */
    public class UserTest {

    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().java7Imperative(asList(users));
    new UserTest().java8(asList(users));
    }

    private final UserDao userDao = new UserDao();

    public void java7Imperative(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);
    }
    }

    public void java8(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);
    }
    }

    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);
    }
    }