Guava

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/anguea/article/details/66968374


字符串拼接(用符号)

/**
         * 字符串在集合中的拼接方法③
         */
       集合  List<String> list = new ArrayList<String>();
                list.add("aaa");
                list.add("bbb");
                list.add("ccc");
        guava方法  List<String> glist = Lists.transform(list, new Function<String, String>() {
                public String apply(String input) {
                    return String.valueOf(input) + "s";//每一个里面添加一个S

                }
            });
循环打印检验
            for (String s:glist) {
                System.out.println(s);
            }




Joiner joiner =Joiner.on("; ").skipNulls();
return joiner.join("Harry",null,"Ron","Hermione");

Joiner.on(",").join(Arrays.asList(1,5,7));// returns "1,5,7"

通过字符串的固定符号如(,或-)返回一个字符串迭代器
Iterable<String> iter = Splitter.on(',')
           .trimResults()
           .omitEmptyStrings()
           .split("foo,bar,,qux");

字符串操作
//删除控制字符
        String noControl = CharMatcher.JAVA_ISO_CONTROL.removeFrom(str); // remove control characters
        System.out.println(noControl);
        //保留字符串里面的数字
        String theDigits = CharMatcher.DIGIT.retainFrom(str);
        System.out.println(theDigits);
        //所有的空白用单个空白代替
        String spaced = CharMatcher.WHITESPACE.trimAndCollapseFrom(str,' ');
        System.out.println(spaced);
        //提出所有的数字
        String noDigits = CharMatcher.JAVA_DIGIT.replaceFrom(str, "*");
        System.out.println(noDigits);
        //保留小写字母和数字
        String lowerAndDigit = CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom(str);
        System.out.println(lowerAndDigit);

Java的原生类型就是指基本类型:byte、short、int、long、float、double、char和boolean。

//2016.2.16把原生类型转换为其它原生类型
        long kk = 278554547;
        int in = Ints.checkedCast(kk);
        //字节转化为int
        Byte by = 'e';
        int bv = by.intValue();
        System.out.println(bv);

//两个数字之间的取值范围
        System.out.println("open:"+Range.open(1, 10));
        System.out.println("closed:"+ Range.closed(1, 10));
        System.out.println("closedOpen:"+ Range.closedOpen(1, 10));
        System.out.println("openClosed:"+ Range.openClosed(1, 10));    
        System.out.println("greaterThan:"+ Range.greaterThan(10));
        System.out.println("atLeast:"+ Range.atLeast(10));
        System.out.println("lessThan:"+ Range.lessThan(10));
        System.out.println("atMost:"+ Range.atMost(10));
        System.out.println("all:"+ Range.all());
        System.out.println("closed:"+Range.closed(10, 10));
        System.out.println("closedOpen:"+Range.closedOpen(10, 10));
        //会抛出异常
        System.out.println("open:"+Range.open(0, 1));
        System.out.println("Operations");
        System.out.println("downTo:"+Range.downTo(4, BoundType.OPEN));
        System.out.println("upTo:"+Range.upTo(4, BoundType.CLOSED));
        System.out.println("range:"+Range.range(1, BoundType.CLOSED, 4, BoundType.OPEN)); 
        System.out.println("Query Operations");
        Range.closedOpen(4, 4).isEmpty(); // returns true
        Range.openClosed(4, 4).isEmpty(); // returns true
        Range.closed(4, 4).isEmpty(); // returns false
        Range.open(3, 4).isEmpty(); // Range.open throws IllegalArgumentException
        Range.closed(3, 10).lowerEndpoint(); // returns 3
        Range.open(3, 10).lowerEndpoint(); // returns 3
        Range.closed(3, 10).lowerBoundType(); // returns CLOSED
        Range.open(3, 10).upperBoundType(); // returns OPEN


展开阅读全文

没有更多推荐了,返回首页