Sorted List in Java

I need to sort the list in java as below:

List contains collection of objects like this,

List list1 = {obj1, obj2,obj3,.....};

I need the final list which has "lowest value" and "repetition of name should avoid".


List list1 = {[Nellai,10],[Gujarath,10],[Delhi,30],[Nellai,5],[Gujarath,15],[Delhi,20]}

After Sorting , I need the list like this :

List list1 = {[Nellai,5],[Gujarath,10],[Delhi,20]};

I have 2 Delhi (30,20) in my list. But I need only one Delhi which has lowest fare (20).

How to do that it in java?

Gnaniyar Zubair


Almost the same as @Visage answer, but the order is different:

public class NameFare {
    private String name;
    private int fare;
    public String getName() {
        return name;
    public int getFare() {
        return fare;
    @Override public void equals(Object o) {
        if (o == this) {
            return true;
        } else if (o != null) {
            if (getName() != null) {
                return getName().equals(o.getName());
            } else {
                return o.getName() == null;
        return false;
public Collection<NameFare> sortAndMerge(Collection<NameFare> toSort) {
    ArrayList<NameFare> sorted = new ArrayList<NameFare>(toSort.size());
    for (NameFare nf : toSort) {
        int idx = sorted.getIndexOf(nf); 
        if (idx != -1) {
            NameFare old = sorted.get(idx);
            if (nf.getFare() < old.getFare()) {
    Collections.sort(sorted, new Comparator<NameFare>() {
        public int compare(NameFare o1, NameFare o2) {
            if (o1 == o2) {
                return 0;
            } else {
                if (o1.getName() != null) {
                    return o1.getName().compareTo(o2.getName()); 
                } else if (o2.getName() != null) {
                    return o2.getName().compareTo(o1.getName()); 
                } else {
                    return 0;

If order doesn't matter, a solution is to use a Map[String, Integer], add an entry each time you find a new town, update the value each time the stored value is less than the stored one and then zip all the pairs into a list.

I would do it in two stages.

Firstrly sort the list using a custom comparator.

Secondly, traverse the list and, for duplicate entries (which will now be adjacent to each other, provided you worte your comparator correctly), remove the entries with the higher values.

If you want to avoid duplicates, perhaps a class like TreeSet would be a better choice than List.

Need Your Help

Thread not Updating Progress Bar on GlassPane

java swing graphics graphics2d

In the following application I have put a button ,clicking on which makes the GlassPane visible and a Thread starts which updates the Progress bar value.Below is the code:-

ToolBar not visible in 9930 simulator

blackberry toolbar

I've added a bottom toolbar using a ToolbarManager which shows in the 9800 and 9520(which have a similar screen size) simulator but not in the 9930 simulator. I haven't tested this on an actual 993...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.