Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group arraylist items by date in Android

In my program I have an array list of type Program like this:

   List<Program> programList= new ArrayList<Program>(); 

public class Program {

public String name;
public String date;

public Program(String name, String date) {
    this.name = name;
    this.date= date;
}
    public String date getDate()
    {
     return date;
     }

    public String date setDate(String date)
    {
     this.date=date;
     }

      public String date getName()
    {
     return name;
     }

    public String date setName(String name)
    {
     this.name=name;
     }

and in my activity i am adding items to the list

      for(int i=0; i < 100; i++)
      {
       Program p= new Program("name","some date");
       programList.add(p):
      }

And I want to group items by date, like there are hundred items in list and many of them have same date, I want to make pairs with the date and the new lists of the items having same date.

like image 741
Munazza Avatar asked Jan 30 '26 20:01

Munazza


2 Answers

You can use an HashMap and use as key the String date, as value you can use an ArrayList<Program> . HashMap.get(key) will return null if an object with that key does not exists. If it returns null you should create a new ArraList and put it inside the HashMap with that key. If it does not you should use the object returned to add the Programm instance eg

  HashMap<String, ArrayList<Programm>> myProgram = new  HashMap<String, ArrayList<Programm>>() ;
  for(int i=0; i < 100; i++)
  {
    String date = "your date";
     ArrayList<Programm> programList = myProgram.get(date);
     if (programList == null) {
         programList = new ArrayList<Programm>();
         myProgram.put(date, programList);
    }
     Program p= new Program("name","some date");
     programList.add(p):
  }

check for typo

like image 152
Blackbelt Avatar answered Feb 01 '26 08:02

Blackbelt


First of all, your Program class should look like this:

private String name;
private String date;

public Program(String name, String date) {
    this.name = name;
    this.date = date;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

If you have public fields like you had, its pointless to make getters and setters.

Anyhow, you could use HashMap for sorting programs by date:

private static class Bucket {

    private List<Program> programs = new ArrayList<Program>();

    private static HashMap<String, Bucket> map = new HashMap<String, Bucket>();

    public static void addProgram(Program p) {
        String tempDate = p.getDate();
        Bucket correspondingBucket = map.get(tempDate);
        if (correspondingBucket == null) {
            correspondingBucket = new Bucket();
            map.put(tempDate, correspondingBucket);
        }
        correspondingBucket.programs.add(p);

    }
}
like image 30
jellyfication Avatar answered Feb 01 '26 09:02

jellyfication



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!