Skip to content Skip to sidebar Skip to footer

Best Way Or Similar Lambda Expression Or Linq On Android (eclipse)

for example if i have ArrayList productlist = new ArrayList (); how do you get all the products which product.code equals to 1 and 2? is there a lib

Solution 1:

No lambda expressions in Android (or Java) yet.

You might want to see these options (haven't tested them in Android though):

Also, see this DZone link and this one. A stackoverflow post mentions this.

Update:

Lambda expression are now available in Java SE 8 For more information visit:

Lambda Expressions

Solution 2:

As shown in this introductory article Java 8 Stream on Android — Medium, there are two gems that seem to play really nicely together to provide the kind of LINQ-like experience you're asking for.

These are called retrolambda and Stream.

Added bonus, they don't need Java 8, so using them is compatible with old versions of Android.

Your C# code:

productlist
.where(product=> product.code.contains(arraywithproductcode))
.ToList();

Would turn to something like this:

productlist
.filter(product -> product.code.contains(arraywithproductcode))
.collect(Collectors.toList());

With the usual remark: "Do you really need to turn it into a list at the end? Perhaps the lazily-evaluated stream is what you actually want."

For other examples, use aNNiMON/Android-Java-8-Stream-Example: Demo app of using Java 8 features with Retrolambda and Lightweight-Stream-API

We use that here with Android Studio, but I would be surprised if it did not work with Eclipse.

Solution 3:

Depending on your use case you could either create a new list, iterate over the old one and add the ones matching your criteria to your new list.

If you want to avoid that and can live with the limitations of Guavas Collections2.filter() method, you can do this:

Collection<Product> filtered = Collections2.filter(productlist, newPredicate<Product>() {
    @Overridepublicbooleanapply(Product p) {
        return p.code == 1 || p.code == 2;
    }
});

Solution 4:

Maybe you could try this Java library: https://code.google.com/p/qood/

It handles data without any getter/setters, so it's more flexible than LINQ.

in your Product case, it might looks like:

final String[] productCodeArray = { ... };
finalQModelproducts= ...; //QModel {PROD_CODE, PROD_FIELD1, PROD_FIELD2, ...}finalQModelfiltered= products.query()
  .where(QNew.<String>whereIn("PROD_CODE").values(productCodeArray))
  .result().debug(-1);

Post a Comment for "Best Way Or Similar Lambda Expression Or Linq On Android (eclipse)"