Guava源码阅读-collect-Multiset

package com.google.common.collect;

我们在进行字符统计时,同常采用的方法就是:

        String[] text=new String[]{"the weather is good ","today is good","today has good weather","good weather is good"};  
        HashMap<String, Integer> hashMap=new HashMap<String, Integer>();  
        for (int i=0;i<text.length;i++){  
            String temp=text[i];  
            String[] words=temp.split("\\s");  
            for(int j=0;j<words.length;j++){  
                if(!hashMap.containsKey(words[j])){  
                    hashMap.put(words[j], new Integer(1));  
                }else{  
                    int k=hashMap.get(words[j]).intValue()+1;  
                    hashMap.put(words[j], new Integer(k));  
                }  
            }  
        }  

 这种方法的思想就是:首先建立一个Map,key值存储单词,value存储出现次数,在循环添加单词,如果没有相同的key,则将单词添加到key中,并设置它的value值为1,如果map中含有相同的key,则将对应的value值加1。

这种方法冗余且容易出错。guava设计了一个集合类,Multiset,就是今天我们要介绍的。

先看看Multiset怎么进行词频统计的:

 

     String[] text=new String[]{"the weather is good ","today is good","today has good weather","good weather is good"};  
        Multiset<String> set = HashMultiset.create(list);for (int i=0;i<text.length;i++){  
            String temp=text[i];  
            String[] words=temp.split("\\s");  
            for(int j=0;j<words.length;j++){  
              set.add(words[j]);
            }  
        }  
    在获取某个单词的个数时:    
    System.out.println(set.count("the")); //这样就可以了哦

 

 

 

简单吧,Mutiset解决了我们很多问题,从类名上我们就可以知道这个set集合可以存放相同的元素。

现在看看它的主要用法:

 

Multiset接口定义的接口主要有:
    add(E element) :向其中添加单个元素
    add(E element,int occurrences) : 向其中添加指定个数的元素
    count(Object element) : 返回给定参数元素的个数
    remove(E element) : 移除一个元素,其count值 会响应减少
    remove(E element,int occurrences): 移除相应个数的元素
    elementSet() : 将不同的元素放入一个Set中
    entrySet(): 类似与Map.entrySet 返回Set<Multiset.Entry>。包含的Entry支持使用getElement()和getCount()
    setCount(E element ,int count): 设定某一个元素的重复次数
    setCount(E element,int oldCount,int newCount): 将符合原有重复个数的元素修改为新的重复次数
    retainAll(Collection c) : 保留出现在给定集合参数的所有的元素
    removeAll(Collectionc) : 去除出现给给定集合参数的所有的元素

 

 

 

实例:

 

 

      Multiset<String> wordsMultiset = HashMultiset.create();
        wordsMultiset.addAll(wordList);
        
        
        
        for(String key:wordsMultiset.elementSet()){
            System.out.println(key+" count:"+wordsMultiset.count(key));
        }
        
        if(!wordsMultiset.contains("peida")){
            wordsMultiset.add("peida", 2);
        }
       

     for(String key:wordsMultiset.elementSet()){
            System.out.println(key+" count:"+wordsMultiset.count(key));
        }
        
        
        if(wordsMultiset.contains("peida")){
            wordsMultiset.setCount("peida", 23);
        }
        
        System.out.println("============================================");
        for(String key:wordsMultiset.elementSet()){
            System.out.println(key+" count:"+wordsMultiset.count(key));
        }
        
        if(wordsMultiset.contains("peida")){
            wordsMultiset.setCount("peida", 23,45);
        }
        
        System.out.println("============================================");
        for(String key:wordsMultiset.elementSet()){
            System.out.println(key+" count:"+wordsMultiset.count(key));
        }
        
        if(wordsMultiset.contains("peida")){
            wordsMultiset.setCount("peida", 44,67);
        }
        
        System.out.println("============================================");
        for(String key:wordsMultiset.elementSet()){
            System.out.println(key+" count:"+wordsMultiset.count(key));
        }

转载于:https://www.cnblogs.com/haolnu/p/7413607.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值