39.5k views
0 votes
Write a public static method called getPIDs that has a parameter of type Map called nameToPID (keys are student names and values are student PIDs). It should return a HashSet containing all PIDs in nameToPID.

User Hidroto
by
5.0k points

1 Answer

4 votes

Answer:

Java program is explained below

Step-by-step explanation:

import java.util.HashSet;

import java.util.Map;

import java.util.TreeMap;

public class TruckRace1 {

public static HashSet<String> getPIDs (Map<String,String>nameToPID ){

HashSet<String> pidSet = new HashSet<>();

for(String ids: nameToPID.values()){

pidSet.add(ids);

}

return pidSet;

}

public static void main(String[] args) {

Map<String,String> pids = new TreeMap<>();

pids.put("John","123");

pids.put("Jason","124");

pids.put("Peter","125");

pids.put("Alice","126");

pids.put("Peny","129");

HashSet<String> ids = getPIDs(pids);

for(String id:ids){

System.out.println(id);

}

}

}

User Mlibre
by
4.8k points