简体   繁体   English

如何获得List <Y> 来自地图 <X,Y> 通过提供List <X> 使用谷歌收藏?

[英]How to get List<Y> from Map<X,Y> by providing List<X> using google collection?

I have a Map<X, Y> and List<X> , I would like to extract the values from Map<X, Y> by providing the List<X> , which will result in List<Y> . 我有一个Map<X, Y>List<X> ,我想通过提供List<X>Map<X, Y>提取值,这将导致List<Y> One way to do this is 一种方法是

List<X> keys = getKeys();  
Map<X, Y> map = getMap();  
List<Y> values = Lists.newArrayListWithCapacity(keys.size());  
for(X x : keys){  
   values.add(map.get(x));  
}

Now again I need to remove nulls in values ( List<Y> ) by using Predicate or something. 现在我需要使用Predicate或其他东西删除值( List<Y> )中的空值。 Any better way to do this? 有更好的方法吗?
Is there any good reason why this method isn't there in google collections library? 谷歌馆藏库中没有这种方法有什么好的理由吗?

Something like this: 像这样的东西:

List<Y> values = Lists.newArrayList(
    Iterables.filter(
        Lists.transform(getKeys(), Functions.forMap(getMap(), null), 
        Predicates.notNull()));

Same concept as axtavt's answer , but using FluentIterable (reads sequentially instead of inside-out): axtavt的答案相同的概念,但使用FluentIterable (顺序读取而不是从里到外读取):

List<Y> values = FluentIterable
    .from(keys)
    .transform(Functions.forMap(map, null))
    .filter(Predicates.notNull())
    .toList();

@axtavt's answer would probably be the most efficient given your exact requirements. 鉴于您的确切要求, @ axtavt的答案可能是最有效的。 If your List of keys were instead a Set , something like this would be my preference: 如果您的密钥List是一个Set ,那么这样的东西将是我的偏好:

List<Y> values = Lists.newArrayList(
    Maps.filterKeys(getMap(), Predicates.in(getKeys())).values());

I think a special method for doing this isn't in Guava because it's not generally useful enough. 我认为这样做的一种特殊方法不是在番石榴中,因为它通常不够用。 Plus, as you can see there are ways of composing things Guava does provide to achieve this. 另外,正如您所看到的,有很多方法可以组合Guava为实现这一目标而提供的功能。 Guava focuses on providing building blocks, with specific methods for operations that are very common. Guava专注于提供构建模块,具有非常常见的特定操作方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java8:如何转换地图 <X, List<Y> &gt;到地图 <Y,X> 使用Stream? - Java8: How to Convert Map<X, List<Y>> to Map<Y,X> using Stream? 如何从Observable转换 <List<X> &gt;至可观察 <List<Y> &gt;,使用以X作为参数并返回Observable的函数 <Y> - How to convert from Observable<List<X>> to Observable<List<Y>>, using a function that takes X as parameter & returns Observable<Y> 获取在笛卡尔坐标上表示x,y的值的列表 - get a list of values to represent x,y on Cartesian 如何使用x,y坐标列表绘制曲线(峰值) - How to Drawing a curve line(peaks) using list of x,y coordinates 如何转换地图<X, Map<Y,Z> &gt; 到地图<Y, Map<X,Z> &gt; 使用 Java8 流 - How to convert Map<X, Map<Y,Z>> to Map<Y, Map<X,Z>> using Java8 stream 如何从给定的宽度和高度的有序1d列表-0,1,2,3,4,5,6…中获取2d坐标(x,y)? - How to get the 2d coordinates(x,y) from an ordered 1d list - 0,1,2,3,4,5,6… with given width & height? 如何序列化类型X的列表,填充Y类型的对象,它扩展X,并在反序列化时仍然获得Y元素? - How to serialize a list of type X, filled with objects of type Y, which extends X, and still get Y elements upon deserialization? 如何创建接口“X”扩展列表接口并使用抽象 class Y 和 ArrayList 实现 X - How to create a interface “X” extending List interface and implement X using a abstract class Y with ArrayList Java从字符串获取XY坐标图 - Java Get Map Of X-Y Coordinates From String 使用x,y坐标点列表绘制曲线峰线 - Drawing curve line peaks using list of x,y coordinates points
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM