The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java : Collection Framework : HashSet (Add UserDefined Object)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java : Collection Framework : HashSet (Add UserDefined Object) Posted: Dec 19, 2014 7:50 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java : Collection Framework : HashSet (Add UserDefined Object)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=RTC-WPdjweE&list=UUhwKlOVR041tngjerWxVccw

Employee.java
public class Employee
{

private String name;
private int age;
private int salary;

public Employee( String name, int age, int salary )
{
super();
this.name = name;
this.age = age;
this.salary = salary;
}

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 int getSalary()
{
return salary;
}

public void setSalary( int salary )
{
this.salary = salary;
}

@Override
public String toString()
{
return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
+ "]";
}

}

HashSetExample.java
import java.util.HashSet;

/*
* Storing user-defined class objects.
*/

public class HashSetExample
{
public static void main(String[] args)
{
HashSet<Employee> hashSet = new HashSet<Employee>();

Employee john = new Employee("John", 32, 40000);
Employee david = new Employee("David", 42, 80000);
Employee peter = new Employee("Peter", 52, 150000);

hashSet.add(john);
hashSet.add(david);
hashSet.add(peter);

System.out.println("hashSet : " + hashSet + "\n");

/*
* Using for each loop getting each employee object from the hashSet
*/

for (Employee employee : hashSet)
{
System.out.println("-------------------------------------");
System.out.println(employee.toString());
System.out.println("Name : " + employee.getName());
System.out.println("Age : " + employee.getAge());
System.out.println("Salary : " + employee.getSalary());
}

}
}

Output
hashSet : [Employee [name=Peter, age=52, salary=150000], Employee [name=John, age=32, salary=40000], Employee [name=David, age=42, salary=80000]]

-------------------------------------
Employee [name=Peter, age=52, salary=150000]
Name : Peter
Age : 52
Salary : 150000
-------------------------------------
Employee [name=John, age=32, salary=40000]
Name : John
Age : 32
Salary : 40000
-------------------------------------
Employee [name=David, age=42, salary=80000]
Name : David
Age : 42
Salary : 80000

To Download HashSetDemoUserDefined Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/HashSetDemoUserdefined.rar?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Read: Java : Collection Framework : HashSet (Add UserDefined Object)

    Topic: EAGER fetching is a code smell Previous Topic   Next Topic Topic: Day 3 of 30 Min Project 12/17/14

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use