2

I want to iterate a list in a specify order using the Interface Iterator. In this case, I want to iterate the list (listofproducts) in descending product.prize order.

public class Invoice {

    private static int static_id;

    private int id;
    private String date;
    private List<Product> listofproduct = new ArrayList<Product>();
    private boolean open;
}


public class Product {

    private static int count = 0;

    private int code;
    private String name;
    private String description;
    private double price;
}

I have a public method to get the price. How can I solve this?

4
  • 1
    You'll have to write a comparator, sort the list with this comparator (which is quite expesive) and the iterate the sorted list Commented Jan 25, 2016 at 12:45
  • Any chances to do it without sorting the list? Commented Jan 25, 2016 at 12:46
  • It really depends on what you are trying to do. If you want to go through the list in a particular order, then you will most likely need to sort it. Commented Jan 25, 2016 at 12:48
  • @MassimoLavermicocca i don't think you can do it without sorting the list. The only other possibility is to keep an already sorted list and keep the sorting every time you insert an element (which is more expensive at inserting) Commented Jan 25, 2016 at 12:50

3 Answers 3

2

If the amount of data isn't too big you can do the following without thinking about the performace:

List<Product> sortList = new ArrayList<>(origList);
Collections.sort(sortList, new Comparator<Product>() {
    @Override
    public int compare(Product arg0, Product arg1) {
        return (int)(arg1.getPrice() - arg0.getPrice());
    }
});

This will create a copy of the original list which will be sorted by a comparator. After that, iterating over sortList will be sorted by price (descending)

Sign up to request clarification or add additional context in comments.

Comments

2

You should use the Comparable<T> interface to implement your comparaison logic :

 class Product implements Comparable<Product>{

    private static int count = 0;

    private int code;
    private String name;
    private String description;
    private Double price;


    @Override
    public int compareTo(Product p) {
        return price.compareTo(p.getPrice());
    }
}

Now to sort the list you can simply use :

Collections.sort(listOfProduct);

2 Comments

Implementing the interface Comparable has another advantage: No need for getters! You can just directly access the private fields
@ParkerHalo Yes and we use essentially the Comparator when we have several comparaison logic to implements for the same object what is undoable using the Comparator :)
0

There are two way to achieve that. With Comparator it is easy to use, you should create a new implementation of the interface Comparator and then implement the compare and equals methods. It is really powerful because you can create several comparator to sort in different fields.

You can also make you Product as Comparable and then implements the compareTo method.

You can see sample with camparator and comparable interfaces

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.