Java ArrayList 到 HashMap 的映射

发布于 2024-09-24 20:12:31 字数 501 浏览 0 评论 0原文

我有页面被给予 ArrayList;其中每个文档都有一个称为类型的属性。

我不知道独特类型或文档的数量。

我想将此 ArrayList 排序为 HashMap但我在理解它时遇到了一些困难。

一些伪代码希望

for (int i = 0; i < documents.size(); i++) 
{
   if there is an array for documents[i].type
   add to this array
   else create a new array for this type
   add document[i].type and the array of documents with matching type to the hashmap
}

我知道这是错误的方法并且显然行不通。我愿意接受任何建议。

谢谢

I have page gets given an ArrayList<Document> where each document has a property called type.

I don't know the number of unique types or documents.

I want to sort this ArrayList into a HashMap<type, document[]> but am having some trouble getting my head around it.

Some pseudo-code would like like

for (int i = 0; i < documents.size(); i++) 
{
   if there is an array for documents[i].type
   add to this array
   else create a new array for this type
   add document[i].type and the array of documents with matching type to the hashmap
}

I know this is the wrong approach and clearly won't work. I am open to any suggestions.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽手叙旧 2024-10-01 20:12:31
// create the map to store stuff, note I'm using a List instead of an array
// in my opinion it's a bit cleaner
Map<String, List<Document>> map = new HashMap<String, List<Document>>();

// now iterate through each document
for(Document d : documents){

    // check to see if this type is already known
    List<Document> list = map.get(d.type);

    if(list == null){
        // list is null when it wasn't found in the map
        // this is a new type, create a new list
        list = new ArrayList<Document>();

        // store the list in the map
        map.put(d.type, list);
    }

    // finally, whether we got a hit or a miss, we want
    // to add this document to the list for this type
    list.add(d);
}
// create the map to store stuff, note I'm using a List instead of an array
// in my opinion it's a bit cleaner
Map<String, List<Document>> map = new HashMap<String, List<Document>>();

// now iterate through each document
for(Document d : documents){

    // check to see if this type is already known
    List<Document> list = map.get(d.type);

    if(list == null){
        // list is null when it wasn't found in the map
        // this is a new type, create a new list
        list = new ArrayList<Document>();

        // store the list in the map
        map.put(d.type, list);
    }

    // finally, whether we got a hit or a miss, we want
    // to add this document to the list for this type
    list.add(d);
}
亣腦蒛氧 2024-10-01 20:12:31

我认为您要查找的术语不是按类型排序,而是按类型索引GuavaMultimap 接口旨在将键映射到多个值,而无需处理集合的所有麻烦价值观。特别是,Guava 有一个方法,旨在完全执行您想要执行的操作:

List<Document> documents = ...
ImmutableListMultimap<Type, Document> typeIndex = Multimaps.index(documents,
    new Function<Document, Type>() {
      public Type apply(Document input) {
        return input.getType();
      }
    });

for(Type type : typeIndex.keySet()) {
  ImmutableList<Document> documentsWithType = typeIndex.get(type);
  ...
}

这与执行: 几乎相同,

ListMultimap<Type, Document> typeIndex = ArrayListMultimap.create();
for(Document document : documents) {
  typeIndex.put(document.getType(), document);
}

只是生成的多重映射是不可变的。另请注意,上面的内容几乎与马克的示例完全相同。

I think rather than sorting by type, the term you're looking for is indexing by type. Guava's Multimap interface is designed for mapping keys to multiple values without all the hassle of dealing with the collections of values. In particular, Guava has a method that's designed to do exactly what you're trying to do:

List<Document> documents = ...
ImmutableListMultimap<Type, Document> typeIndex = Multimaps.index(documents,
    new Function<Document, Type>() {
      public Type apply(Document input) {
        return input.getType();
      }
    });

for(Type type : typeIndex.keySet()) {
  ImmutableList<Document> documentsWithType = typeIndex.get(type);
  ...
}

This is pretty much the same as doing:

ListMultimap<Type, Document> typeIndex = ArrayListMultimap.create();
for(Document document : documents) {
  typeIndex.put(document.getType(), document);
}

except that the resulting multimap is immutable. Note also that the above is almost exactly equivalent to Mark's example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文