google guava 测试

来源:互联网 发布:淘宝店可以转天猫吗 编辑:程序博客网 时间:2024/05/18 10:30

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=3total count=4[red, yellow x 3]set value=redset value=yellowset value=yellowset 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 wordtest google guava Fiels class---------------------方式二读取文件内容----------------------hello wordtest google guava Fiels class---------------------方式三按行读取文件内容----------------------第0行:hello word第1行:test google guava Fiels class

参考地址

http://ifeve.com/google-guava/



0 0
原创粉丝点击