Solved

How do you use long in a HashMap?

Posted on 2011-09-15
9
259 Views
Last Modified: 2012-05-12
I am trying to use long instead of integer to create a phoneBook. It keeps saying its an integer and its to large.

import java.util.HashSet;
import java.util.HashMap;
/**
 * Write a description of class MapTester here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class PhoneBook
{
private HashMap<String, Long> phoneBook = new HashMap<String, Long> ();
private HashSet<String, Long> phoneBook = new HashSet<String, Long>();

public PhoneBook()
{
phoneBook.put("Homer Jay Simpson", 53193924587);
phoneBook.put("Peter Griffin", 53154321945);
phoneBook.put("Apu Nahasapeemapetilon", 53142344418);
}


/**
 * Method that takes two parameters the name and telephone number and then attempts to add information into the phonebook
 */
public void phoneBook(String name, long number)

{
if(phoneBook.put(name, number);
}


public String lookupNumber(String name)

{
return phoneBook.get(name);
}
}
0
Comment
Question by:ryanbecker24
9 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36544718
You shoul put letter L in the end of the long number
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36544722
phoneBook.put("Homer Jay Simpson", 53193924587L);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36544725
Otherwise it does not know that it is long
0
 

Author Comment

by:ryanbecker24
ID: 36544748
Ok thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 36544814
how about this

phoneBook.put("Apu Nahasapeemapetilon", new Long(53142344418));
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36544952
with autoboxing you don't need to create new Long(...), as soon as compiler understands
that you are providing long number and for that you need to add "L" or "l"
In general if you write numeber without decimal point Java will treat it as integer, untill you add "L" or "l" to it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36545175
A phone number is not an integral value - it's a String, so you need Map<String, String>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36545193
For a more durable design, you need something other than someone's name as the key in your Map, or the phone book will break if you have duplicate names
0
 

Expert Comment

by:osmanad
ID: 36551786
It is best to initialize Long class with given integer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now