拆分Java的ArrayList中所包含对象的属性 [英] Split Java ArrayList by properties of contained Objects

查看:175
本文介绍了拆分Java的ArrayList中所包含对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有个约会值的的ArrayList 包含对象。
现在我想设法创建一个新的的ArrayList 为包含所有从主的ArrayList 具有对象每年同年的日期值。

I have an ArrayList containing Objects that have a date value. Now I want to manage to create a new ArrayList for each year that contains all the Objects from the main ArrayList that have the same year in their date Value.

因此​​,所有的另一个从2010年去的对象在一个列表中,全部来自1999年。

So all Objects from 2010 go in one List, all from 1999 in another.

推荐答案

您需要一个<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/Map.html\"><$c$c>Map<Year,List<DatedObject>>,甚至 的SortedMap TreeMap的

You need a Map<Year,List<DatedObject>>, maybe even SortedMap like a TreeMap.

您也可以使用<一个href=\"http://guava-libraries.google$c$c.com/svn-history/r8/trunk/javadoc/com/google/common/collect/Multimap.html\">multimap番石榴。

从本质上讲,你每年从地图(也许只是一个整数)的列表&LT; D​​atedObject&GT; ,在属于那年。

Essentially you map from each year (perhaps just an Integer), the List<DatedObject> that belong in that year.

而自从几年有一种天然的排序顺序,您可能想看看的SortedMap 为您提供您需要的功能。最有可能的答案是肯定的。

And since years have a natural sorting order, you may want to see if SortedMap provides you with functionalities that you need. Most likely the answer is yes.

下面是在Java中的一个片段,显示您如何填充地图。还需要注意的是<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/NavigableMap.html\"><$c$c>NavigableMap是用来代替的SortedMap ; 的NavigableMap 允许包含的范围查询(见相关的问题)。

Here's a snippet in Java that shows you how you can populate the map. Note also that NavigableMap is used instead of SortedMap; NavigableMap allows inclusive range queries (see related question).

    class DatedObject {
        final String name; final int year;
        DatedObject(String name, int year) {
            this.name = name; this.year = year;
        }
        @Override public String toString() {
            return String.format("%s (%d)", name, year);
        }
    }
    List<DatedObject> masterList = Arrays.asList(
        new DatedObject("A", 2010),
        new DatedObject("B", 2009),
        new DatedObject("C", 2006),
        new DatedObject("D", 2010),
        new DatedObject("E", 2009),     
        new DatedObject("F", 2011)          
    );
    NavigableMap<Integer,List<DatedObject>> objectsByYear =
        new TreeMap<Integer,List<DatedObject>>();
    for (DatedObject obj : masterList) {
        List<DatedObject> yearList = objectsByYear.get(obj.year);
        if (yearList == null) {
            objectsByYear.put(obj.year, yearList = new ArrayList<DatedObject>());
        }
        yearList.add(obj);          
    }
    System.out.println(objectsByYear);
    // prints "{2006=[C (2006)], 2009=[B (2009), E (2009)],
    //          2010=[A (2010), D (2010)], 2011=[F (2011)]}"
    System.out.println(objectsByYear.get(2011));
    // prints "[F (2011)]"
    System.out.println(objectsByYear.subMap(2007, true, 2010, true));
    // prints "{2009=[B (2009), E (2009)], 2010=[A (2010), D (2010)]}"

相关问题


  • How当仅支持半开区间做包括的范围查询(ALA SortedMap.subMap)

  • Related questions

    • How to do inclusive range queries when only half-open range is supported (ala SortedMap.subMap)
    • 如果你绝对在列表与LT坚持;列表与LT; D​​atedObject&GT;&GT; partitionedList ,然后建立映射同上,只需用遵循它:

      If you absolutely insists on a List<List<DatedObject>> partitionedList, then build the map as above, and simply follow it with:

      List<List<DatedObject>> partitionedList =
          new ArrayList<List<DatedObject>>(objectsByYear.values());
      System.out.println(partitionedList);
      // prints "[[C (2006)], [B (2009), E (2009)], [A (2010), D (2010)], [F (2011)]]"
      


      的multimap中示例

      您也可以使用 Multimap之番石榴和<一个href=\"http://guava-libraries.google$c$c.com/svn-history/r8/trunk/javadoc/com/google/common/collect/Multimaps.html#index%28java.lang.Iterable,%20com.google.common.base.Function%29\"><$c$c>Multimaps.index实用方法如下:


      The MultiMap Example

      You can also use Multimap from Guava, and Multimaps.index utility method as follows:

          Multimap<Integer,DatedObject> mmap = Multimaps.index(
              masterList,
              new Function<DatedObject, Integer>(){
                  @Override public Integer apply(DatedObject from) {
                      return from.year;
                  }
              }
          );
          System.out.println(mmap);
          // prints "{2010=[A (2010), D (2010)], 2009=[B (2009), E (2009)],
          //          2006=[C (2006)], 2011=[F (2011)]}"
      

      API链接

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆