Tuesday 2 June 2015

How to storing Java Object In LinkedHashSet



import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetOfObject {

   
    public static void main(String[] args) {
       
        Set<User> lhs = new LinkedHashSet<User>();
        lhs.add(new User("shivam",100));
        lhs.add(new User("yogesh", 200));
        lhs.add(new User("aanchal",500));
        lhs.add(new User("raja", 400));
        lhs.add(new User("ankush",700));
       
        Iterator<User> itr =lhs.iterator();
       
        while (itr.hasNext()) {
            User user = (User) itr.next();
            System.out.println("User Name = "+user.getName()+" User Id = "+user.getId() );
        }
       
    }
}

class User{
   
    private String name;
    private int id;
   
    public User(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
   
   
     
}

No comments:

Post a Comment