0% found this document useful (0 votes)
15 views3 pages

Lab 5 A

Uploaded by

elan29102004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Lab 5 A

Uploaded by

elan29102004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Started on Friday, 26 April 2024, 5:18 PM

State Finished
Completed on Tuesday, 30 April 2024, 2:15 PM
Time taken 3 days 20 hours
Marks 30.00/30.00
Grade 100.00 out of 100.00
Name 730422243022 CHANDRU S
Question 3
Write a program to read a set of strings during runtime using a Hashset S1, LinkedHashSet S2 and
Correct
TreeSet S3 which will be able to store only Strings. Create a printAll() method and pass S1, S2, S3 which
Mark 10.00 out
will print all the elements and observe the inference.
of 10.00
Note: Size of the set is 6.

For example:

Input Result

Infosys HCL Wipro TechMahindra Using HashSet: [HCL, Wipro, Infosys, TechMahindra,
Hexaware Altran Altran, Hexaware]
Using LinkedHashSet: [Infosys, HCL, Wipro, TechMahindra,
Hexaware, Altran]
Using TreeSet: [Altran, HCL, Hexaware, Infosys,
TechMahindra, Wipro]

Krish Ram Venkat Govind Madhav Using HashSet: [Mukund, Krish, Madhav, Govind, Venkat,
Mukund Ram]
Using LinkedHashSet: [Krish, Ram, Venkat, Govind,
Madhav, Mukund]
Using TreeSet: [Govind, Krish, Madhav, Mukund, Ram,
Venkat]

Answer: (penalty regime: 0 %)


1 ▼ import java.util.HashSet;
2 import java.util.LinkedHashSet;
3 import java.util.TreeSet;
4 import java.util.Scanner;
5 import java.util.Set;
6
7 ▼ public class SetExample {
8 ▼ public static void main(String[] args) {
9 Scanner scanner = new Scanner(System.in);
10 Set<String> set1 = new HashSet<>();
11 Set<String> set2 = new LinkedHashSet<>();
12 Set<String> set3 = new TreeSet<>();
13 ▼ for (int i = 0; i < 6; i++) {
14 String input = scanner.next();
15 set1.add(input);
16 set2.add(input);
17 set3.add(input);
18 }
19 scanner.close();
20 printAll("Using HashSet: ", set1);
21 printAll("Using LinkedHashSet: ", set2);
22 printAll("Using TreeSet: ", set3);
23 }
24
25 ▼ public static void printAll(String message, Set<String> set) {
26 System.out.print(message);
27 System.out.println(set);
28 }
29 }
Input Expected Got

 Infosys HCL Wipro Using HashSet: [HCL, Wipro, Using HashSet: [HCL, Wipro, 
TechMahindra Infosys, TechMahindra, Infosys, TechMahindra,
Hexaware Altran Altran, Hexaware] Altran, Hexaware]
Using LinkedHashSet: Using LinkedHashSet:
[Infosys, HCL, Wipro, [Infosys, HCL, Wipro,
TechMahindra, Hexaware, TechMahindra, Hexaware,
Altran] Altran]
Using TreeSet: [Altran, HCL, Using TreeSet: [Altran, HCL,
Hexaware, Infosys, Hexaware, Infosys,
TechMahindra, Wipro] TechMahindra, Wipro]

 AAA BBB CCC DDD EEE Using HashSet: [AAA, CCC, Using HashSet: [AAA, CCC, 
FFF BBB, EEE, DDD, FFF] BBB, EEE, DDD, FFF]
Using LinkedHashSet: [AAA, Using LinkedHashSet: [AAA,
BBB, CCC, DDD, EEE, FFF] BBB, CCC, DDD, EEE, FFF]
Using TreeSet: [AAA, BBB, Using TreeSet: [AAA, BBB,
CCC, DDD, EEE, FFF] CCC, DDD, EEE, FFF]

 Krish Ram Venkat Using HashSet: [Mukund, Using HashSet: [Mukund, 


Govind Madhav Mukund Krish, Madhav, Govind, Krish, Madhav, Govind,
Venkat, Ram] Venkat, Ram]
Using LinkedHashSet: [Krish, Using LinkedHashSet: [Krish,
Ram, Venkat, Govind, Madhav, Ram, Venkat, Govind, Madhav,
Mukund] Mukund]
Using TreeSet: [Govind, Using TreeSet: [Govind,
Krish, Madhav, Mukund, Ram, Krish, Madhav, Mukund, Ram,
Venkat] Venkat]

 SriShakthi Institute Using HashSet: [Engineering, Using HashSet: [Engineering, 


of Engineering And Institute, Technology, And, Institute, Technology, And,
Technology SriShakthi, of] SriShakthi, of]
Using LinkedHashSet: Using LinkedHashSet:
[SriShakthi, Institute, of, [SriShakthi, Institute, of,
Engineering, And, Engineering, And,
Technology] Technology]
Using TreeSet: [And, Using TreeSet: [And,
Engineering, Institute, Engineering, Institute,
SriShakthi, Technology, of] SriShakthi, Technology, of]

Passed all tests! 

▸ Show/hide question author's solution (Java)


Correct
Marks for this submission: 10.00/10.00.

You might also like