0

I started with C# and now I'll work with to Java. I need to implement a simple linq query(filter + get) in Java.

The C# equivalent could be:

public interface IPriceRule
{
    bool IsMatch(OrderItem item);
    decimal CalculatePrice(OrderItem item);
}
//
_pricingRules = new List<IPriceRule>();
//...
public decimal CalculatePrice(OrderItem item)
{
    return _pricingRules.First(r => r.IsMatch(item)).CalculatePrice(item);
}

Knowing that java doesn't have Linq, the first thing that come to me are extension methods, but also did not exist. Then I have to do all (Maybe I get bad habits for cause of the simplicity):

@Override
public BigDecimal CalculatePrice(OrderItem item) {
    IPriceRule _selectedRule = getFirstItemThatMatch(_pricingRules, item);
    if(_selectedRule != null)
        return _selectedRule.CalculatePrice(item);
    else
        return BigDecimal.ZERO;
}

private IPriceRule getFirstItemThatMatch(List<IPriceRule> prmCollection, OrderItem item) {
    IPriceRule _result = null;
    for(IPriceRule itemPriceRule : prmCollection) {
        if(itemPriceRule.IsMatch(item)) {
            _result = itemPriceRule;
            break;
        }
    }
    return _result;
}

I am not convinced that this java code are equivalent, optimal. Maybe exist a technique to optimize and improve it in terms of algorithmic complexity and coding speed.

I believe that .net optimize and normalize the queries and bring it to you with linq, compared with the code that you could write.

Erick Asto Oblitas
  • 1,399
  • 3
  • 21
  • 47
  • 1
    You can just `return itemPricRule` from within the for-loop. That takes away four lines of code. – Marko Topolnik Dec 29 '13 at 21:55
  • Have you looked at `Guava`?. Sidenote: The `time-complexity` tag seems a tad bit out of place. – reto Dec 29 '13 at 21:55
  • Side note: "I believe that .net optimize and normalize the queries" is somewhat true only for LINQ-to-SQL (where LINQ expressions are rewritten as SQL queries), LINQ-to-XML/LINQ-to-Objects don't do any optimizations of this kind (most LINQ queries use "lazy evaluation", but it is different). – Alexei Levenkov Dec 29 '13 at 22:09

2 Answers2

1

As you know, LINQ is not available in Java (reference).

But you can use any ORM for database layer as Hibernate/JPA. In .NET I used nHibernate, which is equivalent to hibernate in Java (so I get off pretty easy).

But LINQ is not ORM (abstraction class) so you won't be having a full glass.

Community
  • 1
  • 1
Gun2sh
  • 870
  • 12
  • 22
0

Loosely speaking, I guess you need to look into using some ORM in Java. For example Hibernate or Entity EJB or JPA in general. These are in some way similar to LINQ to SQL for C#. You don't need to do all this yourself in Java. Things are different in Java but Java offers more in that respect IMHO... And if you're just looking for lambda expressions in general, you might check this.

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

But lambda expressions will only be available in Java 8, it seems.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159