How to add an object in my collection by only using add method?

14,069
public class DataCollection implements java.io.Serializable{

    private String Name;
    private String Address;
    private int Age;
    public DataCollection(String name, String address, int age){
            this.Name=name;
            this.Address=address;
            this.Age=age;
    }
}

After that create DataCollection objects:

DataCollection d1 = new DataCollection("nik", "10/5 cross", 20);//creation of List collection

Now put the object inside a collection:

List<DataCollection> list = new LinkedList<DataCollection>();
list.add(d1);

You can iterate like below:

List<DataCollection> list=new LinkedList<DataCollection>();
for(DataCollection d : list){
    System.out.println(d);
}
Share:
14,069

Related videos on Youtube

user2988935
Author by

user2988935

Updated on September 27, 2022

Comments

  • user2988935
    user2988935 over 1 year

    I have to create a collection of Name,Address,Age.How do I create this as a collection object in java.

    My snippet is as follows:

    public class DataCollection implements java.io.Serializable{
    
        private String Name;
        private String Address;
        private int Age;
    }
    

    And I have the getter and setter methods...

    In the main method, how do I create this as a collection??

    list.add(new DataCollection(??????) );
    

    Can someone please help me on this?

    • Danny
      Danny over 10 years
      Off topic. In the future you should start classes with an uppercase letter (e.g. DataCollection)
  • user2988935
    user2988935 over 10 years
    @Shreyos,@blubob.. based on your answers, I have updated my code as shown below: Please correct if my understanding is wrong.
  • Gilbert Le Blanc
    Gilbert Le Blanc over 10 years
    No. You instantiate a List with an ArrayList or LinkedList. List<DataCollection> list = new ArrayList<DataCollection>(); You instantiate to an interface when you can. You instantiate to a concrete class when there is no interface or you need to use a method in the concrete class.
  • user2988935
    user2988935 over 10 years
    import java.util.ArrayList; import java.util.List; public final String getName() public final String getAddress() public final int Age() public final void setName(String aName) public final void setAddress(String aAddress) public final void setAge(int aAge) Main Mthod: List<DataCollection> list = new ArrayList<DataCollection>(); DataCollection dataCollection = new DataCollection(); dataCollection .setName("John"); dataCollection .setAddress("Chicago"); dataCollection .setAge(21); list.adddataCollection();
  • user2988935
    user2988935 over 10 years
    When I try to use LinkedList it gives me error like LinkedList is not generic.It cannot be parameterizec with arguments<DataCollection>.
  • Trying
    Trying over 10 years
    @user2988935 You have to import this two: import java.util.LinkedList; import java.util.List;. Make sure you use this LinkedList rather than some arbitrary implementation.
  • user2988935
    user2988935 over 10 years
    @Trying... Thanks a lot. Is there a way to check the objects in the collection? I am using the list.get(i) but it returns the index. Is there anyway I can get the value ?
  • Trying
    Trying over 10 years
    @user2988935 see the edit in the answer.
  • user2988935
    user2988935 over 10 years
    Yes I tried. But I get the arbitrary values like ecd55fca,fa79750e,57b9a57,c83bc96b.... whereas the actual values are different. Kindly help.
  • blueblob
    blueblob over 10 years
    You are printing a pointer to the object. try System.out.println(d.getName()) or override the toString() method
  • Trying
    Trying over 10 years
    @user2988935 override toString() method in the class DataCollection .
  • user2988935
    user2988935 over 10 years
    Thank you so much everyone..@Trying,@bluebob,@Danny,@Ian,@Shreyas.... It worked :-)
  • user2988935
    user2988935 over 10 years
    I am sorry... one more question. How can I add these of objects in Map.. If i give map.update(key,list) will it work?
  • user2988935
    user2988935 over 10 years
    When I try to do it, it shows me ClassNotFoundException. My classpath is all set with correct jars but still the error is shown...
  • user2988935
    user2988935 over 10 years
    This is after adding the Map...