google guava 测试

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个单词

2. 示列代码

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;

import java.util.List;

public class MutisetTest {

    public static final List<String> collection = Lists.newArrayList("yellow", "red", "yellow", "yellow");

    public static void main(String[] args) {
        Multiset<String> set = HashMultiset.create();
        for (String key : collection) {
            set.add(key);
        }
        int yellowCount = set.count("yellow");
        System.out.println(String.format("yellow count=%s", yellowCount));
        System.out.println(String.format("total count=%s", set.size()));
        System.out.println(set.toString());

        for (String key : set) {
            System.out.println(String.format("set value=%s", key));
        }
    }
}
输出:
yellow count=3
total count=4
[red, yellow x 3]
set value=red
set value=yellow
set value=yellow
set value=yellow

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/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值