您的位置:首页 > 编程语言 > Go语言

google guava 测试

2016-03-26 00:27 555 查看
HashMultiset 统计单词次数

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: