Java BeanUtils Tutorial

Java BeanUtils – Querying Or Filtering Collections

Java BeanUtils &-8211; Querying Or Filtering Collections &-8211; this Article or News was published on this date:2019-05-12 20:19:28 kindly share it with friends if you find it helpful

Java BeanUtils &-8211; Querying Or Filtering Collections


Advertisements


Description

The collections of beans can be filtered in the commons-collections by using the interface Predicate and also provides either true or false value on the evaluation of an input object. There is a Predicate called BeanPropertyValueEqualsPredicate which will assess the set property value against the given value.

Syntax

public BeanPropertyValueEqualsPredicate(String propertyName, Object propertyValue)

The above syntax has two parameters, which decides what property to be evaluated and what should be its expected value. It creates a Predicate for evaluating the target object and returns true if the value specified by propertyName is equal to the value specified by the propertyValue; otherwise it returns false.

The property names are defined by org.apache.commons.beanutils.PropertyUtils and can be simple, indexed, nested or mapped.

For instance, you can filter a collection of beans where myCar property is false:

// create the closure
BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate( "myCar", Boolean.FALSE );
	
// filter the collection
CollectionUtils.filter( myCollection, predicate );

The above code filters the &-8216;myCollection&-8217; collection and returns the boolean value for the object&-8217;s myCar property.



Advertisements

Java BeanUtils &-8211; Home

Java BeanUtils &-8211; Overview

Background

Basic Property Access

Nested Property Access

Customizing Introspection

Suppressing Properties

Background

Basic DynaBeans

ResultSetDynaClass

RowSetDynaClass

WrapDynaBean

Lazy DynaBeans

Background

BeanUtils and ConvertUtils

Create Custom Converters

Locale Aware Conversions

Utility Objects & Classes

Comparing Beans

Operating On Collections

Querying Or Filtering Collections

Transforming Collections

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who

Java BeanUtils – Transforming Collections

Java BeanUtils &-8211; Transforming Collections &-8211; this Article or News was published on this date:2019-05-12 20:19:28 kindly share it with friends if you find it helpful

Java BeanUtils &-8211; Transforming Collections


Advertisements


Description

The conversion from input object to output object is supported in commons-collections with the help of Transformer interface. The Transformers can be applied to get the output collection from input collection with the help of codes available in Commons-collections. BeanToPropertyTransformer is an example of Transformer that will convert a bean to its property value. This feature is able to extract a particular property from the input object and present it as output object.

If you try to find out a particular car model from many car users in a collection. The syntax is shown below:

 
    // creating transformer
     BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer( "person.company.carmodel" );

     // transforming Collection
     Collection carModel = CollectionUtils.collect( peopleCollection, transformer );


Advertisements

Java BeanUtils &-8211; Home

Java BeanUtils &-8211; Overview

Background

Basic Property Access

Nested Property Access

Customizing Introspection

Suppressing Properties

Background

Basic DynaBeans

ResultSetDynaClass

RowSetDynaClass

WrapDynaBean

Lazy DynaBeans

Background

BeanUtils and ConvertUtils

Create Custom Converters

Locale Aware Conversions

Utility Objects & Classes

Comparing Beans

Operating On Collections

Querying Or Filtering Collections

Transforming Collections

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who

Java BeanUtils – Comparing Beans

Java BeanUtils &-8211; Comparing Beans &-8211; this Article or News was published on this date:2019-05-12 20:19:27 kindly share it with friends if you find it helpful

Java BeanUtils &-8211; Comparing Beans


Advertisements


Description

In Apache Commons Beanutils, you can compare the JavaBean objects by using the BeanComparator class based on a specified shared property value. This can be done by using the org.apache.commons.beanutils.BeanComparator comparator.

Example

The below example shows how to compare the two different beans. We will be creating two objects and set the first object to &-8220;BMW&-8221; and the other object to &-8220;AUDI&-8221;. Then, we will compare the objects by using the BeanComparator by calling its compare() method.

Note: For BeanComparator, commons-collection and commons-logging jar files need to be included.

package com.javadb.apachecommons.beanutils;
import org.apache.commons.beanutils.BeanComparator;

public class BeanComparatorExample {
    public static void main(String[] args) {
        Car car1 = new Car();
        car1.setBrand("BMW");
        
        Car car2 = new Car();
        car2.setBrand("AUDI");
        
        BeanComparator comparator = new BeanComparator("brand");
        
        System.out.println("The value after comparing two beans is: " + comparator.compare(car1, car2));
    }
}

Now we will create one more class with the below code and save it as Car.java.

package com.javadb.apachecommons.beanutils;

public class Car {
    private String brand;
	
    public String getBrand() {
        return brand;
    }
    
    public void setBrand(String brand) {
        this.brand = brand;
    }
}

Output

  • Save the above first code as BeanComparatorExample.java.

  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

Comparing Beans



Advertisements

Java BeanUtils &-8211; Home

Java BeanUtils &-8211; Overview

Background

Basic Property Access

Nested Property Access

Customizing Introspection

Suppressing Properties

Background

Basic DynaBeans

ResultSetDynaClass

RowSetDynaClass

WrapDynaBean

Lazy DynaBeans

Background

BeanUtils and ConvertUtils

Create Custom Converters

Locale Aware Conversions

Utility Objects & Classes

Comparing Beans

Operating On Collections

Querying Or Filtering Collections

Transforming Collections

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who

Java BeanUtils – Operating On Collections

Java BeanUtils &-8211; Operating On Collections &-8211; this Article or News was published on this date:2019-05-12 20:19:27 kindly share it with friends if you find it helpful

Java BeanUtils &-8211; Operating On Collections


Advertisements


Description

The Commons-Collections are build upon interfaces, implementations and utilities. It contains Closure interface in the code that can be applied on the arbitrary input object and code permits to apply Closures to contents of collection. There is Closure called BeanPropertyValueChangeClosure sets specified property to specified value. This value can be combined with commons-collections in which all the beans will have specified property to specified value in the collection.

For instance, you can set the myCar property to TRUE for entire collection as shown below:

 
 //creating the closure
 BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure( "myCar", Boolean.TRUE );

 //updating the Collection
 CollectionUtils.forAllDo( myCollection, closure );


Advertisements

Java BeanUtils &-8211; Home

Java BeanUtils &-8211; Overview

Background

Basic Property Access

Nested Property Access

Customizing Introspection

Suppressing Properties

Background

Basic DynaBeans

ResultSetDynaClass

RowSetDynaClass

WrapDynaBean

Lazy DynaBeans

Background

BeanUtils and ConvertUtils

Create Custom Converters

Locale Aware Conversions

Utility Objects & Classes

Comparing Beans

Operating On Collections

Querying Or Filtering Collections

Transforming Collections

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who

Java BeanUtils – Locale Aware Conversions

Java BeanUtils &-8211; Locale Aware Conversions &-8211; this Article or News was published on this date:2019-05-12 20:19:26 kindly share it with friends if you find it helpful

Java BeanUtils &-8211; Locale Aware Conversions


Advertisements


Description

The regular classes available in org.apache.commons.beanutils are not assigned for any specific event. These classes provide a clear interface to make use of situations very easily where the locale is not main thing. You can use Locale-aware extensions of beanutils classes which helps localization, from the org.apache.commons.beanutils.locale package.



Advertisements

Java BeanUtils &-8211; Home

Java BeanUtils &-8211; Overview

Background

Basic Property Access

Nested Property Access

Customizing Introspection

Suppressing Properties

Background

Basic DynaBeans

ResultSetDynaClass

RowSetDynaClass

WrapDynaBean

Lazy DynaBeans

Background

BeanUtils and ConvertUtils

Create Custom Converters

Locale Aware Conversions

Utility Objects & Classes

Comparing Beans

Operating On Collections

Querying Or Filtering Collections

Transforming Collections

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who