Solved

How do you use long in a HashMap?

Posted on 2011-09-15
9
250 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

12 Experts available now in Live!

Get 1:1 Help Now