Removing certain elements from an Object in a Map

问题: I have an object Map Map<Integer, User> where the user's id's are mapped to User object that has id, firstName, lastName, Name, email, zipCode, country, state How...

问题:

I have an object Map

Map<Integer, User> where the user's id's are mapped to User object that has id, firstName, lastName, Name, email, zipCode, country, state

How do I reduce it to a Map that has only id and name, the other user info is irrelevant.

--EDIT

sorry, I wasn't clear in my question, I basically want to go from

0 : {id: 0, name: 'test0', country: 'us', firstName: 'ft0', lastName: 'lt0'},
1 : {id: 1, name: 'test1', country: 'us', firstName: 'ft1', lastName: 'lt1'},
2 : {id: 2, name: 'test2', country: 'us', firstName: 'ft2', lastName: 'lt2'}

to

0 : {id: 0, name: 'test0', country: 'us'},
1 : {id: 1, name: 'test1', country: 'us'},
2 : {id: 2, name: 'test2', country: 'us'}

Also I have a User class that has all the user properties and a UserV2 class that has just id, name and country


回答1:

Use a Stream to avoid temporary state.

final Map<String, String> output = 
           input.entrySet()
                .stream()
                .collect(Collectors.toMap(
                    o -> o.getKey(),              
                    o -> o.getValue().getName()
                ));

Collectors.toMap accepts two functional interfaces as input parameters

toMap(Function<? super T, ? extends K> keyMapper,  // Returns the new key, from the input Entry
      Function<? super T, ? extends U> valueMapper // Returns the new value, from the input Entry
) { ... }

To handle that usecase, you need to create a new, simplified, representation of the user.

public class SimpleUser {
    public final String id;
    public final String name;
    public final String country;

    private SimpleUser(
            final String id,
            final String name,
            final String country) {
        this.id = id;
        this.name = name;
        this.country = countr;
    }

    public static SimpleUser of(
            final String id,
            final String name,
            final String country) {
        return new SimpleUser(id, name, country);
    }
}

Than you just

.collect(Collectors.toMap(
       o -> o.getKey(),
       o -> {
          final User value = o.getValue();
          return SimpleUser.of(user.getId(), user.getName(), user.getCountry());
       }
));

回答2:

This answer uses Java Streams. The collect method can accept a Collector. This one takes each (Integer, User) pair and creates an (Integer, UserV2) pair.

Map<Integer, UserV2> userIdToUserV2 = users.entrySet().stream()
    // Map (Integer, User) -> (Integer, UserV2)
    .collect(Collectors.toMap(
        // Use the same Integer as the map key
        Map.Entry::getKey,
        // Build the new UserV2 map value
        v -> {
            User u = v.getValue();
            // Create a UserV2 from the User values
            return new UserV2(u.getId(), u.getName(), u.getCountry());
        }));
  • 发表于 2019-02-20 07:23
  • 阅读 ( 200 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除