google guava 测试
2016-03-26 00:27
555 查看
HashMultiset 统计单词次数
输出
Files 操作文件
结果:
参考地址
http://ifeve.com/google-guava/
import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset.Entry; /** * * 统计一个词在文档中出现了多少次 */ public static void main(String[] args) throws Exception{ HashMultiset<String> multiset = HashMultiset.create(); multiset.add("hello"); multiset.add("hello"); multiset.add("word"); multiset.add("hello"); multiset.add("hello"); // 方法一遍历 System.out.println("---------------------方法一遍历----------------------"); for(Entry<String> entry : multiset.entrySet()){ System.out.println(entry.getElement()+"出现:"+entry.getCount()+"次"); } // 方法二遍历 System.out.println("---------------------方法二遍历----------------------"); for(String word : multiset.elementSet()){ System.out.println(word+"出现:"+multiset.count(word)+"次"); } System.out.println("----------------------统计总数-----------------------"); System.out.println("一共有" +multiset.size()+"个单词"); }
输出
---------------------方法一遍历---------------------- hello出现:4次 word出现:1次 ---------------------方法二遍历---------------------- hello出现:4次 word出现:1次 ----------------------统计总数----------------------- 一共有5个单词
Files 操作文件
import java.io.File; import java.io.IOException; import com.google.common.base.Charsets; import com.google.common.collect.HashMultiset; import com.google.common.collect.ImmutableList; import com.google.common.collect.Multiset.Entry; import com.google.common.io.Files; /** * guava 测试类 * * @author zhengyong * */ public class TestGuava { public static void main(String[] args) throws Exception { testFiles(); } /** * google Guava Files 测试 * @throws IOException */ private static void testFiles() throws IOException { String textFilePath = "/Users/zhengyong/test.txt"; File file = new File(textFilePath); System.out.println("---------------------方式一读取文件内容----------------------"); String content1 = Files.asCharSource(file, Charsets.UTF_8).read(); System.out.println(content1); System.out.println("---------------------方式二读取文件内容----------------------"); String content2 = Files.asByteSource(file).asCharSource(Charsets.UTF_8).read(); System.out.println(content2); // 读取字节流 - 读取文件流,并写向另外一个文件 File fileForm = new File("/Users/zhengyong/testFile.jpg"); File fileDest = new File("/Users/zhengyong/Files.png"); byte[] bytes = Files.asByteSource(fileForm).read(); Files.write(bytes, fileDest); System.out.println("---------------------方式三按行读取文件内容----------------------"); ImmutableList<String> lines = Files.asCharSource(file, Charsets.UTF_8).readLines(); for (int i = 0, length = lines.size(); i < length; i++) { System.out.println("第" + i + "行:" + lines.get(i)); } } }
结果:
---------------------方式一读取文件内容---------------------- hello word test google guava Fiels class ---------------------方式二读取文件内容---------------------- hello word test google guava Fiels class ---------------------方式三按行读取文件内容---------------------- 第0行:hello word 第1行:test google guava Fiels class
参考地址
http://ifeve.com/google-guava/
相关文章推荐
- good luck
- 智联招聘LOGO
- google工具包
- golang笔记——map
- 实用Google 搜索方法总结(持续更新)
- HDU 3715 Go Deeper 2-SAT 二分答案
- Google源码下载,通过清华大学 TUNA 镜像源
- Webgoat学习笔记1
- [django]自定义全局context
- 关于OGR读取多边形-Polygon(GDAL 2.0版本之前)
- django-模型超级总结
- go语言学习心得
- zjnu1726 STOGOVI (lca)
- Django CSRF_TOKEN跨域攻击处理
- POJ 2195 Going Home (最小费用最大流)
- Go语言核心之美 3.4-Struct结构体
- GoDaddy重推CN域名注册服务
- GoDaddy重推CN域名注册服务
- hdu 5645 DZY Loves Balls
- hdu 5646 DZY Loves Partition