java beginner

java beginner

Hi

using hashcode() and equal() method and hashmap object how to compare employee lastname and firstname ,display in console

View Answers

August 17, 2012 at 2:15 PM

Here is an example that uses hashcode() and equal() method and store the employee information into hashmap and display it.

import java.util.*;
public class Employee {
private String firstname;
private String lastname;
public Employee(String firstname, String lastname){
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
        return firstname;
    }
    public String getLastname() {
        return lastname;
    }

public String toString() {
return lastname + ", " + firstname;
}
public int hashCode() {
return this.toString().hashCode();
}
public boolean equals(Object emp) {
if ( emp == null ) return false;
if ( this.getClass() != emp.getClass() ) return false;
String name = ((Employee)emp).toString();
return this.toString().equals(name);
}
public static void main(String[]args){
    HashMap<Integer,Employee> map = new HashMap<Integer,Employee>();
    map.put(1,new Employee("Annie","Smith"));
    map.put(2,new Employee("Angel","De'souza"));
    map.put(3,new Employee("John","Paul"));
    map.put(4,new Employee("Joe","Smith")); 
   for (Employee employee : map.values())
        {
    System.out.println(employee.toString());
        }

}
}









Related Tutorials/Questions & Answers:
java bEGINNER
java bEGINNER  WHAT IS THE BEST WAYS,BOOKS AND pRACTICAL IMPLEMENTATION TECHNIQUES FOR A BEGINNER
java beginner
java beginner  hai a i'm beginner 2 java... i want to fetch data from database using combo box(drop down list) without using javascript...when i click list(ex:countrys should b selected)selected country should get inserted
Advertisements
JAVA BEGINNER
JAVA BEGINNER  IS JAVA PURE OBJECT ORIENTED
Java Beginner
Java Beginner  can we declare a class inside an interface? where this type of declaration required? give one example
java beginner
java beginner  Hi i have a methd that is public static insert ( map p), i have passed a java map object in this method , so i want to store employee infrmation in haspmap and how to find highest salery and afetr public
Java Beginner
Java Beginner  To reverse a every other word in a string. Example :- If the input is "This is a line of message" the output should be "sihT is a line fo message
java beginner
java beginner  Hi using hashcode() and equal() method and hashmap object how to compare employee lastname and firstname ,display in console
java beginner
java beginner  Hi how to sort list of element containing a data object like date of birth of employee. display employee in youngest first and oldest last , using one of the collection class for sorting
java beginner
java beginner  Write a program that will grade the students quiz. The program will ask the user to enter the number of the questions in the quiz, the right answer for each question and save it in array. The program will ask
java beginner
java beginner  HI I am swetha iam confused when to use service(), doget(), dopost() method in servlets   Any request from client are handled initially by the service() method before delegating to the doXxx() methods
Is Java a good beginner language?
Is Java a good beginner language?  Hi, What a beginner should learn? Which programming is best paying and what should I choose? Is Java a good beginner language? I am making my career in programming. I want to select best path
jAVA BEGINNER PROBLEMS
jAVA BEGINNER PROBLEMS  I need the program that takes from standard input an expression without left parenthesis and prints the equivalent infix expression with the parenthesis inserted??? Like EXAMPLE: 1+2)3-4)5-6
Java beginner - Java Beginners
Java beginner  what is the diffrence between abstraction and interface? with coding
What is the right compiler for a java beginner?
What is the right compiler for a java beginner?  Hi, Which compiler... for a java beginner? Thanks   Hi, You should use the JDK for compiling... for a java beginner? Thanks
What is the right compiler for a java beginner?
What is the right compiler for a java beginner?  Hi, Which compiler... for a java beginner? Thanks   Hi, You should use the JDK for compiling... for a java beginner? Thanks
Beginner
Beginner  how to call a gui class from applet class   Hello Friend, Please visit the following link: http://www.roseindia.net/java/example/java/applet/SApplet.shtml Thanks
beginner
beginner  provide a simple java exception program that uses try , catch, throw and finally blocks
How a beginner can start Java Programming?
How a beginner can start Java Programming?  I Just have programming knowledge of C & C++. Let's know how I can learn Java? Is there any Java... have programming language of C and C++ then you can learn Java programming
Tutorial for total beginner in Java
Tutorial for total beginner in Java - Explains the Basics of Java programming language. This is a video tutorial for total beginner in Java. In this video... language. This video is for total beginner in Java which will teach how to create
I am beginner and want to learn Java fast
I am beginner and want to learn Java fast  Hi, I am a beginner in Java and want to learn it quickly. Which is the best tutorials on roseindia.net to learn it fast? Thanks   Hi, In the first place java you should know
I am beginner and want to learn Java fast
I am beginner and want to learn Java fast  Hi, I am a beginner in Java and want to learn it quickly. Which is the best tutorials on roseindia.net to learn it fast? Thanks   Hi, In the first place java you should know
What are the best way to learn Spring & Hibernate Framework for a Java beginner?
What are the best way to learn Spring & Hibernate Framework for a Java beginner?  Hi, What are the best way to learn Spring & Hibernate Framework for a Java beginner? I want to learn these technologies ASAP. Thanks
What are the best way to learn Spring & Hibernate Framework for a Java beginner?
What are the best way to learn Spring & Hibernate Framework for a Java beginner?  Hi, What are the best way to learn Spring & Hibernate Framework for a Java beginner? I want to learn these technologies ASAP. Thanks
What are the best way to learn Spring & Hibernate Framework for a Java beginner?
What are the best way to learn Spring & Hibernate Framework for a Java beginner?  Hi, What are the best way to learn Spring & Hibernate Framework for a Java beginner? I want to learn these technologies ASAP. Thanks
Nag(Beginner)
Nag(Beginner)  sir i am new to java.. i want to know what topics comes under core java, adv. java and also the remaining topics... plz tell me   Java Tutorials
Beginner
beginner
beginner
beginner
beginner
beginner
beginner
Generating Random Numbers to Fill array. Java Beginner needing help!
Generating Random Numbers to Fill array. Java Beginner needing help!  Hello all! I am new to this site, and Java programming. My problem is: Write...() methods. I am new to Java programming and this seems mind blowing to me :( I
Learn Java for beginner
a beginner in Java is exposed to a Java Guide having several chapters...Java is the most used programming language in the world. Java provides... and available to anyone. This is the advantage Java has on other language
Servlet Tutorial for beginner
Servlet Tutorial for beginner  Hi, I am beginner in Servlet. I have learned the JSP a little bit. Now I wan't to learn Servlet. Tell me the URL... developed in Java. Servlet's run on Servlet container. Read more at Servlet
data science beginner jobs
data science beginner jobs  Hi, I am beginner in Data Science... beginner jobs Try to provide me good examples or tutorials links so that I can learn the topic "data science beginner jobs". Also tell me which
beginner data science jobs
beginner data science jobs  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: beginner... learn the topic "beginner data science jobs". Also tell me which
ai beginner course
ai beginner course  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: ai beginner course... the topic "ai beginner course". Also tell me which is the good training
artificial intelligence beginner course
artificial intelligence beginner course  Hi, I am beginner in Data...: artificial intelligence beginner course Try to provide me good examples... beginner course". Also tell me which is the good training courses in Machine
machine learning python beginner
machine learning python beginner  Hi, I am beginner in Data Science...: machine learning python beginner Try to provide me good examples or tutorials links... are beginner in Data Science and searching for the best tutorials for getting
Hibernate programming tutorial for beginner
Hibernate programming tutorial for beginner  Hi, I am beginner in Hibernate and I wish to learn it quickly. I am hoping to learn the latest ORM... the link: Hibernate beginner tutorial. This link contains many tutorials
Java guide
Java guide       Any beginner requires guidance to start with. A beginner in the field of java... practical guide to the beginner and experienced java programmer. These tutorials have
ModuleNotFoundError: No module named 'Beginner_nester'
ModuleNotFoundError: No module named 'Beginner_nester'  Hi, My... named 'Beginner_nester' How to remove the ModuleNotFoundError: No module named 'Beginner_nester' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'for-beginner-piano'
ModuleNotFoundError: No module named 'for-beginner-piano'  Hi, My... named 'for-beginner-piano' How to remove the ModuleNotFoundError: No module named 'for-beginner-piano' error? Thanks   Hi, In your
SQL for Beginner
SQL for Beginner      ... the easiest elaborative examples for the beginner. The beginner will reached into the immediate result as the SQL command is submitted. It bring the beginner
Hibernate beginner tutorial
Hibernate the beginner tutorial - Essential Hibernate tutorials for beginner... will be illustrated how one can easily map Java objects with relational database... will help you in starting with the Hibernate framework. As a beginner in Hibernate
j2me beginner
for php beginner
What's the best way to learn data science as a beginner?
What's the best way to learn data science as a beginner?  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: What's the best way to learn data science as a beginner? Try
Java Tutorial
Java Tutorials If you are a beginner and looking for the Java tutorials to learn java programming language from scratch then this the best place to start with the Java programming language. Java programming language contains huge

Ads