Question

In: Computer Science

The aim of this lab is to learn the way of converting an ARRAY to a...

The aim of this lab is to learn the way of converting an ARRAY to a SET and the other way around. To complete this lab we need to remember the implementation of the Set Interface, HashSet Class, and Arrays.

  1. Build an array with some elements (String, Integer, or Float).
  2. Then initialize a set (using HashSet) with the elements of the converted array. To transfer an Array into a Set, firstly we have to convert it to a List using asList() method as HashSet accepts a List as a constructor.
  3. Build a direct conversion over the same Array to be a Set by using the stream() method, instead of converting an Array to a List then to a Set as been done on Sept 2.

Solutions

Expert Solution

Java code for your requirements given below,

Java program:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class ArrayToSet
{
public static void main(String aa[])
{
   String courses[]= {"cse","ece","eee","it","mech"};

//Convert using asList method
   Set<String> set = new HashSet<String>(Arrays.asList(courses));
   System.out.println(set);

//Convert using stream
   Set<String> set1= Arrays.stream(courses).collect(Collectors.toSet());
   System.out.println(set1);
  
}
}

Output should be displaying like this,

Note:

I printed set objects directly, If u want to print set values one by one use Iterator like this,

Iterator value=set.iterator();
   while(value.hasNext())
   {
       System.out.println(value.next());
   }


Related Solutions

Learn by Doing Matched Pairs: In this lab you will learn how to conduct a matched...
Learn by Doing Matched Pairs: In this lab you will learn how to conduct a matched pairs T-test for a population mean using StatCrunch. We will work with a data set that has historical importance in the development of the T-test. Paired T hypothesis test: μD = μ1 - μ2 : Mean of the difference between Regular seed and Kiln-dried seed H0 : μD = 0 HA : μD > 0 Hypothesis test results: Difference Mean Std. Err. DF T-Stat...
how does culture impact the way infants learn
how does culture impact the way infants learn
In a physics lab students are conducting an experiment to learn about the heat capacity of...
In a physics lab students are conducting an experiment to learn about the heat capacity of different materials. The first group is instructed to add 1.5-g copper pellets at a temperature of 92°C to 345 g of water at 16°C. A second group is given the same number of 1.5-g pellets as the first group, but these are now aluminum pellets. Assume that no heat is lost to or gained from the surroundings for either group. (a) If the final...
Goal: in this lab, you will learn to configure sudo to allow a user mrussell to...
Goal: in this lab, you will learn to configure sudo to allow a user mrussell to change password for users. Please follow the steps and answer all the questions at the end of the lab instruction. In the Linux machine ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. For this part you will need a 2nd normal user (non-root) account. If you don't have an account for "auser" with password “room1202” yet, you can create one by running (as room1202): $ sudo useradd -c "A User"...
The Problem Statement In this lab section, you are going to learn how to sort an...
The Problem Statement In this lab section, you are going to learn how to sort an array of integers, and to sort an array of objects. We are going to divide the work into two parts. Part 1. Sorting an array of integers using selection sort In this part, you are given the code (see List 1) for sorting an array of integers into ascending order. The sorting method used is the selection sort. You can cut-and-paste the code into...
what is the correct way of doing a anatomy lab report?
what is the correct way of doing a anatomy lab report?
Recall that an array could be a convenient way of storing the elements of a Heap....
Recall that an array could be a convenient way of storing the elements of a Heap. Give a Pseudocode that determines whether an array H[1..N] is indeed a Heap, i.e., it’s elements satisfy the Heap property.
In this lab, you will practice the use of array by building an simple maze game,...
In this lab, you will practice the use of array by building an simple maze game, where your task is to control the prince to defeat the monster and save Snow White. You have to work based on the given skeleton code. Game Description The Snow White is detained in the maze by the monster, who needs the prince's help to rescure her out from the exit. The pre-set maze is shown below, with width 11 and height 8. The...
In this lab you will learn how to use methods from the Math class in order...
In this lab you will learn how to use methods from the Math class in order to calculate the area or the volume of several different shapes. If you are confused about the Methods you can access from the Math class and would like to see some examples click here. Hint: Most of these methods can be done in one line. Step 1 - circleArea In this method you will calculate the area of a circle. Before you can calculate...
This all has to be in javascript A common way to remove duplicates from an array...
This all has to be in javascript A common way to remove duplicates from an array is to convert it to a set and then convert it back to an array. We will do that here. Please use these variables and data structures: var input = [3, 4, 2, 2, 4, 5, 3, 1, 3, 6]; var set = new Set(); 1. Get each element from the input array and add it to the set. 2. Get the elements from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT