Cool way to manipulate collections in Java

Hello friends, Today We’re talking about Purify4J which is a Java library to manipulate collections. In my recent project, I’ve to filter the Collection list and for this need to write some verbose code. And reuse that verbose code is nonsense work for me. Then Purify4J born and work very well for me. I hope it is helpful for you as for me.

Why use Purify4J?

 

If you want to iterate over a collection is the easiest way to filtering, mapping, and order. It is a cool way to manipulate collections in Java.

How to use Purify4J in Java Project?

Implementation

It’s easy to use. Just add import static com.github.geekonjava.Refination.*; in your class and that’s it!

See More:
ScribeJava – OAuth client Java library
Nowadays every project need social registration for attract more users but...

Animal.java

public class Animal {
  
  private String name;
  private int age;
  
  public Animal() {}
  
  public Animal(String name, int age) {
    super();
    this.name = name;
    this.age = age;
  }
  
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}

For Filter

First, We’ve Collection. Here we create an Animal List, and we called it animals.

List<Animal> animals = new ArrayList<>();
    animals.add(new Animal("Lion",23));
    animals.add(new Animal("Elephant",24));
    animals.add(new Animal("Cat",23));
    animals.add(new Animal("Tiger",25));
    animals.add(new Animal("Dog",23));
    animals.add(new Animal("Donkey",21));
    animals.add(new Animal("Human",23));

Now, you want to take all cats, it’s easy for Purify4J! In this case, name is a method (animal.name()).

See More:
Why Design Patterns important in Java
When you build software, you have to face lots of...
from(animals).where("name", eq("Cat")).all();

Or, would the first animal with 2 years old? Easy too!

from(animals).where("age", eq(2)).first();

 

[tmh_article_ads]

For Advance Filter

You can be more specific in your query, adding more specifications, like and and or.

//with AND
from(animals).where("name", eq("Lion")).and("age", eq(2)).all();

//with OR
from(animals).where("name", eq("Dog")).or("age", eq(5)).all();

For Matching

There are other matchers to be precise!

eq("Cat")
eqIgnoreCase("Cat")
contains("og")
greaterThan(3)
lessThan(10)
isNull()

Or a special matcher, called not.

not(eq("Bird"))
not(contains("at"))
not(isNull())

For Ordering

An order is a very interesting feature to sort your collection.

//Order by Column by default
from(animals).where("name", eq("Cat")).orderBy("age").all();

//Order by Column with Descending order
from(animals).where("age", eq(5)).orderBy("name", Order.DESC).first();

You can use just order, without the filter.

from(animals).orderBy("name");

Hope you like this library and useful for you! Just share with your friends.

By Tell Me How

It is a technology blog and admin has excellent experience in programming from 5+ year. You can contact us at ceo.tellmehow@gmail.com

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...