使用谷歌收藏过滤和排序列表 [英] filter and sort list using google collections

查看:156
本文介绍了使用谷歌收藏过滤和排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个列表(或Set):

  List< String> testList = Lists.newArrayList(assocX,srcT,destA,srcX,不关心Y,垃圾,srcB); 

我想找回一个ImmutableList(Set),它按照自然顺序排列/以src开始的是第一个,assoc第二个和dest最后一个。如果一个术语不包含那些,那么它应该从结果列表中删除。



因此,这里的结果是srcB,srcT,assocX, destA。



我想我可以用Iterables.filter或Predicates的组合来做到这一点,但是没有看到它。

编辑:代替列表的集合也是可行的。



谓词< String> filter = new Predicate< String>(){
@Override
public boolean apply(String input){
return input.startsWith(src)|| input.startsWith(assoc)|| input.startsWith( DEST);
}
};

函数< String,Integer> assignWeights = new Function< String,Integer>(){
@Override
public Integer apply(String from){
if(from.startsWith(src)){
返回0;
} else if(from.startsWith(assoc)){
return 1;
} else if(from.startsWith(dest)){
return 2;
} else {
/ *不应该是可能的,但必须做一些事情* /
抛出新的IllegalArgrumentException(从+不是一个有效的参数);
}
}
};

ImmutableList< String> onFileOf(assignWeights).sortedCopy(
Iterables.filter(testList,filter)

);

如果您开始添加更多前缀来过滤或排序,此解决方案绝对不会很好地扩展因为你必须不断更新过滤器和每个前缀的权重。


Suppose I have a list (or Set):

List<String> testList = Lists.newArrayList("assocX","srcT","destA","srcX", "don't care Y", "garbage", "srcB");

I would like to get back an ImmutableList(Set) that sorts/groups terms in natural order where terms that begin with "src" are first, "assoc" second and "dest" last. If a term does not contain those then it should be removed from the resulting list.

Therefore the result here is "srcB", "srcT", "assocX", "destA".

I think I can do this with some combination of Iterables.filter or Predicates but just not seeing it. There must be a succinct way of doing it I think.

EDIT: A set in place of a list works as well.

解决方案

As long as those three prefixes are the only things you care about, I'd suggest something like this:

    Predicate<String> filter = new Predicate<String>() {
        @Override
        public boolean apply(String input) {
            return input.startsWith("src") || input.startsWith("assoc") || input.startsWith("dest");
        }
    };

    Function<String, Integer> assignWeights = new Function<String, Integer>() {
        @Override
        public Integer apply(String from) {
            if (from.startsWith("src")) {
                return 0;
            } else if (from.startsWith("assoc")) {
                return 1;
            } else if (from.startsWith("dest")) {
                return 2;
            } else {
                /* Shouldn't be possible but have to do something */
                throw new IllegalArgrumentException(from + " is not a valid argument");
            }
        }
    };

    ImmutableList<String> sortedFiltered = ImmutableList.copyOf(
            Ordering.natural().onResultOf(assignWeights).sortedCopy(
                    Iterables.filter(testList, filter)
            )
    );

This solution definitely wouldn't scale out incredibly well if you start adding more prefixes to filter out or sort by, since you'd have to continually update both the filter and the weight of each prefix.

这篇关于使用谷歌收藏过滤和排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆