1

I have one Object say

class ABC{
private String name;

private String code;

private BigDecimal priceM;

private BigDecimal priceL;

private BigDecimal priceO;

 public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public BigDecimal getPriceM() {
        return priceM;
    }

    public void setPriceM(BigDecimal priceM) {
        this.priceM = priceM;
    }

    public BigDecimal getPriceL() {
        return priceL;
    }

    public void setPriceL(BigDecimal priceL) {
        this.priceL = priceL;
    }

    public BigDecimal getPriceO() {
        return priceO;
    }

    public void setPriceO(BigDecimal priceO) {
        this.priceO = priceO;
    }



}

Now, say i have a list of ABC and at some point i want that list to filter according to searchCriteria.

Is there any efficient way to achieve this...???

Example ,

I have

AIM : List and i want new List which will contain List of ABC with priceM values between 100 to 200

saidesh kilaru
  • 740
  • 2
  • 10
  • 18
JOHND
  • 2,597
  • 6
  • 27
  • 35
  • efficient in what way? you can easily iterate that list and put condition to check that get filtered objects. As it is user-defined class, you have to check that value while iterating using getter and filter – vishal_aim Dec 24 '12 at 07:42
  • if i use if condition in for loop its not efficient in performance. – JOHND Dec 24 '12 at 08:34
  • did you check/measure it using any profiler that this is the area where you need efficiency? – vishal_aim Dec 24 '12 at 10:42
  • First of all it's bad practice to use "if" inside a for loop... – JOHND Dec 24 '12 at 11:20
  • where is the link for that suggestion? as far as I know we should not do premature optimization – vishal_aim Dec 24 '12 at 11:22
  • I am not sure about java but in sql we avoid you can check out http://blogs.sas.com/content/iml/2012/02/13/avoid-unnecessary-if-then-statements-in-loops/ correct me if i am wrong. – JOHND Dec 24 '12 at 11:40
  • 1
    I dont think it holds good for java... http://stackoverflow.com/questions/4043455/which-costs-more-while-looping-assignment-or-an-if-statement – vishal_aim Dec 24 '12 at 11:50
  • Thanks Vishal...very useful link....!!! – JOHND Dec 24 '12 at 11:51
  • When somebody gives you an answer, how will you know if it is efficient or not? What attributes must it have fir it to be "efficient" for you? I mean, I hope you know what you're looking for, otherwise we're just losing our time. For sure, there is no way to do this without loops or recursion and an if condition. – Edwin Dalorzo Dec 24 '12 at 14:18
  • @EdwinDalorzo : Sorry i don't want to waste your time....Actually i am new in java world so i am learning what to avoid and what to do to become good programmer....Sorry if i have wasted your valuable time...!!! actually i read http://blogs.sas.com/content/iml/2012 which made me bit confuse/02/13/avoid-unnecessary-if-then-statements-in-loops/ – JOHND Dec 24 '12 at 14:27

3 Answers3

1

Google Guava have some classes around Matchers and Predicate maybe that can help

http://java.dzone.com/articles/google-guava-goodness-matching

Maybe you can use reflection to achieve automatic matching

Vinay Lodha
  • 2,185
  • 20
  • 29
1

The new APIs in JDK 8

List<ABC> items = asList(...);
Stream<ABC> stream = items.stream();
stream.filter( item -> item.getPrice() >= 100 && item.getPrice() <= 200 )
      .forEach ( System.out::println );
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
0

Java provide a Collection.sort(list, comp) method which takes a list and Comparator object as argument

In that case pass your original list which need to be sort and create a Comparator object and override the compare method which is rules for sorting.

You can create multiple Comparator object based on your requirement.

Please refer below link for more help.

  1. http://www.vogella.com/blog/2009/08/04/collections-sort-java/

  2. How do I sort a list by different parameters at different timed

Community
  • 1
  • 1
Rais Alam
  • 6,970
  • 12
  • 53
  • 84