com.google.common.collect.Lists#transform使用注意

/**
* Returns a list that applies {@code function} to each element of {@code
* fromList}. The returned list is a transformed view of {@code fromList};
* changes to {@code fromList} will be reflected in the returned list and vice
* versa.
*
* <p>Since functions are not reversible, the transform is one-way and new
* items cannot be stored in the returned list. The {@code add},
* {@code addAll} and {@code set} methods are unsupported in the returned
* list.  --对List元素的修改也不支持
*
* <p>The function is applied lazily, invoked when needed. This is necessary
* for the returned list to be a view, but it means that the function will be
* applied many times for bulk operations like {@link List#contains} and
* {@link List#hashCode}. For this to perform well, {@code function} should be
* fast. To avoid lazy evaluation when the returned list doesn't need to be a
* view, copy the returned list into a new list of your choosing. --如果returnList不需要追踪fromList的变化,可以copy returnList to a new List
*
* <p>If {@code fromList} implements {@link RandomAccess}, so will the
* returned list. The returned list is threadsafe if the supplied list and
* function are.
*
* <p>If only a {@code Collection} or {@code Iterable} input is available, use
* {@link Collections2#transform} or {@link Iterables#transform}.
*
* <p><b>Note:</b> serializing the returned list is implemented by serializing
* {@code fromList}, its contents, and {@code function} -- <i>not</i> by
* serializing the transformed values. This can lead to surprising behavior,
* so serializing the returned list is <b>not recommended</b>. Instead,
* copy the list using {@link ImmutableList#copyOf(Collection)} (for example),
* then serialize the copy. Other methods similar to this do not implement
* serialization at all for this reason.
*/

 

public class ListReference {

    public static void main(String[] args) {
        List<UserEntity> userEntities = Lists.newArrayList();
        userEntities.add(new UserEntity(1, "x"));
        userEntities.add(new UserEntity(2, "y"));
        List<UserDto> userDtos = Lists.transform(userEntities, new Function<UserEntity, UserDto>() {
            public UserDto apply(UserEntity userEntity) {
                return new UserDto(userEntity.getId(), userEntity.getName());
            }
        });

        print(userDtos.iterator(), "before change return list... ");
        testReference(userDtos);
        print(userDtos.iterator(), "after change return list... ");

        for (UserEntity userEntity: userEntities) {
            userEntity.setName(userEntity.getName() + "_change");
        }

        print(userDtos.iterator(), "after change from list... ");
    }

    private static void testReference(List<UserDto> userDtos) {
        System.out.println("changing return list... ");
        for (UserDto userDto: userDtos) {
            userDto.setId(userDto.getId() + 100);
            System.out.println(ToStringBuilder.reflectionToString(userDto));
        }
        print(userDtos.iterator(), "after changing return list... ");
    }

    private static void print(Iterator iterator, String note) {
        System.out.println(note);
        while (iterator.hasNext()) {
            System.out.println(ToStringBuilder.reflectionToString(iterator.next()));
        }
    }

    static class UserEntity {
        private int id;
        private String name;

        UserEntity(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }

    static class UserDto {
        private int id;
        private String name;

        UserDto(){}

        UserDto(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }
}

  

输出:
before change return list... org.ryan.other.ListReference$UserDto@5debf305[id=1,name=x] org.ryan.other.ListReference$UserDto@52a9fd96[id=2,name=y] changing return list... org.ryan.other.ListReference$UserDto@1647ad40[id=101,name=x] org.ryan.other.ListReference$UserDto@3bbf502d[id=102,name=y] after changing return list... org.ryan.other.ListReference$UserDto@d28d900[id=1,name=x] org.ryan.other.ListReference$UserDto@74be95bf[id=2,name=y] after change return list... org.ryan.other.ListReference$UserDto@c596a7a[id=1,name=x] org.ryan.other.ListReference$UserDto@425d75eb[id=2,name=y] after change from list... org.ryan.other.ListReference$UserDto@5e8b957[id=1,name=x_change] org.ryan.other.ListReference$UserDto@71e001c8[id=2,name=y_change]

  

  

posted @ 2016-12-13 22:39  ryan.li  阅读(1979)  评论(0)    收藏  举报