使用java&;映射过滤数据 公共类读取器{ 公共静态void main(字符串[]args)引发IOException、ParseException{ 缓冲读取器; 字符串animalName=“cat”; 字符串=null; 试一试{ reader=新的BufferedReader(新的InputStreamReader( 新文件输入流(“C:/dila.txt”); 映射结果=新建LinkedHashMap(); Map result2=新建LinkedHashMap(); while(reader.ready()){ 字符串行=reader.readLine(); /用空格分隔一行/ 字符串[]值=行。拆分(“,”); 字符串键=null; 如果(值[1]。比较到(动物名称)==0){ 键=值[0]; animal=”“+值[1]。比较(动物名称); 整数和=0; 整数计数=0; /获取最后一个计数器和总和/ if(结果容器(键)){ sum=result.get(键); count=result2.get(键); }否则{ } /递增计数和,并使用键保存在映射中/ put(key,sum+Integer.parseInt(值[2]); 结果2.输入(键,计数+1); } } /交互并打印新输出/ for(字符串键:result.keySet()){ 整数和=result.get(键); 整数计数=result2.get(键); System.out.println(key+“”+animalName+“”+sum+“\t”+计数); } reader.close(); }catch(filenotfounde异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); }捕获(IOE异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); } } } 我有以下文本文件 2010年11月2日,第6类 2010年11月2日,第3类 2010年11月2日,狗,4 2010年2月11日,第11类 2010年11月3日,第1类 2010年11月3日,狗,3 2010年11月3日,第8类 2010年11月3日,第80类 上述代码当前正在打印此摘要数据 2010年11月2日第20类3 2010年11月3日第104类4 2010年11月4日第26类2 我需要帮助打印摘要,如下所示 2010年1月11日 2010年2月11日第20类3 2010年3月11日第104类4 2010年4月11日第26类2 2010年5月11日 2010年6月11日 2010年7月11日 2010年8月11日 2010年9月11日 2010年10月11日 2010年11月11日 2010年11月12日 2010年11月13日 2010年11月14日 2010年11月15日 2010年11月16日 2010年11月17日 2010年11月18日 2010年11月19日 2010年11月20日 2010年11月21日 2010年11月22日 2010年11月23日 2010年11月24日 2010年11月25日 2010年11月26日 2010年11月27日 2010年11月28日 2010年11月29日 2010年11月30日

使用java&;映射过滤数据 公共类读取器{ 公共静态void main(字符串[]args)引发IOException、ParseException{ 缓冲读取器; 字符串animalName=“cat”; 字符串=null; 试一试{ reader=新的BufferedReader(新的InputStreamReader( 新文件输入流(“C:/dila.txt”); 映射结果=新建LinkedHashMap(); Map result2=新建LinkedHashMap(); while(reader.ready()){ 字符串行=reader.readLine(); /用空格分隔一行/ 字符串[]值=行。拆分(“,”); 字符串键=null; 如果(值[1]。比较到(动物名称)==0){ 键=值[0]; animal=”“+值[1]。比较(动物名称); 整数和=0; 整数计数=0; /获取最后一个计数器和总和/ if(结果容器(键)){ sum=result.get(键); count=result2.get(键); }否则{ } /递增计数和,并使用键保存在映射中/ put(key,sum+Integer.parseInt(值[2]); 结果2.输入(键,计数+1); } } /交互并打印新输出/ for(字符串键:result.keySet()){ 整数和=result.get(键); 整数计数=result2.get(键); System.out.println(key+“”+animalName+“”+sum+“\t”+计数); } reader.close(); }catch(filenotfounde异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); }捕获(IOE异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); } } } 我有以下文本文件 2010年11月2日,第6类 2010年11月2日,第3类 2010年11月2日,狗,4 2010年2月11日,第11类 2010年11月3日,第1类 2010年11月3日,狗,3 2010年11月3日,第8类 2010年11月3日,第80类 上述代码当前正在打印此摘要数据 2010年11月2日第20类3 2010年11月3日第104类4 2010年11月4日第26类2 我需要帮助打印摘要,如下所示 2010年1月11日 2010年2月11日第20类3 2010年3月11日第104类4 2010年4月11日第26类2 2010年5月11日 2010年6月11日 2010年7月11日 2010年8月11日 2010年9月11日 2010年10月11日 2010年11月11日 2010年11月12日 2010年11月13日 2010年11月14日 2010年11月15日 2010年11月16日 2010年11月17日 2010年11月18日 2010年11月19日 2010年11月20日 2010年11月21日 2010年11月22日 2010年11月23日 2010年11月24日 2010年11月25日 2010年11月26日 2010年11月27日 2010年11月28日 2010年11月29日 2010年11月30日,java,Java,我有大量从“,”中分离出来的数据。所以我想读一行并拆分我已经做到了。但我的要求高于结果下面是执行此操作的代码。我正在接受谷歌番石榴库的帮助,因为它帮助我写更少的代码;-)。如果您只想使用纯java,那么您也可以修改代码,如果逻辑需要一些调整,那么请查看processLine(…)方法,这就是更改的方向 好的,我看到的唯一缺少的代码是按排序顺序打印不属于输入文件的日期的空数据。这很简单,交给你吧。以下是提示: 我已经运行了您的示例文件,它打印了下面的摘要 public class Reader {

我有大量从“,”中分离出来的数据。所以我想读一行并拆分我已经做到了。但我的要求高于结果

下面是执行此操作的代码。我正在接受谷歌番石榴库的帮助,因为它帮助我写更少的代码;-)。如果您只想使用纯java,那么您也可以修改代码,如果逻辑需要一些调整,那么请查看processLine(…)方法,这就是更改的方向

好的,我看到的唯一缺少的代码是按排序顺序打印不属于输入文件的日期的空数据。这很简单,交给你吧。以下是提示:

我已经运行了您的示例文件,它打印了下面的摘要

public class Reader {
        public static void main(String[] args) throws IOException, ParseException {
            BufferedReader reader;
            String animalName="cat";
            String animal = null;
            try {
                reader = new BufferedReader(new InputStreamReader(
                        new FileInputStream("C:/dila.txt")));
                Map<String, Integer> result = new LinkedHashMap<String, Integer>();
                Map<String, Integer> result2 = new LinkedHashMap<String, Integer>();

                while (reader.ready()) {
                    String line = reader.readLine();
/split a line with spaces/
                    String[] values = line.split(",");
                    String key = null;                 
                    if(values[1].compareTo(animalName)==0){
                    key = values[0];
                    animal=""+values[1].compareTo(animalName);
                    int sum = 0;
                    int count = 0;
/get a last counter and sum/
                    if (result.containsKey(key)) {
                        sum = result.get(key);
                        count = result2.get(key);
                    } else{
                    }
 /increment sum a count and save in the map with key/
                    result.put(key, sum + Integer.parseInt(values[2]));
                    result2.put(key, count + 1);
                }
                }

 /interate and print new output/
                for (String key : result.keySet()) {
                    Integer sum = result.get(key);
                    Integer count = result2.get(key);
                    System.out.println(key +"   "+animalName+ " " + sum + "\t" + count);
                }
                reader.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
11/3/2010第89 3类
11/3/2010狗3 1
2010年11月2日狗4 1
2010年11月2日第20类3
导入java.io.File;
导入java.io.IOException;
导入java.util.Iterator;
导入java.util.Map;
导入com.google.common.base.CharMatcher;
导入com.google.common.base.charset;
导入com.google.common.base.Joiner;
导入com.google.common.base.Splitter;
导入com.google.common.collect.Maps;
导入com.google.common.io.Files;
导入com.google.common.io.LineProcessor;
公共类AnimalSummaryBuilder
{
私有静态最终拆分器Splitter=Splitter.on(CharMatcher.anyOf(“,”);
私有静态最终Joiner Joiner=Joiner.on(“\t”);
@抑制警告(“未选中”)
公共静态void main(最终字符串[]args)引发异常
{
@抑制警告(“原始类型”)
映射结果=Files.readLines(新文件(“c:/1.txt”)、Charsets.ISO_8859_1、new LineProcessor(){
私有最终映射结果=Maps.newHashMap();
公共对象getResult()
{
返回结果;
}
公共布尔processLine(最终字符串行)引发IOException
{
迭代器列=SPLITTER.split(line.Iterator();
字符串日期=columns.next();
字符串名称=columns.next();
int value=Integer.valueOf(columns.next()).intValue();
动物当前行=新动物(日期、名称);
if(result.containsKey(currentRow))
{
Summary Summary=result.get(当前行);
summary.increaseCount();
汇总。加总(价值);
}
其他的
{
Summary initialSummary=新摘要();
initialSummary.setCount(1);
initialSummary.setTotal(值);
结果.输入(当前行,初始汇总);
}
返回true;
}
});
for(Map.Entry:result.entrySet())
{
动物=entry.getK
11/3/2010   cat 89  3
11/3/2010   dog 3   1
11/2/2010   dog 4   1
11/2/2010   cat 20  3



import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;

public class AnimalSummaryBuilder
{
    private static final Splitter SPLITTER = Splitter.on(CharMatcher.anyOf(","));

    private static final Joiner JOINER = Joiner.on("\t");

    @SuppressWarnings("unchecked")
    public static void main(final String[] args) throws Exception
    {
        @SuppressWarnings("rawtypes")
        Map<Animal, Summary> result = Files.readLines(new File("c:/1.txt"), Charsets.ISO_8859_1, new LineProcessor() {

            private final Map<Animal, Summary> result = Maps.newHashMap();

            public Object getResult()
            {
                return result;
            }

            public boolean processLine(final String line) throws IOException
            {
                Iterator<String> columns = SPLITTER.split(line).iterator();

                String date = columns.next();
                String name = columns.next();
                int value = Integer.valueOf(columns.next()).intValue();

                Animal currentRow = new Animal(date, name);

                if (result.containsKey(currentRow))
                {
                    Summary summary = result.get(currentRow);
                    summary.increaseCount();
                    summary.addToTotal(value);
                }
                else
                {
                    Summary initialSummary = new Summary();
                    initialSummary.setCount(1);
                    initialSummary.setTotal(value);
                    result.put(currentRow, initialSummary);
                }
                return true;
            }
        });

        for (Map.Entry<Animal, Summary> entry : result.entrySet())
        {
            Animal animal = entry.getKey();
            Summary summary = entry.getValue();
            System.out.println(JOINER.join(animal.date, animal.name, summary.total, summary.count));
        }
    }

    final static class Animal
    {
        String date;

        String name;

        public Animal(final String date, final String n)
        {
            this.date = date;
            this.name = n;
        }

        @Override
        public int hashCode()
        {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((date == null) ? 0 : date.hashCode());
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj)
        {
            if (this == obj)
            {
                return true;
            }
            if (obj == null)
            {
                return false;
            }
            if (!(obj instanceof Animal))
            {
                return false;
            }
            Animal other = (Animal) obj;
            if (date == null)
            {
                if (other.date != null)
                {
                    return false;
                }
            }
            else if (!date.equals(other.date))
            {
                return false;
            }
            if (name == null)
            {
                if (other.name != null)
                {
                    return false;
                }
            }
            else if (!name.equals(other.name))
            {
                return false;
            }
            return true;
        }

    }

    final static class Summary
    {
        private int total;

        private int count;

        void setTotal(int value)
        {
            total = value;
        }

        void setCount(int i)
        {
            count = i;
        }

        void increaseCount()
        {
            count++;
        }

        void addToTotal(int valueToAdd)
        {
            total += valueToAdd;
        }
    }
}
public class Reader
{
    public static void main(final String[] args) throws ParseException
    {
        BufferedReader reader = null;
        String animalName = "cat";
        // String animal = null;
        try
        {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/1.txt")));
            Map<String, Integer> result = new LinkedHashMap<String, Integer>();
            Map<String, Integer> result2 = new LinkedHashMap<String, Integer>();

            while (reader.ready())
            {
                String line = reader.readLine();
                // split a line with spaces
                String[] values = line.split(",");
                String key = null;
                if (values[1].compareTo(animalName) == 0)
                {
                    key = values[0];
                    // animal=""+ ""+values[1].compareTo(animalName);
                    int sum = 0;
                    int count = 0;
                    // get a last counter and sum
                    if (result.containsKey(key))
                    {
                        sum = result.get(key);
                        count = result2.get(key);
                    }
                    else
                    {
                    }
                    // increment sum a count and save in the map with key
                    result.put(key, sum + Integer.parseInt(values[2]));
                    result2.put(key, count + 1);
                }
            }

            String date = result.keySet().iterator().next();
            DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(df.parse(date));
            int monthStart = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
            int monthEnd = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            calendar.set(Calendar.DAY_OF_MONTH, 0);

            // interate and print new output
            for (int i = monthStart; i < monthEnd; i++)
            {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                String key = df.format(calendar.getTime());
                if (result.containsKey(key))
                {
                    Integer sum = result.get(key);
                    Integer count = result2.get(key);
                    System.out.println(key + "   " + animalName + " " + sum + "\t" + count);
                }
                System.out.println(key);
            }
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}