`
chenjingbo
  • 浏览: 460658 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Guava总结4-杂七杂八

 
阅读更多

Splitter 和 Joiner

一个是自动分割,一个是合并.直接看代码

//用逗号隔开.
private static final String SPLITE = ",";

    private static final Joiner joiner = Joiner.on(SPLITE);

    private static final Splitter splitter = Splitter.on(SPLITE);

public void join(){
//将list自动变成以逗号分割组成的字符串
lotteryAwardMsgDO.setJuids(joiner.join(lotteryAwardMsgBO.getJuidList()));
}

public void splite{
//把一个字符串用逗号分隔开,然后自动转换成Iterable<String>
for(String juidStr: splitter.trimResults().omitEmptyStrings().split(lotteryAwardMsgDO.getJuids())){
            if(isNumeric(juidStr)){
                lotteryAwardMsgBO.addJuId(Long.parseLong(juidStr));
            }
        }
}
//比如 一个字符串是 “key1_value1:key2_value2”,可以这样
Map<String,String> map = Splitter.on(":").trimResults().withKeyValueSeparator("_").split("key1_value1:key2_value2");

 

 

列表的自动类型转换

比如将List<String> 类型转换成List<Long>

直接看例子.

@Override
        public List<BackCatClientTO> getChildren() {
            List<BackCategoryTO> children = backCategoryForest.getChildBackCats().get(categoryId);
            if(children == null || children.isEmpty()) return Collections.EMPTY_LIST;
            List<Long> categoryIds = Lists.transform(children,new Function<BackCategoryTO, Long>() {
                @Nullable
                @Override
                public Long apply(@Nullable BackCategoryTO backCategoryTO) {
                    return backCategoryTO.getCategory();
                }
            });
            return BackCatClientServiceImpl.this.build(categoryIds);
        }

 

筛选

比如某一个List中,把一些不符合要求的数据给筛选掉,直接看例子

注意,这个方法是非线程安全的.

Iterables.removeIf(frontCats, new Predicate<JuCategoryTO>() {
				@Override
				public boolean apply(@Nullable JuCategoryTO input) {
					return input.isHidden();
				}
			});

 

排序

直接看代码

Collections.sort(juCategoryTOs,new Comparator<JuCategoryTO>() {
            @Override
            public int compare(JuCategoryTO o1, JuCategoryTO o2) {
                //大的返回1,小的返回-1 .
                if(o1.getIndex() > o2.getIndex()){
                    return 1;
                } else if(o1.getIndex() < o2.getIndex()){
                    return -1;
                }else {
                    return 0;
                }
            }
        });

 双向map

public class VisitTypeMachine {

    private static final  BiMap<Integer,String> visitType2Name = HashBiMap.create();

    static {
        visitType2Name.put(1,"淘宝类目");
        visitType2Name.put(2,"直接访问");
        visitType2Name.put(3,"淘宝搜索");
        visitType2Name.put(4,"淘宝站内其他");
        visitType2Name.put(5,"店内浏览");
        visitType2Name.put(6,"QQ空间");
        visitType2Name.put(7,"阿里旺旺非广告");
        visitType2Name.put(8,"店铺收藏");
        visitType2Name.put(9,"购物车");
        visitType2Name.put(10,"淘宝客");
        visitType2Name.put(11,"淘宝站外其他");
        visitType2Name.put(12,"我的淘宝");
        visitType2Name.put(13,"直通车");
        visitType2Name.put(99,"未纳入");

    }

    public static String getNameByType(Integer type){
        return visitType2Name.get(type);
    }

    public static Integer getTypeByName(String name){
        Integer integer = visitType2Name.inverse().get(name);
        return integer;
    }
}

 

TreeRangeMap

经常会有一些需求,比如 0到60分是不及格,60到85分是良 85到100是优。如果不想写一堆if else 就可以试试用 TreeRangeMap

hashRangeMap = TreeRangeMap.create(); 
            hashRangeMap.put(Range.atMost(83268L), 0);
            hashRangeMap.put(Range.openClosed(83268L, 1998816L), 1);
            hashRangeMap.put(Range.openClosed(1998816L, 170404392L), 10);
            hashRangeMap.put(Range.openClosed(170404392L, 5899180011L), 100);
            hashRangeMap.put(Range.openClosed(5899180011L, 8184000062L), 300);
            hashRangeMap.put(Range.openClosed(8184000062L, 43657112106L), 700);
            hashRangeMap.put(Range.openClosed(43657112106L, 136910372737L), 1500);
            hashRangeMap.put(Range.openClosed(136910372737L, 698513762858L), 4000);
            hashRangeMap.put(Range.openClosed(698513762858L, 1396960220484L), 6000);
            hashRangeMap.put(Range.greaterThan(1396960220484L), 8000);

 

 

分享到:
评论

相关推荐

    guava-30.0-jre-API文档-中文版.zip

    赠送jar包:guava-30.0-jre.jar; 赠送原API文档:guava-30.0-jre-javadoc.jar; 赠送源代码:guava-30.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-30.0-jre.pom; 包含翻译后的API文档:guava-30.0-jre-...

    guava-27.0.1-jre-API文档-中文版.zip

    赠送jar包:guava-27.0.1-jre.jar; 赠送原API文档:guava-27.0.1-jre-javadoc.jar; 赠送源代码:guava-27.0.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-27.0.1-jre.pom; 包含翻译后的API文档:guava-...

    guava-25.0-jre-API文档-中文版.zip

    赠送jar包:guava-25.0-jre.jar; 赠送原API文档:guava-25.0-jre-javadoc.jar; 赠送源代码:guava-25.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-25.0-jre.pom; 包含翻译后的API文档:guava-25.0-jre-...

    guava-28.2-jre-API文档-中文版.zip

    赠送jar包:guava-28.2-jre.jar; 赠送原API文档:guava-28.2-jre-javadoc.jar; 赠送源代码:guava-28.2-jre-sources.jar; 赠送Maven依赖信息文件:guava-28.2-jre.pom; 包含翻译后的API文档:guava-28.2-jre-...

    guava-27.0-jre.jar

    guava-27.0-jre.jar

    guava-11.0.2-API文档-中文版.zip

    赠送jar包:guava-11.0.2.jar; 赠送原API文档:guava-11.0.2-javadoc.jar; 赠送源代码:guava-11.0.2-sources.jar; 赠送Maven依赖信息文件:guava-11.0.2.pom; 包含翻译后的API文档:guava-11.0.2-javadoc-API...

    guava-29.0-jre-API文档-中英对照版.zip

    赠送jar包:guava-29.0-jre.jar; 赠送原API文档:guava-29.0-jre-javadoc.jar; 赠送源代码:guava-29.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-29.0-jre.pom; 包含翻译后的API文档:guava-29.0-jre-...

    guava-20.0-API文档-中文版.zip

    赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...

    guava-27.1-jre.jar

    guava-27.1-jre

    guava-28.2-android.jar

    guava-28.2-android.jar

    最新版 guava-30.1-jre.jar

    最新版 guava-30.1-jre.jar

    guava-28.2-jre-API文档-中英对照版.zip

    赠送jar包:guava-28.2-jre.jar; 赠送原API文档:guava-28.2-jre-javadoc.jar; 赠送源代码:guava-28.2-jre-sources.jar; 赠送Maven依赖信息文件:guava-28.2-jre.pom; 包含翻译后的API文档:guava-28.2-jre-...

    guava-29.0-jre-API文档-中文版.zip

    赠送jar包:guava-29.0-jre.jar; 赠送原API文档:guava-29.0-jre-javadoc.jar; 赠送源代码:guava-29.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-29.0-jre.pom; 包含翻译后的API文档:guava-29.0-jre-...

    guava-20.0-API文档-中英对照版.zip

    赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...

    guava-26.0-android-API文档-中文版.zip

    赠送jar包:guava-26.0-android.jar; 赠送原API文档:guava-26.0-android-javadoc.jar; 赠送源代码:guava-26.0-android-sources.jar; 赠送Maven依赖信息文件:guava-26.0-android.pom; 包含翻译后的API文档:...

    guava-24.1-jre-API文档-中英对照版.zip

    赠送jar包:guava-24.1-jre.jar; 赠送原API文档:guava-24.1-jre-javadoc.jar; 赠送源代码:guava-24.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-24.1-jre.pom; 包含翻译后的API文档:guava-24.1-jre-...

    guava-23.0-API文档-中文版.zip

    赠送jar包:guava-23.0.jar; 赠送原API文档:guava-23.0-javadoc.jar; 赠送源代码:guava-23.0-sources.jar; 赠送Maven依赖信息文件:guava-23.0.pom; 包含翻译后的API文档:guava-23.0-javadoc-API文档-中文...

    guava-18.0-API文档-中文版.zip

    赠送jar包:guava-18.0.jar; 赠送原API文档:guava-18.0-javadoc.jar; 赠送源代码:guava-18.0-sources.jar; 包含翻译后的API文档:guava-18.0-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:...

    guava-28.0-android-API文档-中文版.zip

    赠送jar包:guava-28.0-android.jar; 赠送原API文档:guava-28.0-android-javadoc.jar; 赠送源代码:guava-28.0-android-sources.jar; 赠送Maven依赖信息文件:guava-28.0-android.pom; 包含翻译后的API文档:...

    guava-26.0-jre.zip

    4. **并发工具**:Guava提供了丰富的并发工具类,如 ListeningExecutorService、CountDownLatch、CyclicBarrier 和 Exchanger,它们简化了多线程编程,提高了并发程序的可靠性。 5. **字符串处理**:Guava提供了...

Global site tag (gtag.js) - Google Analytics