Question

In: Computer Science

Write a program which takes three lists of names, and print out all the names only...

Write a program which takes three lists of names, and print out all the names only once. The expected runtime should be O(n) where n is the total number of names in the lists. You don’t have to sort the list. Use appropriate data structure to store the names, so add all data to the collection only once efficiently. The read-in list method and list1, list2, list3 is given. You may assume that the expected runtime of searching or inserting into a hash table is O(1). Do not call the list's contains method, because it slows down the run time.

import java.io.*;
import java.util.*;
public class A4PrintName{
   public static List<String> readInFile(String filename){
      List<String> input = new ArrayList<>();
      try (Scanner sin = new Scanner(new FileReader(filename))){
         while (sin.hasNextLine()){
            input.add(sin.nextLine());
         }
      } catch (FileNotFoundException e){
         e.printStackTrace();
      }
      return input;
  }
   public static void main(String[] args){
      List<String> namelist1 = readInFile("A4input1.txt");
      List<String> namelist2 = readInFile("A4input2.txt");
      List<String> namelist3 = readInFile("A4input3.txt");
      Set<String> names;
      //your code starts here... you may write any function where you need.
   }
}

list 1:

Amy
Andy
Anna
Ben
Benjamin
Catherine
Emma
James
Jessie
Jennifer
John
Karen
Kelly
Kyle
Lena
Liam
Mary
Mia
Steve
William

list 2:

Amy
Andy
Anne
Ben
Benjamin
Catherine
David
Emma
James
Jessie
Jennifer
Jenny
John
Karen
Kelly
Kyle
Mary
Mia
Selina
Tina
William

list 3:

Amy
Ana
Anne
Dave
George
Selina
Shawn
William

Solutions

Expert Solution

import java.io.*;
import java.util.*;
public class Main{
public static List<String> readInFile(String filename){
List<String> input = new ArrayList<>();
try (Scanner sin = new Scanner(new FileReader(filename))){
while (sin.hasNextLine()){
input.add(sin.nextLine());
}
} catch (FileNotFoundException e){
e.printStackTrace();
}
return input;
}
public static void main(String[] args){
List<String> namelist1 = readInFile("A4input1.txt");
List<String> namelist2 = readInFile("A4input2.txt");
List<String> namelist3 = readInFile("A4input3.txt");
Set<String> names=new HashSet<String>();//create hashset
names.addAll(namelist1);//add all values using addAll method in namelist1
names.addAll(namelist2);//in the same manner add for namelist2,namelist3
names.addAll(namelist3);
System.out.println(names);//print the names

/*You can also use loop upto how many values are there and add each element separately*/
  
}
}

If you have any doubts comment it


Related Solutions

11.10 LAB: All permutations of names PLEASE ANSWER IN C++! Write a program that lists all...
11.10 LAB: All permutations of names PLEASE ANSWER IN C++! Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names (until -1), and use a recursive method to create and output all possible orderings of those names, one ordering per line. When the input is: Julia Lucas Mia -1 then the output is (must match the below ordering): Julia...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
Write a program to demonstrate the use of InetAddress. The program takes a list of names...
Write a program to demonstrate the use of InetAddress. The program takes a list of names or IP addresses as command line parameters and prints the name and an IP address of the local host, followed by names and IP addresses of the hosts specified on the command line. use java program
There are three things wrong with this program. List each. print("This program takes three numbers and...
There are three things wrong with this program. List each. print("This program takes three numbers and returns the sum.") total = 0 for i in range(3):     x = input("Enter a number: ")     total = total + i print("The total is:", x)
PART A Write a C program that takes the names and surnames of the students and...
PART A Write a C program that takes the names and surnames of the students and then displays the initial and last name of the name on the screen. For instance, if Onur Uslu is entered as input, the output of our program must be O. Uslu. PART B Write a C program that asks the user to enter a string and then sends it to a function that does the following job and shows the response from the function...
how to write a cpp program that takes a number from a user, and print the...
how to write a cpp program that takes a number from a user, and print the sum of all numbers from one to that number on screen? using loop interation as basic as possible.
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
Write in C++ Write a program that accepts the names of three political parties and the...
Write in C++ Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.   Be sure to provide labels (party name) and number-align your output values.
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a...
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters.The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH Hint: Use the fgets function to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT