Java容器-引入Guava类库

目录

1、只读设置

2、函数式编程+组合式编程

3、约束条件

4、集合操作(并集、差集、交集)

代码实现

1、只读设置

public static void main(String [] args){
        //只读设置
        List ls=new ArrayList();
        ls.add("a");
        ls.add("b");
        ls.add("c");
        //不使用guava的类库
        List <String > readList= Collections.unmodifiableList(ls);
        //readList.add("d"); 报错
        //使用guava的类库
        List<String> imutableList= ImmutableList.of("a","b","c");
        //imutableList.add("a"); 运行报错
}

2、函数式编程

(1)函数一:找出集合众的回文字符串,回文又称 mirror word ,backword,是指字符串从前面或者后面读都是一样的,比如moom 

//结果:moom   因为moon逆序以后还是moom
    public static void main(String[] args) {
        List<String> list = Lists.newArrayList("dog", "cat", "pig", "moom");
    Collection</span>&lt;String&gt; pList=Collections2.filter(list, <span style="color: #0000ff;">new</span> Predicate&lt;String&gt;<span style="color: #000000;">() {
        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">boolean</span><span style="color: #000000;"> apply(String s) {
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> StringBuilder(s).reverse().toString().equals(s);
        }
    });

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 匿名内部类,同时创建对象,Collections2.filter类似过滤器</span>
    <span style="color: #0000ff;">for</span><span style="color: #000000;">(Object o:pList){
        System.out.println(o);
    }
}</span></span></pre>

(2)函数二:日期转换

//结果:1970-01-01    1970-01-24   1970-01-02
 public static void main(String [] args){
        Set<Long> timeSet= Sets.newHashSet();
        timeSet.add(1000L);
        timeSet.add(2000L*1000000);
        timeSet.add(3000L*20000);
    Collection</span>&lt;String&gt; transList= Collections2.transform(timeSet, <span style="color: #0000ff;">new</span> Function&lt;Long, String&gt;<span style="color: #000000;">() {
        </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String apply(Long input) {
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> SimpleDateFormat("yyyy-MM-dd"<span style="color: #000000;">).format(input);
        }
    });

    </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(String s:transList){
        System.out.println(s);
    }
}</span></span></pre>

(3)函数三:组合式编程

public static void main(String [] args){
        List<String> list = Lists.newArrayList("happy", "sad", "wahaha");
        //方法一
        Function<String,String>f1=new Function<String, String>() {
            public String apply(String s) {
                return s.length()>5&&s.length()<20?s:"error";
            }
        };
        //方法二:字母全部大写
        Function<String, String> f2 = new Function<String, String>() {
            public String apply(String input) {
                return input.toUpperCase();
            }
        };
        //组合方法
        Function<String, String> f = Functions.compose(f1, f2);
        Collection resultCol=Collections2.transform(list,f);
        for(Object s:resultCol){
            System.out.println(s);
        }
    }

 3、约束条件

 public static void main(String[] args) {
        Set<String> sets = Sets.newHashSet();
        // 创建约束
        Constraint<String> constraint = new Constraint<String>() {
        @Override
        </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String checkElement(String element) {
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 非空验证</span>

Preconditions.checkNotNull(element);

            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 长度限制 5-20,否则报错</span>

Preconditions.checkArgument(
element.length()
>= 5 && element.length() <= 20,
element);
return element;
}

    };

    Set</span>&lt;String&gt; cs =<span style="color: #000000;"> Constraints.constrainedSet(sets, constraint);

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> cs.add(null); 报错java.lang.NullPointerException
    </span><span style="color: #008000;">//</span><span style="color: #008000;">cs.add("qaz"); 报错java.lang.IllegalArgumentException: qaz</span>
}</span></pre>

 4、交集、并集、差集

 public static void main(String [] args){
        Set<Integer> sets=Sets.newHashSet(1,2,3,4,5,6);
        Set<Integer> set2=Sets.newHashSet(3,4,5,6,7,8,9);
    Sets.SetView</span>&lt;Integer&gt; intersection =<span style="color: #000000;">Sets.intersection(sets, set2);
    </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Integer in:intersection){
        System.out.print(in</span>+"  "<span style="color: #000000;">);
    }
    System.out.println(</span>""<span style="color: #000000;">);
    </span><span style="color: #008000;">//</span><span style="color: #008000;">差集</span>
    Sets.SetView&lt;Integer&gt; intersection2=<span style="color: #000000;">Sets.difference(sets,set2);
    </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Integer in:intersection2){
        System.out.print(in</span>+"  "<span style="color: #000000;">);
    }
    System.out.println(</span>""<span style="color: #000000;">);
    </span><span style="color: #008000;">//</span><span style="color: #008000;">并集</span>
    Sets.SetView&lt;Integer&gt; intersection3=<span style="color: #000000;">Sets.union(sets,set2);
    </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Integer in:intersection3){
        System.out.print(in</span>+"  "<span style="color: #000000;">);
    }
}</span></span></pre>

 

posted @ 2017-03-18 19:43  码农皮邱  阅读(2153)  评论(0编辑  收藏  举报