Question: java beginner

Ads

 
 
 

Share on Google+Share on Google+

asmit
java beginner
1 Answer(s)      4 years and 9 months ago
Posted in : Java Beginners

Hi

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

Ads
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());
        }

}
}

Ads









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  Hi using hashcode() and equal() method and hashmap object how to compare employee lastname and firstname ,display in console
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 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
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
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
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
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
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
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
more. One way to learn Java is through online training. Here a beginner in Java...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
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
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
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 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
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
Tutorials   
Java Spring Hibernate Struts Training What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly?

Ads

 
Advertisement null

Ads