Question

I have 2 strings from twitter JSON "follower_count" and "user id"

i need something to sort them according to favourite count in descending order, i.e with greater Follower count gets placed first and then access their "user id" link. and print from 1 to 10.

Best way to do that in java/android with hashmap or linked hashmap or any other ?

Update:- i used sorted maps as suggested, actually Treemap

here is my progress so far:

//get  the first status
        status = statuses.get(0);
     //get id of the status
          long l= status.getId(); 
        //get retweeters id
          ki =twitter.getRetweeterIds(l, 100, -1);

         long[] id=ki.getIDs();
         //for every retweeter id, get followers count and put in treemap
         TreeMap<Integer,Long> tm = new TreeMap<Integer, Long>();
         for(int k=0;k<=id.length;k++)
         {
             u = twitter.showUser(id[k]);
             follower=u.getFollowersCount();
             tm.put(follower,id[k] );
         }
NavigableMap<Integer,Long> reverseTreeMap = tm.descendingMap();
Was it helpful?

Solution 4

Finally i was able to do that by using Treemaps(sorted maps) as suggested in the other answers, thing about tree map is they automatically sort the values by keys. I.e

Treemap<Integer,String> tree =new Treemap<Integer, String>();
    //now put items in it
     tree.put(5,"hello" );
     tree.put(4,"hello again" );
     tree.put(6,"Goodbye" );  

then when we will take output of that it will come as"

  4=hello again
  5=hello
  6=Goodbye

Now the last task is to reverse that order, for that use

NavigableMap<Integer,String> reverseTreeMap = tree.descendingMap();

This is my final code, thats how i solved my problem :

 //Using treemap to sort retweeters according to their followers
 TreeMap<Integer,String> tm = new TreeMap<Integer, String>();
 try{
     for(int k=0;k<10;k++)
         {
             publishProgress(5);
             Log.i(a, "treemap followers"+k);
             u = twitter.showUser(id[k]);
             follower=u.getFollowersCount();
             url=u.getProfileImageURL();
             tm.put(follower,url );
         }
    }
catch(Exception e)
    {
        Log.e(a, e.toString());
    }
Log.i(a, "Done treemap");

 //Reverse the order of the treemap 
 Log.i(a, "Reversing Treemap");
 NavigableMap<Integer,String> reverseTreeMap = tm.descendingMap();
 publishProgress(5);

 //Put treemap values in string
 s=reverseTreeMap.values().toString();
 s=s.replace("[", "");
 s=s.replace("]", "");

     //make array of string s
     s=s.split(",");
  Log.i(a, "Done");

OTHER TIPS

Have you tried SortedMap? it provides ordering on its keys. http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html

If you are looking for specifically at sorting performance alone a HashTable or a TreeMap (actually a red-black binary tree) can be a great resource for sorting performance, but they are slower than some other data structures when adding (and in TreeMap's case, deleting) items.

beside this you can try with SortedMap as well.

actually there is tradeoff always in between as if it consists of records that you can lookup by some key value, then a Map will probably be a good data structure. If it's just a list of things than use a List. On the other hands For example, ArrayList and LinkedList have different performance constarints. an ArrayList is efficient if you need to lookup an element by index, and a LinkedList is not. On the other hand, a LinkedList is efficient if you need to insert elements in random places in the list, while and ArrayList is not. The API documentation of the different collection implementations describes these differences in detail.

Unless you have a huge number of them I would suggest putting your objects in an ArrayList. Sort that based on a custom Comparator and be done with it. For small lists, like yours seem to be, using something more complicated is usually not faster or easier.

-edit-

I also suggest using ArrayList as a default rather then LinkedList. The use case for LinkedList is imo very narrow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top