How to Add and Fetch Class Data from List in Java

  • One of the most concept which is used in interview question as well as in program level . because if we add object into list and print the reference it will print whole data not referece
  • But if we add Class level data it will print reference so we need to perform type casting so easily we can fetch the record and print it 
programme:-



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package highleveljava;

import java.util.*;

/**
 *
 * @author Admin
 */
public class GenericType {
    /*
     Global Variable
     */

    public static String customername;
    public static String customerid;
    public static String customerlocation;

    /*
     Setter and getter
     */
    public static String getCustomername() {
        return customername;
    }

    public static void setCustomername(String customername) {
        GenericType.customername = customername;
    }

    public static String getCustomerid() {
        return customerid;
    }

    public static void setCustomerid(String customerid) {
        GenericType.customerid = customerid;
    }

    public static String getCustomerlocation() {
        return customerlocation;
    }

    public static void setCustomerlocation(String customerlocation) {
        GenericType.customerlocation = customerlocation;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        /*
         set the variable value
         */
        GenericType classobject = new GenericType();
        classobject.setCustomername("Anand");
        classobject.setCustomerid("101");
        classobject.setCustomerlocation("Bangalore");
        /*
         Add Class reference into list
         */
        List<GenericType> list = new ArrayList<GenericType>();
        list.add(classobject);
    
        /*
         Fetch class level data
         */
        customername = ((GenericType) list.get(0)).getCustomername();
        customerid = ((GenericType) list.get(0)).getCustomerid();
        customerlocation = ((GenericType) list.get(0)).getCustomerlocation();

        /*
         Lets Print the data
         */
        System.out.println("\t" + customername + "\t" + customerid + "\t" + customerlocation);
    }

}



Thanks  for More Details Comment here !  

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Online Book Store Website Using Java

How to read CSV File in Java