简体   繁体   中英

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 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM