• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

How do you use long in a HashMap?

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
ryanbecker24
Asked:
ryanbecker24
1 Solution
 
for_yanCommented:
You shoul put letter L in the end of the long number
0
 
for_yanCommented:
phoneBook.put("Homer Jay Simpson", 53193924587L);
0
 
for_yanCommented:
Otherwise it does not know that it is long
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ryanbecker24Author Commented:
Ok thanks
0
 
Gurvinder Pal SinghCommented:
how about this

phoneBook.put("Apu Nahasapeemapetilon", new Long(53142344418));
0
 
for_yanCommented:
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
 
CEHJCommented:
A phone number is not an integral value - it's a String, so you need Map<String, String>
0
 
CEHJCommented:
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
 
osmanadCommented:
It is best to initialize Long class with given integer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now