guava之只读、函数式编程(过滤、转换)
来源:互联网 发布:在淘宝退货卖家拒收 编辑:程序博客网 时间:2024/05/22 02:24
package com.lfj.myguava;import java.util.ArrayList;import java.util.Collections;import java.util.List;import com.google.common.collect.ImmutableList;/** * 只读设置 * @author Administrator * */public class Demo1 { public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); //对原有list进行包装,相等于原有List的一个视图、快照 List<String> readList=Collections.unmodifiableList(list); //readList.add("d"); //java.lang.UnsupportedOperationException异常 list.add("e"); //改变原有List 视图也一起改变 //对比查看 初始化List,且不可再改变 List<String> list1=ImmutableList.of("a","b","c"); //list1.add("e"); 添加失败 }}
package com.lfj.myguava;import java.text.SimpleDateFormat;import java.util.Collection;import java.util.List;import java.util.Set;import com.google.common.base.Function;import com.google.common.base.Functions;import com.google.common.base.Predicate;import com.google.common.collect.Collections2;import com.google.common.collect.Lists;import com.google.common.collect.Sets;/** * 函数式编程 * 1.Predicate 断言 * 2.Function * * 工具:Collections2.filter 过滤器 * Collections2.transform 转换 * @author Administrator * */public class Demo2 { public static void main(String[] args) { //创建List 静态初始化 List<String> list=Lists.newArrayList("son","dad","moom","refer"); //找出回文 palindrome Collection<String> palindromeList =Collections2.filter(list,new Predicate<String>(){ public boolean apply(String input) { //业务逻辑 return new StringBuilder(input).reverse().toString().equals(input); } }); for(String temp:palindromeList){ System.out.println(temp); } Set<Long> timeSet=Sets.newHashSet(); timeSet.add(10000000000L); timeSet.add(4444444444444444L); timeSet.add(8888888888888L); Collection<String> timeStrCol= Collections2.transform(timeSet, new Function<Long,String>(){ @Override public String apply(Long input) { return new SimpleDateFormat("yyyy-MM-dd").format(input); } }); for (String string : timeStrCol) { System.out.println(string); } //组合式编程 //确保容器中的字符串长度不超过5,超过进行截取,后全部大写 List<String> list2=Lists.newArrayList("lfj","happiness","howwww"); Function<String,String> f1=new Function<String, String>() { @Override public String apply(String input) { // TODO Auto-generated method stub return input.length()>5?input.substring(0,5):input; } }; Function<String, String> f2=new Function<String, String>() { @Override public String apply(String input) { // TODO Auto-generated method stub return input.toUpperCase(); } }; //组合使用 Function<String,String> f=Functions.compose(f1, f2); Collection<String> result=Collections2.transform(list2, f); for (String string : result) { System.out.println(string); } }}
输出:
dad moom refer 142808-11-02 1970-04-27 2251-09-05 LFJ HAPPI HOWWW
阅读全文
0 0
- guava之只读、函数式编程(过滤、转换)
- guava之只读、函数式编程(过滤、转换、组合 )、约束和集合
- guava之只读、函数式编程(过滤 转换 约束)、集合与apacheJAVA134-135
- guava ---只读,过滤,转换,组合,约束
- 135_容器_guava之_只读_函数式编程(过滤、转换、组合 )_约束_集合
- Guava 教程(3):Java 的函数式编程,通过 Google Collections 过滤和调用
- JAVA学习笔记46——其他容器简介3:guava只读控制+函数式编程+集合操作
- guava(4)函数式编程
- java guava 函数式编程
- Guava类库学习--只读设置+函数编程+约束条件+集合操作
- guava function and predicate 函数式编程
- Java Guava中的函数式编程讲解
- 使用Guava进行函数式编程
- [Google Guava] 4-函数式编程
- shell文本过滤编程(八):awk之内置函数
- shell文本过滤编程(八):awk之内置函数
- Java经典类库-Guava中的函数式编程讲解
- Java经典类库-Guava中的函数式编程讲解
- NP 完全问题8.3
- 修道士和野人问题
- react native 相册功能
- django post和get方法获取数据 数据在模板上的遍历
- 按日期查询
- guava之只读、函数式编程(过滤、转换)
- ROS机器人操作系统的安装、配置与初级教程 1
- 二叉查找树的简单实现
- ffmpeg参数解释--中文详细
- .html().text().val() 三者的区别
- 堆栈到底是个什么东西?
- ES-TCPTransport 2
- POJ1979 Red and Black (DFS)
- Git学习总结(16)——开源世界GitHub和开源中国GitOSChina同步提交