Question

Write a java program that stores the following information in a HashMap: Sue is friends with...

Write a java program that stores the following information in a HashMap:

Sue is friends with Bob, Jose, Alex, and Cathy

Cathy is friends with Bob and Alex

Bob is friends with Alex, Jose, and Jerry

After storing the information, prompt the user to enter a name. If the name that is entered is Sue, Cathy, or Bob, print out the name and the list of friends. Otherwise print a message indicating that the name is not in the HashMap.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class FriendsList {
   public static void main(String[] args) {
       HashMap<String, List<String>> list = new HashMap<String, List<String>>();
       List<String> frnds = new ArrayList<String>();
       frnds.add("Bob");
       frnds.add("Jose");
       frnds.add("Alex");
       frnds.add("Cathy");
       list.put("Sue", frnds);

       frnds = new ArrayList<String>();
       frnds.add("Bob");
       frnds.add("Alex");
       list.put("Cathy", frnds);

       frnds = new ArrayList<String>();
       frnds.add("Jose");
       frnds.add("Alex");
       frnds.add("Cathy");
       frnds.add("Jerry");
       list.put("Bob", frnds);
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the name: ");
       String name = sc.nextLine();
       sc.close();
       frnds = list.get(name);
       if (frnds == null)
           System.out.println(name + " does not exist in the list");
       else
           System.out.println(frnds);

   }
}

Add a comment
Know the answer?
Add Answer to:
Write a java program that stores the following information in a HashMap: Sue is friends with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a PYTHON program that stores the following amino acids into an array: Trp Arg Liu...

    Write a PYTHON program that stores the following amino acids into an array: Trp Arg Liu Ilu Asp Then prompt the user to give you a three letter amino acid abbreviation. Test to see if the a.a. the user gave you is in the array or not. If it is, print out a message to the user saying "Amino acid found", otherwise print out "Amino acid NOT found". You have to use a loop structure to look up every element...

  • Java Program: Make an application called that stores at least four different movies, a showtime (such...

    Java Program: Make an application called that stores at least four different movies, a showtime (such as "11:00am"), and the movie rating (such as "PG-13") in a two-dimensional array. Movies with multiple showtimes should be in the 2d array multiple times, once for each showing. Prompt the user for a movie name and output all showtimes for that film, or a message indicating it was not found.

  • Create a program using MIPS that does the following: The program will do the following: Prompt...

    Create a program using MIPS that does the following: The program will do the following: Prompt the user to enter a letter indicating Celsius or Fahrenheit C or c indicates that temperature entered is in Celsius. F or f indicates that temperature entered is in Fahrenheit. If the letter entered is not C, c, F, or f, print out error message "Wrong Letter!". Prompt the user to enter an integer indicating the temperature matching the letter just entered. Convert the...

  • in java please Write a program that contains an array with all of your friends' first...

    in java please Write a program that contains an array with all of your friends' first names. Enter the values in any manner and order you choose (I recommend favorites first). Now take that array and sort it alphabetically so that names that start with ‘A' c me first. Look to see if you have any duplicates and if you find any, print out their names. You may use a binary search to determine this. 2. Sample output: Input list...

  • Write a program that does the following in Python Code: Stores the following three lists: last_name...

    Write a program that does the following in Python Code: Stores the following three lists: last_name = ["Smith", "Jones", "Williams", "Bailey", "Rogers", "Pyle", "Rossington"] first_name =["Lisa", "Bill", "Jay", "Sally", "Walter","Jake","Gary"] phone_number =["240-233-1921", "301-394-2745", "571-321-8934", "703-238-3432", "703-947-9987", "301-945-3593", "240-671-3221"] Has a function named combine_lists() that takes in last_names, first_names and phone_numbers as lists and returns a dictionary called phone_book that uses last_name as the key and a list consisting of first_name and phone_number. Has a main function that iterates through the...

  • Create a program that allows a user to enter up to five names of friends. Use...

    Create a program that allows a user to enter up to five names of friends. Use a twodimensional array to store the friends’ names. After each name is entered, the user should have the option to enter another name or print out a report that shows each name entered thus far

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • Please complete this code for java Lab Write a program as follows: Create an array with...

    Please complete this code for java Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a 'match found' message. Also print at which index the match was found, else print a 'match not found' message...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • Write a program that does the following in Python Code: Stores the following three lists: last_...

    Write a program that does the following in Python Code: Stores the following three lists: last_name = ["Smith", "Jones", "Williams", "Bailey", "Rogers", "Pyle", "Rossington"] first_name =["Lisa", "Bill", "Jay", "Sally", "Walter","Jake","Gary"] phone_number =["240-233-1921", "301-394-2745", "571-321-8934", "703-238-3432", "703-947-9987", "301-945-3593", "240-671-3221"] Has a function named combine_lists() that takes in last_names, first_names and phone_numbers as lists and returns a dictionary called phone_book that uses last_name as the key and a list consisting offirst_name and phone_number. Has a main function that iterates through the phone_book...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT