1

I am currently using AsyncTask() to fetch rss feeds from 3 different xmls like this,

for (int i = 0; i < urls.length; i++) {
                GetDates currentBlog = new GetDates(i);
                currentBlog.execute();
            }

and I am using DOM parser to read dates from each xml feed like this,

eElement.getElementsByTagName(pubDate).item(0).getTextContent())

all the dates are fetched as String and then converted into Date object using SimpleDateFormatter and then stored in ArrayList.

Since the dates are fetched from 3 different feeds and stored in ArrayList, the ArrayList isn't sorted,

Here is the data in the ArrayList

// First Feed
Sun Jan 18 22:07:35 GMT+05:00 2015
Sat Jan 17 06:02:05 GMT+05:00 2015
Sat Jan 17 01:34:10 GMT+05:00 2015
Fri Jan 16 22:58:32 GMT+05:00 2015
Fri Jan 16 01:56:06 GMT+05:00 2015
Thu Jan 15 21:46:18 GMT+05:00 2015
Thu Jan 15 01:57:09 GMT+05:00 2015
Wed Jan 14 22:47:15 GMT+05:00 2015
Wed Jan 14 01:05:22 GMT+05:00 2015
Tue Jan 13 21:59:15 GMT+05:00 2015
Tue Jan 13 17:14:11 GMT+05:00 2015
Tue Jan 13 15:03:06 GMT+05:00 2015
Tue Jan 13 12:31:12 GMT+05:00 2015
Tue Jan 13 06:01:12 GMT+05:00 2015
// Second Feed Starts here
Sat Jan 17 01:56:00 GMT+05:00 2015
Fri Jan 16 19:23:30 GMT+05:00 2015
Fri Jan 16 05:07:00 GMT+05:00 2015
Fri Jan 16 09:07:00 GMT+05:00 2015
Wed Jan 14 02:44:00 GMT+05:00 2015
// Third Feed Starts here
Sat Jan 17 01:56:00 GMT+05:00 2015
Fri Jan 16 19:23:30 GMT+05:00 2015
Fri Jan 16 05:07:00 GMT+05:00 2015
Fri Jan 16 09:07:00 GMT+05:00 2015
Wed Jan 14 02:44:00 GMT+05:00 2015

I wanted to sort that ArrayList so I used Comparator class to do that, but the problem is that I have to wait until the data from all 3 feeds are loaded before I can start sorting.

this is what i am doing,

if (allfeedsareloaded == true)
   // Start sorting here

I learned that I can use TreeSet class to keep the object sorted at all times but I have no clue how to use it?

Is there any other way to keep data sorted at all times without using a lot of memory or time ?

Joe
  • 1,488
  • 3
  • 16
  • 27
  • possible duplicate of [Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?](http://stackoverflow.com/questions/4903611/java-list-sorting-is-there-a-way-to-keep-a-list-permantly-sorted-automatically) – alex2410 Jan 19 '15 at 15:33
  • 1
    Thats not a duplicate, the question you mentioned is more general and I am asking about implementing TreeSet on my situation. – Joe Jan 19 '15 at 15:38

2 Answers2

2

If you already have your comparator working properly, using TreeSet is as simple as

   Set<String> ts = new TreeSet<String>(yourComparator);
   ts.add(yourStringDate);

Also since you mentioned that you're already convert to dates you should follow what @SubOptimal says in his answer, you don't need a custom compartor, date instances will be sorted by their natural order. For what concerns your questions inside commments, you should initialize a TreeSet before parsing the feeds, and at a moment when you convert an node value to Date store inside, so in a pseudo code

    SortedSet sortedSet = Collections.synchronizedSortedSet(new TreeSet<Date>());
    asyncTask1 // fetch parse and add to set from source 1
    asyncTask2 // fetch parse and add to set from source 2
    asyncTask3 // fetch parse and add to set from source 3
    System.out.println(datesSet); // no further action elements are already 

From what you described your set can get updated concurrently so initialiaze it as SortedSet sortedSet = Collections.synchronizedSortedSet(new TreeSet<Date>());

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • how it will help with my situation, could you please add just few detail ? – Joe Jan 19 '15 at 15:39
  • also, where to add this code ? after all feeds loaded ? – Joe Jan 19 '15 at 15:43
  • I've added a pseudo code, the main point is to just initialize a TreeSet before parsing your feeds, than after the point you convert the node value to a Date instance just add it, it will sort automatically – Master Slave Jan 19 '15 at 15:58
  • also make sure that you change the generic type of `SortedSet` to `Date`, -- `SortedSet` – Sufiyan Ghori Jan 19 '15 at 16:34
2

If you create a TreeSet it will keep cares about the natural order of the dates.

edit update the example to include a solution with a sorted ArrayList

Calendar cal = GregorianCalendar.getInstance();

Set<Date> datesSet = new TreeSet<>();
cal.set(2015, Calendar.MAY, 10);
datesSet.add(cal.getTime());

cal.set(2015, Calendar.MAY, 1);
datesSet.add(cal.getTime());
datesSet.add(cal.getTime());

cal.set(2015, Calendar.MAY, 5);
datesSet.add(cal.getTime());

// all May dates
System.out.printf("%nTreeSet: output in natural order, no duplicates%n");
for (Date d : datesSet) {
    System.out.println(d);
}

List<Date> datesList = new ArrayList<>();
cal.set(2015, Calendar.MAY, 10);
datesList.add(cal.getTime());

cal.set(2015, Calendar.MAY, 1);
datesList.add(cal.getTime());
datesList.add(cal.getTime());

cal.set(2015, Calendar.MAY, 5);
datesList.add(cal.getTime());

System.out.printf("%nArrayList: output in insertion order, duplicates possible%n");
for (Date d : datesList) {
    System.out.println(d);
}

Collections.sort(datesList);
System.out.printf("%nArrayList: output in natural order, duplicates possible%n");
for (Date d : datesList) {
    System.out.println(d);
}
SubOptimal
  • 22,518
  • 3
  • 53
  • 69