【问题标题】:Trying to fill and return a List inside a HashMap of objects尝试在对象的 HashMap 中填充并返回 List
【发布时间】:2013-11-30 10:38:37
【问题描述】:

我需要统计字符串的出现次数和关系。

例如,我应该读取一个像上面这样的文件:

(city)  (name)  (surname)
London  Johnny  Stuart
Leeds   Johnny  Bravo
Wigan   Richard Stuart
Bristol Richard Bravo
Bolton  Johnny  Bravo
York    Alex    Crowley
Bolton    null    null

我需要计算姓氏的数量、唯一名称和与姓氏相关的列表名称,如上面的输出(姓氏、姓氏计数、唯一名称计数、名称列表):

Stuart  2 2 Johnny, Richard
Bravo   3 2 Johnny, Richard
Crowley 1 1 Alex

我正在尝试实现 MutableInt (Most efficient way to increment a Map value in Java) 的修改版本,但没有成功。

代码:

class MutableInt{

    //Surname Count
    int value = 1;
        public void increment () { ++value;      }
        public int  get ()       { return value; }
    //Name List and Count
    List<String> name;
    int nameCount = 1;
        public void incrementName () { ++nameCount;      }
        public int getNameCount() { return nameCount; }         

        public void addName(String n){
            if(n != null && !name.contains(n)){
                name.add(n);
                incrementName();
            }
        }
        public String getName() {
            return Arrays.toString(name.toArray());

        }
        public String toString() {
        return (this.get()+
            "\t"+ this.getNameCount() +
            "\t"+ this.getName());
        }
}

HashMap 填充:

    Map<String, MutableInt> nameCount = new HashMap<String, MutableInt>();
    String l;
    while ((l = inputStream.readLine()) != null) {

       if (curLine++ < 1) {
            continue;
        }

        values = l.split("\t");
        String name = values[1];
        String surname = values[2];

        // Count Names
        MutableInt count1 = nameCount.get(surname);
        if (count1 == null) {
            nameCount.put(surname, new MutableInt());
            nameCount.get(surname).addName(name);

        }
        else {
            count1.increment();
            nameCount.get(surname).addName(name);
        }
}

印刷:

Iterator<Entry<String, MutableInt>> itrE1 = nameCount.entrySet().iterator();

    while(itrE1.hasNext()){
        Map.Entry<String, MutableInt> entry1 = itrE1.next();

        efileWriter.write(entry1.getKey()"\t"+ entry1.getValue().toString()+"\n");
   }

这是我遇到的错误:

java.lang.NullPointerException at diverenrich.InterfaceAgent$writeSummary$MutableInt.addGene(InterfaceAgent.java:626)
        at diverenrich.InterfaceAgent$writeSummary.action(InterfaceAgent.java:744)

Line 626 -> if(na != null && !name.contains(name)){ ... }
Line 744 -> nameCount.get(surname).addName(name);

有什么建议吗?

【问题讨论】:

  • nameCount 的正确值为 0,而不是 1!

标签: java string list hashmap


【解决方案1】:

在您的MutableInt 中,您没有初始化name,所以它是null - 因此在null 对象上调用.contains() 时的NPE。您需要将其初始化为某些内容,例如:

List<String> name = new ArrayList<String>();

另外,换一种说法,您可能想稍微清理一下您的设计,或者至少为您的对象选择更合适的名称,因为您的 MutableInt 似乎远不止是一个可变整数。

【讨论】:

    猜你喜欢
    • 2020-06-10
    • 2017-10-26
    • 2018-10-31
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多