ArrayList grouping?

Discussion in 'Java' started by news.t-com.hr, May 21, 2009.

  1. How can I group values in ArrayList

    Let's say I have List<Example> list = new ArrayList<Example>();

    and Example is

    public class Example{

    private Date date;
    private Integer no;
    private Integer salary;
    }

    and I have initial data

    01.01.2009 , 1 , 10000
    02.01.2009, 1, 20000
    01.01.2009, 2, 30000

    and I want to transform this to two new lists so I can have in the first
    arrayList grouped values by distinct date, and third row summed;

    01.01.2009, 40000
    02.01.2009, 20000

    and second list distinct date+no and third row summed;

    01.01.2009 , 1, 10000
    01.01.2009, 2, 30000
    02.01.2009, 1 , 20000

    How can i do this transformation ?

    Thanks in advance.




    __________ Information from ESET NOD32 Antivirus, version of virus signature database 4092 (20090520) __________

    The message was checked by ESET NOD32 Antivirus.

    http://www.eset.com
     
    news.t-com.hr, May 21, 2009
    #1
    1. Advertisements

  2. In article <gv36ca$abr$-com.hr>,
    "news.t-com.hr" <> wrote:

    > How can I group values in ArrayList
    >
    > Let's say I have List<Example> list = new ArrayList<Example>();
    >
    > and Example is
    >
    > public class Example{
    >
    > private Date date;
    > private Integer no;
    > private Integer salary;
    > }
    >
    > and I have initial data
    >
    > 01.01.2009 , 1 , 10000
    > 02.01.2009, 1, 20000
    > 01.01.2009, 2, 30000
    >
    > and I want to transform this to two new lists so I can have in the first
    > arrayList grouped values by distinct date, and third row summed;
    >
    > 01.01.2009, 40000
    > 02.01.2009, 20000
    >
    > and second list distinct date+no and third row summed;
    >
    > 01.01.2009 , 1, 10000
    > 01.01.2009, 2, 30000
    > 02.01.2009, 1 , 20000
    >
    > How can i do this transformation ?


    Well, you can loop through the list and create whatever cross-tabulation
    you want: for (Example e : list) { ... }. In the first case, a HashMap
    can be used to group records by date; in the second, Collections.sort()
    with a suitable Comparator can handle the sorting. As Date implements
    Comparable, the required Comparator is particularly straightforward.

    Personally, I'd set up and query a relational database:

    <http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, May 21, 2009
    #2
    1. Advertisements

  3. thanks, i'll try that

    "Piotr Kobzda" <> wrote in message
    news:gv3mgu$bhk$...
    > news.t-com.hr wrote:
    >
    > [...]
    >> How can i do this transformation ?

    >
    > There are many solutions to your problem possible.
    >
    > One, a kind of general solution, may be using the following simple
    > utility:
    >
    >
    > import java.util.HashMap;
    > import java.util.Map;
    > import java.util.Map.Entry;
    >
    > /**
    > * @param <E> the type of elements being grouped
    > * @param <K> the type of grouping key
    > * @param <V> the type of value associated with each group
    > */
    > public abstract class Grouper<E, K, V>
    > {
    > private static class ValueHolder<V>
    > {
    > V value;
    >
    > ValueHolder(V value)
    > {
    > this.value = value;
    > }
    > }
    >
    > private Map<K, ValueHolder<V>> map = new HashMap<K, ValueHolder<V>>();
    >
    > protected abstract K keyOf(E e);
    > protected abstract V createValue(E e, K key);
    > protected abstract V updateValue(E e, K key, V value);
    >
    > public void add(E e)
    > {
    > K key = keyOf(e);
    > ValueHolder<V> valueHolder = map.get(key);
    > if (valueHolder == null)
    > {
    > valueHolder = new ValueHolder<V>(createValue(e, key));
    > map.put(key, valueHolder);
    > }
    > else
    > {
    > valueHolder.value = updateValue(e, key, valueHolder.value);
    > }
    > }
    >
    > public Map<K, V> toMap()
    > {
    > Map<K, V> res = new HashMap<K, V>();
    > for (Entry<K, ValueHolder<V>> e : map.entrySet())
    > {
    > res.put(e.getKey(), e.getValue().value);
    > }
    > return res;
    > }
    >
    > }
    >
    >
    > The implementation provided may be redesigned and/or improved depending on
    > needs (especially implementing Iterable<Entry<K, V>> to allow direct
    > iteration of the inner map might be useful). But the idea presented seems
    > to be flexible enough in many typical grouping scenarios.
    >
    >
    > Below is an example based on your initial posting demonstrating intended
    > use of my simple utility:
    >
    >
    > import java.text.DateFormat;
    > import java.text.ParseException;
    > import java.text.SimpleDateFormat;
    > import java.util.ArrayList;
    > import java.util.Arrays;
    > import java.util.Date;
    > import java.util.List;
    > import java.util.Map.Entry;
    >
    > public class GrouperDemo
    > {
    >
    > static class Example
    > {
    > Date date;
    > Integer no;
    > Integer salary;
    >
    > static final DateFormat dateFmt = new SimpleDateFormat("d.M.y");
    >
    > Example(String date, Integer no, Integer salary)
    > throws ParseException
    > {
    > this(dateFmt.parse(date), no, salary);
    > }
    >
    > Example(Date date, Integer no, Integer salary)
    > {
    > this.date = date;
    > this.no = no;
    > this.salary = salary;
    > }
    >
    > @Override
    > public String toString()
    > {
    > return String.format("[%tF, %s, %s]", date, no, salary);
    > }
    > }
    >
    >
    > static class DateNo
    > {
    > Date date;
    > Integer no;
    >
    > DateNo(Date date, Integer no)
    > {
    > this.date = date;
    > this.no = no;
    > }
    >
    > @Override
    > public boolean equals(Object obj)
    > {
    > return obj instanceof DateNo && equals((DateNo) obj);
    > }
    >
    > public boolean equals(DateNo other)
    > {
    > return date.equals(other.date) && no.equals(other.no);
    > }
    >
    > @Override
    > public int hashCode()
    > {
    > return date.hashCode() ^ no.hashCode();
    > }
    >
    > @Override
    > public String toString()
    > {
    > return String.format("[%tF, %s]", date, no);
    > }
    > }
    >
    >
    > public static void main(String[] args) throws Exception
    > {
    > List<Example> list
    > = Arrays.asList(
    > new Example("01.01.2009", 1, 10000),
    > new Example("02.01.2009", 1, 20000),
    > new Example("01.01.2009", 2, 30000));
    >
    > System.out.println(list);
    >
    >
    > Grouper<Example, Date, Integer> gruper1
    > = new Grouper<Example, Date, Integer>()
    > {
    >
    > @Override
    > protected Date keyOf(Example e)
    > {
    > return e.date;
    > }
    >
    > @Override
    > protected Integer createValue(Example e, Date key)
    > {
    > return e.salary;
    > }
    >
    > @Override
    > protected Integer updateValue(Example e, Date key, Integer value)
    > {
    > return value + e.salary;
    > }
    >
    > };
    >
    >
    > Grouper<Example, DateNo, Integer> gruper2
    > = new Grouper<Example, DateNo, Integer>()
    > {
    >
    > @Override
    > protected DateNo keyOf(Example e)
    > {
    > return new DateNo(e.date, e.no);
    > }
    >
    > @Override
    > protected Integer createValue(Example e, DateNo key)
    > {
    > return e.salary;
    > }
    >
    > @Override
    > protected Integer updateValue(Example e, DateNo key,
    > Integer value)
    > {
    > return value + e.salary;
    > }
    >
    > };
    >
    > for (Example e : list)
    > {
    > gruper1.add(e);
    > gruper2.add(e);
    > }
    >
    > List<DateNo> res1 = new ArrayList<DateNo>();
    > for (Entry<Date, Integer> e : gruper1.toMap().entrySet())
    > {
    > res1.add(new DateNo(e.getKey(), e.getValue()));
    > }
    >
    > List<Example> res2 = new ArrayList<Example>();
    > for (Entry<DateNo, Integer> e : gruper2.toMap().entrySet())
    > {
    > res2.add(
    > new Example(e.getKey().date, e.getKey().no, e.getValue()));
    > }
    >
    >
    > System.out.println(res1);
    > System.out.println(res2);
    > }
    > }
    >
    >
    > piotr
    >
    > __________ Information from ESET NOD32 Antivirus, version of virus
    > signature database 4095 (20090521) __________
    >
    > The message was checked by ESET NOD32 Antivirus.
    >
    > http://www.eset.com
    >
    >
    >




    __________ Information from ESET NOD32 Antivirus, version of virus signature database 4096 (20090522) __________

    The message was checked by ESET NOD32 Antivirus.

    http://www.eset.com
     
    news.t-com.hr, May 22, 2009
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,032
    Natty Gur
    Aug 19, 2003
  2. JemPower
    Replies:
    0
    Views:
    753
    JemPower
    Aug 29, 2003
  3. Craig Buchanan

    Outlook-style grouping?

    Craig Buchanan, Oct 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    469
    Craig Buchanan
    Oct 14, 2003
  4. Brian Henry

    radio buttons on form grouping question

    Brian Henry, Nov 20, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    5,235
    Jon Paugh
    Nov 20, 2003
  5. Craig Buchanan

    datagrid grouping?

    Craig Buchanan, Dec 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    456
    Craig Buchanan
    Dec 3, 2003
  6. Kaidi
    Replies:
    4
    Views:
    3,020
    Kaidi
    Jan 3, 2004
  7. xz
    Replies:
    16
    Views:
    2,679
  8. Philipp
    Replies:
    6
    Views:
    1,224
    Arne Vajhøj
    May 28, 2008
Loading...