পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Friday, February 3, 2017

Java BeanUtils - BeanUtils and ConvertUtils

Description

The BeanUtils is defined as a utility method for populating JavaBeans properties and ConvertUtils method converts string scalar values to objects, string arrays to arrays of the specified class.

BeanUtils

The BeanUtils accepts string values by using the setter methods and automatically converts them to suitable property types for Java primitives and uses the getter methods for reverse conversion. The populate() method accepts set of property values from java.util.HashMap and uses the suitable setters whenever bean contain the property with the same name.

Example

The below example shows usage of BeanUtils properties:
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;

public class Test {
    @SuppressWarnings("unchecked")
    public static void main(String[] args){
        @SuppressWarnings("rawtypes")
        HashMap map = new HashMap();
        map.put("username","admin");
        map.put("password","secret");
        map.put("age","52");
        
        User bean = new User();
        try{
              BeanUtils.populate(bean,map);
        }catch(Exception e){
              e.printStackTrace();
        }
        
        System.out.println("Username: "+bean.getUsername());
        System.out.println("Password: "+bean.getPassword());
        System.out.println("Age: "+bean.getAge());
    }
}
Now we will create another class called User.java as shown below:
public class User {
    private String username;
    private String password;
    private String age;
    
    public String getUsername(){
        return username;
    }
    
    public void setUsername(String username){
        this.username = username;
    }

    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password){
        this.password = password;
    }

    public String getAge() {
        return age;
    }
 
    public void setAge(String age){
        this.age = age;
    }
}

Output

Let's carry out the following steps to see how above code works:
  • Save the above first code as Test.java.
  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.
 BeanUtils and ConvertUtils

ConvertUtils

The Apache Commons BeanUtils is a library that comes with a number of converters to convert to and from different data types and also contain ConvertUtils utility class which makes use of these converters.

Example

The below example shows the conversion of string array to a double array using ConvertUtils utility:
package com.javadb;
import org.apache.commons.beanutils.ConvertUtils;

public class ConvertStringArrayToDoubleArray {
    public static void main(String[] args) {
        String values[] = { "5", "6", "3" };
        double[] doubleValues = (double[])ConvertUtils.convert(values, Double.TYPE);   
        for (double d : doubleValues) {
            System.out.println(d);
        }
    }
}

Output

  • Save the above first code as ConvertStringArrayToDoubleArray.java.
  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.
ConvertUtils

No comments:

Post a Comment