java使用Map进行分组统计

用java读取txt文本,并根据ip段分组合计|后面的值,即ip相同数字累加

127.0.0.1|395
127.0.0.2|390
127.0.0.3|370
127.0.0.4|340
127.0.0.3|339
127.0.0.1|338
127.0.0.5|521

--------------运行结果-------------

127.0.0.5 === 521
127.0.0.3 === 709
127.0.0.4 === 340
127.0.0.2 === 390
127.0.0.1 === 733


-------------------------------

java代码 根据Map中的Key值,进行数值累加


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

 

/**
 * 读取txt文本
 * ip 相等 数字累加
 * @author snail
 *
 */
public class readtxt {
 

    
    /**
     * 根据传递的路径File对象 解析txt
     * @param file
     * @return
     * @author  
     */
    public HashMap<String, Integer> readLogInfo(File file) {
    	HashMap<String, Integer> map = new HashMap<String, Integer>();
         //
        InputStreamReader inputStreamReader = null;
        // 创建文件输入流缓存对象
        BufferedReader bufferedReader = null;
        try {
            if(file!=null){
	            inputStreamReader = new InputStreamReader(new FileInputStream(file),"GBK");
	            bufferedReader = new BufferedReader(inputStreamReader);
	            String lineString ="";
	            String[] strArr=null;
                int value=0;
	            while((lineString = bufferedReader.readLine()) != null){
	            	strArr=lineString.split("\\|");
                    if(map.get(strArr[0])!=null){
                    	value=Integer.parseInt(map.get(strArr[0]).toString())+Integer.parseInt(strArr[1]);
                    }else{
                    	value=Integer.parseInt(strArr[1]);
                    }
                    map.put(strArr[0], value);
	           }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if(inputStreamReader!=null){
                    inputStreamReader.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;    
    }	    
    

    
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			// txt 文本路径
			String filePath="E:"+File.separator+"a.txt";
			File file = new File(filePath);
			readtxt rd = new readtxt();
			HashMap<String, Integer> map=rd.readLogInfo(file);
			if(map !=null){
				
				  Set keySet = map.keySet();//返回键的集合 
				  Iterator it = keySet.iterator(); 

				  while(it.hasNext())      //第一种迭代方式取键值 
				  { 
				   Object key = it.next(); 
				   System.out.println(key+" === "+map.get(key)); //根据键来取对应的值 
				  } 
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		


		
		
	}

}


你可能感兴趣的:(java分组,java统计)