Java 获取HashMap值的计数

Java 获取HashMap值的计数,java,java-io,Java,Java Io,使用以下代码将文本文件内容加载到GUI: Map<String, String> sections = new HashMap<>(); Map<String, String> sections2 = new HashMap<>(); String s = "", lastKey=""; try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) { w

使用以下代码将文本文件内容加载到GUI:

Map<String, String> sections = new HashMap<>();
Map<String, String> sections2 = new HashMap<>();
String s = "", lastKey="";
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
    while ((s = br.readLine()) != null) {
        String k = s.substring(0, 10).trim();
        String v = s.substring(10, s.length() - 50).trim();
        if (k.equals(""))
            k = lastKey;
        if(sections.containsKey(k))
            v = sections.get(k) + v; 
        sections.put(k,v);
        lastKey = k;
    }
} catch (IOException e) {
}
System.out.println(sections.get("AUTHOR"));
System.out.println(sections2.get("TITLE"));
现在我想计算HashMap中的值,但是
sections.size()
计算存储在文本文件中的所有数据行


我想问一下如何计算项目,即
部分中的值
v
?如何根据作者姓名获得数字4?

由于作者具有1对多关系,您应该将其映射到
列表
结构,而不是
字符串

例如:

Map<String, ArrayList<String>> sections = new HashMap<>();
Map<String, String> sections2 = new HashMap<>();
String s = "", lastKey="";
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
    while ((s = br.readLine()) != null) {
        String k = s.substring(0, 10).trim();
        String v = s.substring(10, s.length() - 50).trim();
        if (k.equals(""))
            k = lastKey;

        ArrayList<String> authors = null;
        if(sections.containsKey(k))
        {
            authors = sections.get(k);
        }
        else
        {
            authors = new ArrayList<String>();
            sections.put(k, authors);
        }
        authors.add(v);
        lastKey = k;
    }
} catch (IOException e) {
}

// to get the number of authors
int numOfAuthors = sections.get("AUTHOR").size();

// convert the list to a string to load it in a GUI
String authors = "";
for (String a : sections.get("AUTHOR"))
{
    authors += a;
}
Map sections=newhashmap();
Map sections2=新的HashMap();
字符串s=“”,lastKey=“”;
try(BufferedReader br=newbufferedreader(newfilereader(“input.txt”)){
而((s=br.readLine())!=null){
字符串k=s.substring(0,10).trim();
字符串v=s.substring(10,s.length()-50).trim();
如果(k等于(“”)
k=最后一个键;
ArrayList authors=null;
if(第2(k)节)
{
作者=节。获取(k);
}
其他的
{
authors=newarraylist();
(k,作者);
}
作者:添加(v);
lastKey=k;
}
}捕获(IOE异常){
}
//获取作者的数量
int numOfAuthors=sections.get(“AUTHOR”).size();
//将列表转换为字符串以在GUI中加载
字符串作者=”;
for(字符串a:sections.get(“作者”))
{
作者+=a;
}

谢谢您的回复,现在count正在工作,但我无法
setText
jTextField1
jTextField1.setText(sections.get(“作者”)?如果要将列表转换为字符串,则只需在列表中循环并根据需要连接字符串即可。我将在我的帖子中展示一个例子。谢谢。我很高兴能够提供帮助。我想问一个与
numOfAuthors
相关的问题,我想将其用于
jButton1.doClick(numOfAuthors),但它不能正常工作,我不明白为什么。我不确定您在那里尝试做什么,因为doClick()方法不接受任何参数。我会将此作为另一个问题与相关代码一起发布,以及您得到的错误。
Map<String, ArrayList<String>> sections = new HashMap<>();
Map<String, String> sections2 = new HashMap<>();
String s = "", lastKey="";
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
    while ((s = br.readLine()) != null) {
        String k = s.substring(0, 10).trim();
        String v = s.substring(10, s.length() - 50).trim();
        if (k.equals(""))
            k = lastKey;

        ArrayList<String> authors = null;
        if(sections.containsKey(k))
        {
            authors = sections.get(k);
        }
        else
        {
            authors = new ArrayList<String>();
            sections.put(k, authors);
        }
        authors.add(v);
        lastKey = k;
    }
} catch (IOException e) {
}

// to get the number of authors
int numOfAuthors = sections.get("AUTHOR").size();

// convert the list to a string to load it in a GUI
String authors = "";
for (String a : sections.get("AUTHOR"))
{
    authors += a;
}