Bean এর প্রপার্টিগুলিকে Map এ কনভার্ট করা

Bean Map Creation (বিন ম্যাপ তৈরি করা) - জাভা বীনইউটিলস (Java BeanUtils) - Computer Programming

395

Java Bean এর প্রপার্টিগুলিকে Map এ কনভার্ট করা একটি সাধারণ কাজ যা অনেক সময় প্রয়োজন হয়, যেমন ফর্ম ডেটা, JSON ডেটা ইত্যাদি সংগ্রহ ও ট্রান্সফারের জন্য। Apache Commons BeanUtils লাইব্রেরি ব্যবহার করে একটি Java Bean এর প্রপার্টিগুলি একটি Map-এ কনভার্ট করা খুবই সহজ।

BeanUtils.populate() এবং BeanUtils.describe() মেথড

  1. BeanUtils.describe():
    • এই মেথড ব্যবহার করে একটি Bean এর প্রপার্টি গুলিকে একটি Map<String, String>-এ কনভার্ট করা যায়।
    • এটি একটি Map রিটার্ন করে, যেখানে প্রপার্টির নাম (key) এবং মান (value) স্ট্রিং আকারে থাকে।
  2. BeanUtils.populate():
    • এটি একটি Map এর ডেটা দিয়ে Bean populate করতে ব্যবহৃত হয়।

Map এ Bean এর প্রপার্টিগুলি কনভার্ট করার উদাহরণ:

উদাহরণ: BeanUtils.describe() ব্যবহার করে Bean কে Map এ কনভার্ট করা

import org.apache.commons.beanutils.BeanUtils;
import java.util.Map;
import java.util.HashMap;

public class BeanToMapExample {
    public static void main(String[] args) {
        try {
            // Create a new Person object (Bean)
            Person person = new Person("John", 25);

            // Convert Bean to Map
            Map<String, String> personMap = BeanUtils.describe(person);

            // Output the Map
            for (Map.Entry<String, String> entry : personMap.entrySet()) {
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Person {
    private String name;
    private int age;

    // Constructor, getter and setter methods
    public Person() {}

    public Person(String name, int age) {
        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;
    }
}

ব্যাখ্যা:

  • BeanUtils.describe(person) মেথডটি Person Bean এর প্রপার্টিগুলি একটি Map<String, String> এ কনভার্ট করে। এখানে, key হল প্রপার্টির নাম (যেমন "name", "age") এবং value হল তাদের মান (যেমন "John", "25").
  • Map আউটপুট হবে:

    name: John
    age: 25

BeanUtils.populate() ব্যবহার করে Map থেকে Bean Populate করা

এই উদাহরণে আমরা দেখব কিভাবে একটি Map এর ডেটা দিয়ে Java Bean populate করা যায়:

import org.apache.commons.beanutils.BeanUtils;
import java.util.Map;
import java.util.HashMap;

public class MapToBeanExample {
    public static void main(String[] args) {
        try {
            // Create a Map with property names and values
            Map<String, String> personMap = new HashMap<>();
            personMap.put("name", "Alice");
            personMap.put("age", "28");

            // Create a new Person object (Bean)
            Person person = new Person();

            // Populate the Person object using the Map
            BeanUtils.populate(person, personMap);

            // Output the populated Bean properties
            System.out.println("Name: " + person.getName());  // Output: Alice
            System.out.println("Age: " + person.getAge());    // Output: 28
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Person {
    private String name;
    private int age;

    // Constructor, getter and setter methods
    public Person() {}

    public Person(String name, int age) {
        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;
    }
}

ব্যাখ্যা:

  1. একটি Map তৈরি করা হয়েছে, যেখানে key হচ্ছে Bean এর প্রপার্টির নাম এবং value হচ্ছে সেই প্রপার্টির মান।
  2. BeanUtils.populate() মেথড ব্যবহার করে Map এর ডেটা দিয়ে Person Bean populate করা হয়েছে।
  3. পরে person.getName() এবং person.getAge() ব্যবহার করে কপি করা ডেটা প্রদর্শন করা হয়েছে।

Nested Properties Map থেকে Bean Populate করা

ধরা যাক, আপনি একটি Person Bean এর মধ্যে একটি Address Bean রাখতে চান। এর মাধ্যমে দেখানো হবে কিভাবে নেস্টেড প্রপার্টির ডেটা populate করতে হয়।

import org.apache.commons.beanutils.BeanUtils;
import java.util.Map;
import java.util.HashMap;

public class NestedBeanToMapExample {
    public static void main(String[] args) {
        try {
            // Create a Map with nested Address data
            Map<String, String> addressMap = new HashMap<>();
            addressMap.put("street", "Street 456");
            addressMap.put("city", "City B");

            Map<String, Object> personMap = new HashMap<>();
            personMap.put("name", "John");
            personMap.put("age", "32");
            personMap.put("address", addressMap); // Nested Map

            // Create a Person object (Bean)
            Person person = new Person();

            // Populate Person object using the Map
            BeanUtils.populate(person, personMap);

            // Output the populated Bean properties
            System.out.println("Name: " + person.getName());             // Output: John
            System.out.println("Age: " + person.getAge());               // Output: 32
            System.out.println("Street: " + person.getAddress().getStreet());  // Output: Street 456
            System.out.println("City: " + person.getAddress().getCity());      // Output: City B

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Person {
    private String name;
    private int age;
    private Address address;

    public Person() {}

    // Getter and setter methods
    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;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

class Address {
    private String street;
    private String city;

    public Address() {}

    // Getter and setter methods
    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

ব্যাখ্যা:

  1. Nested Map তৈরি করা হয়েছে যেখানে Address Bean এর প্রপার্টি রয়েছে (যেমন "street", "city").
  2. BeanUtils.populate() মেথড ব্যবহার করে Person Bean এবং তার নেস্টেড Address Bean populate করা হয়েছে।
  3. পরে, Person এবং Address Bean এর প্রপার্টি গুলি অ্যাক্সেস করা হয়েছে।

সারাংশ:

  • BeanUtils.describe() এবং BeanUtils.populate() মেথডের মাধ্যমে Java Bean এর প্রপার্টিগুলি Map থেকে কনভার্ট করা এবং Map থেকে Bean populate করা যায়।
  • describe() মেথড Bean এর প্রপার্টি গুলি Map-এ কনভার্ট করে, এবং populate() মেথড Map থেকে Java Bean এর প্রপার্টি populate করতে ব্যবহৃত হয়।
  • Nested properties সাপোর্ট করে, যা Bean এর মধ্যে অন্য Bean থাকা সত্ত্বেও কাস্টম ডেটা populate করতে সক্ষম।
Content added || updated By
Promotion

Are you sure to start over?

Loading...