Gnaniyar Zubair
Gnaniyar Zubair

Reputation: 8166

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".

Ex:

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

Upvotes: 2

Views: 12913

Answers (5)

aksarben
aksarben

Reputation: 628

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

Upvotes: 1

Przemek Kryger
Przemek Kryger

Reputation: 687

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()) {
                sorted.remove(idx);
                sorted.add(nf);
            }
        }
    }
    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;
                }
            }
        }
    });
}

Upvotes: 1

Bogdan
Bogdan

Reputation: 412

I would use an ArrayList like this:

ArrayList<Name> listOne = new ArrayList<Name>();
listOne.add(new Name("Nellai", 10);
listOne.add(new Name("Gujarath", 10);
listOne.add(new Name("Delhi", 30);
listOne.add(new Name("Nellai", 5);
listOne.add(new Name("Delhi", 20);

Collection.sort(listOne);

Then create the Name class

    class name implements Comparable
{
private String name;
private int number;

public Name(String name, int number)
{
this.name= name;
this.number= number;
}

public String getName()
{
        return this.name;
}
public int getNumber()
{
        return this.number;
} 
public int compareTo(Object otherName) //  must be defined if we are implementing //Comparable interface
{
 if(otherName instanceif Name)
{
throw new ClassCastException("Not valid Name object"):
}
Name tempName = (Name)otherName;
// eliminate the duplicates when you sort 
if(this.getNumber() >tempName.getNumber())
  {
     return 1;
  }else if (this.getNumber() < tempName.getNumber()){
     return -1;
  }else{
     return 0;
  }
}

}

I didn't compiled the code, it's edited here so you should fix the code. And also to figure out how to eliminate the duplicates and print only the lowest one.

You need to sweat too.

Upvotes: 0

Nicolas
Nicolas

Reputation: 24759

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.

Upvotes: 4

pauljwilliams
pauljwilliams

Reputation: 19225

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.

Upvotes: 1

Related Questions