| Author |
Sorting in List<Hash Map>
|
Vass Lee
Greenhorn
Joined: Nov 16, 2011
Posts: 8
|
|
Hi All ,
I i have the data in below format:
Name Age Skill Company
Vass 21 Java Zylog
Samy 24 PB HP
Lee 18 ADF CTS
Reng 16 Java Info
I converted this data into java collections List<Hash Map> like this.
As per data (table format) i want to sort the data in Column level
how can i to achieve ?.
List<HashMap> is type of collection is help to me?
Any idea?
Thanks,
Vass Lee
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4756
|
|
Vass Lee wrote:As per data (table format) i want to sort the data in Column level
List< HashMap> is type of collection is help to me?
You'd be much better off creating a class to hold that data (Person?) and then just creating a List<Person>. If you make your Person class Comparable, you can just sort it as is; otherwise one of the Collections.sort() methods also takes a Comparator.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Vass Lee
Greenhorn
Joined: Nov 16, 2011
Posts: 8
|
|
Hi Winston,
Thanks for your reply. Can you provide any sample for Creating Person class and how to hold the data in that class.?
I am new guy in java
Thanks,
Vass Lee,
|
 |
Nomaan Butt
Ranch Hand
Joined: Oct 19, 2011
Posts: 54
|
|
as winston suggested, make a Person class like below
and create a list of type Person and add objects to it like this
you can change the compareTo() method according to your need
hope this may help you.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4756
|
|
Nomaan Butt wrote:make a Person class like below
@Vass: the only slight change I'd make to Nomaan's suggestion is not to use Strings for everything. 'Age' is plainly a numeric value, so make it one, viz:and I suspect you'll also discover that something like "skill" is actually a code that is stored elsewhere in your database. If it is, then make it a class (or an enum) too. If not, forget what I said and just use a String.
HIH
Winston
|
 |
Vass Lee
Greenhorn
Joined: Nov 16, 2011
Posts: 8
|
|
Hi Winston,
Thanks for your reply.Now i want to apply some filters in column level .how can i apply filter in person class? any filter method is there in java.util. package? can you provide any sample here ?
Thanks,
Vass Lee
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Now i want to apply some filters in column level .how can i apply filter in person class?
What do you mean by column level? Nomaan's code shows how to sort based on the variable name.... You want to do any more addition to the compareTo() method?
|
 |
Vass Lee
Greenhorn
Joined: Nov 16, 2011
Posts: 8
|
|
Hi John,
Thanks for your reply.As per Nomaan's i can able to sort the variable level.Now i want filter the records in variable level.
Thanks,
Vass Lee
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Sorry Vass - i am not understanding what do you mean by filtering...
Might be you want to iterate the List<Person> and take a particular person object if the name variable has a particular value (say "Vass"?)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Create a java.util.Comparator<Person> implementation for all the specific ways you want to compare the Person objects.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4756
|
|
Vass Lee wrote:Thanks for your reply.Now i want to apply some filters in column level .how can i apply filter in person class? any filter method is there in java.util. package? can you provide any sample here ?
Just a small point: you're in Java now and you have a class, so it's best not to think in terms of "columns" any more. Databases have columns, Java has fields (or attributes). Assuming that, per Nomaan's example, you have a java.util.List of Persons, a filtering method might look something like this:however, filters and sorts being what they are, there are endless possibilities (and you might even want to combine the two), so as the others say you should probably look at java.lang.Comparable and java.util.Comparator as well.
Also, Java has a dizzying array of collection types, some of which are very powerful, so I suggest you have a look at the tutorials and get familiar with them.
HIH
Winston
|
 |
 |
|
|
subject: Sorting in List<Hash Map>
|
|
|