从地图中的对象中删除某些元素

我有一个对象地图


Map<Integer, User>其中用户的 id 映射到具有 id、firstName、lastName、Name、email、zipCode、country、state 的 User 对象


如何将其简化为只有 id 和 name 的 Map,其他用户信息无关紧要。


- 编辑


抱歉,我的问题不清楚,我基本上想从


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


0 : {id: 0, name: 'test0', country: 'us'},

1 : {id: 1, name: 'test1', country: 'us'},

2 : {id: 2, name: 'test2', country: 'us'}

我还有一个包含所有用户属性的 User 类和一个只有 id、name 和 country 的 UserV2 类


qq_花开花谢_0
浏览 96回答 2
2回答

白衣非少年

使用 aStream来避免临时状态。final Map<String, String> output =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;input.entrySet()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o -> o.getKey(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o -> o.getValue().getName()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));Collectors.toMap接受两个功能接口作为输入参数toMap(Function<? super T, ? extends K> keyMapper,&nbsp; // Returns the new key, from the input Entry&nbsp; &nbsp; &nbsp; Function<? super T, ? extends U> valueMapper // Returns the new value, from the input Entry) { ... }要处理该用例,您需要创建一个新的、简化的用户表示。public class SimpleUser {&nbsp; &nbsp; public final String id;&nbsp; &nbsp; public final String name;&nbsp; &nbsp; public final String country;&nbsp; &nbsp; private SimpleUser(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String country) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.country = countr;&nbsp; &nbsp; }&nbsp; &nbsp; public static SimpleUser of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String country) {&nbsp; &nbsp; &nbsp; &nbsp; return new SimpleUser(id, name, country);&nbsp; &nbsp; }}比你刚刚.collect(Collectors.toMap(&nbsp; &nbsp; &nbsp; &nbsp;o -> o.getKey(),&nbsp; &nbsp; &nbsp; &nbsp;o -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final User value = o.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return SimpleUser.of(user.getId(), user.getName(), user.getCountry());&nbsp; &nbsp; &nbsp; &nbsp;}));

倚天杖

这个答案使用Java Streams。该collect方法可以接受一个Collector. 这个取每一(Integer, User)对并创建一(Integer, UserV2)对。Map<Integer, UserV2> userIdToUserV2 = users.entrySet().stream()&nbsp; &nbsp; // Map (Integer, User) -> (Integer, UserV2)&nbsp; &nbsp; .collect(Collectors.toMap(&nbsp; &nbsp; &nbsp; &nbsp; // Use the same Integer as the map key&nbsp; &nbsp; &nbsp; &nbsp; Map.Entry::getKey,&nbsp; &nbsp; &nbsp; &nbsp; // Build the new UserV2 map value&nbsp; &nbsp; &nbsp; &nbsp; v -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User u = v.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create a UserV2 from the User values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new UserV2(u.getId(), u.getName(), u.getCountry());&nbsp; &nbsp; &nbsp; &nbsp; }));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java