arraylist

cclgggtu  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(239)

我是新来的收藏家。我需要知道如何使用包含两个条目的arraylist。我正在编写一个程序,读取一个文件并将一个字符串存储到树Map中,并将该字符串与一个具有两列(document id,count)的arraylist相关联。我想知道我怎样才能做到这一点。有人能帮我做些指导吗?以下是系统应如何工作:

例如:如果程序读取单词“hello”,这些单词需要作为treemap(“hello”,arraylist)添加到treemap中。
单词hello与arraylist中的(documentid,count)相关联。

xxls0lw8

xxls0lw81#

不能将两个值放入arraylist。您可以创建一个新类来存储documentid和count。然后将这个新类存储在树Map的列表中:

public class CountPerDocument {
    private final String documentId;
    private final int count;

    CountPerDocument(String documentId, int count){

        this.documentId = documentId;
        this.count = count;
    }

    public String getDocumentId() {
        return documentId;
    }

    public int getCount() {
        return count;
    }
}

把你的Map填成这样

Map<String, List<CountPerDocument>> map = new TreeMap<>();
List<CountPerDocument> list = new ArrayList<>();
//add code to fill your list here
list.add(new CountPerDocument("yourId", 23));
map.put("term", list);
zzlelutf

zzlelutf2#

在arraylist中,我们不能存储在键值对中,因此可以使用对来代替arraylist。pair<string,integer>pair=新对<string,integer>(“documentid”,count);树形图(“你好”,一对);

相关问题