Mini project Java Complete Beginner_Hashset

HandsOn_#Miniproject - Java Complete Beginner_Hashset

Java Complete Beginner_Hashset


1. HashSet in java


A football team has played n matches in a tournament. They have played each match with different sets of players (some players may have continuously played in some of the matches). You have to write a program to find the players who have played all the matches and the players who have played in a particular match and not in the other mentioned match. Input Specifications:


The first line consists of an integer n denoting the number of match es.

The second line consists of a s ingle string


containing the squad of players in each match, separated by a#. The players in a particular match are separated by a s ingle space (the players for a single match may contain duplicates as well, you need to remove the duplicates).


The last two lines consist of two integers x


And y, which denotes the match numbers depending on which you have to find the p layers who have played in match y, but not in x.

package com.fresco; import java.util.HashSet; public class Hashset {

public static String getOut(int numberOfMatches, String squads, int squad1, int squad2)


{


String result = "";

String[] matchSet = squads.split("#"); HashSet<String> intersectionSet = new

HashSet<String>(); HashSet<String> unionAllSet = new HashSet<String>(); HashSet<String> inMatch = new HashSet<String>(); HashSet<String> notInMatch = new HashSet<String>(); String[] playersDetails = matchSet[0].split(" ");

for (String playerName : playersDetails)

{


intersectionSet.add(playerName)

; unionAllSet.add(playerName);


}

if(squad1 == 1)


notInMatch.addAll(intersectionSet)

; if(squad2 == 1) inMatch.addAll(intersectionSet);

for (int i = 1; i < matchSet.length; i+

+) {


HashSet<String> set = new HashSet<String>(); String[] players = matchSet[i].split(" "); for (String playerName : players)


{

set.add(playerName);

}

intersectionSet.retainAll(set)

; unionAllSet.addAll(set); if(i == squad1-1)


notInMatch.addAll(set)

; if(i == squad2-1) inMatch.addAll(set);


}

HashSet<String> notInMatchFinal = new HashSet<String>(); notInMatchFinal.addAll(unionAllSet); notInMatchFinal.removeAll(notInMatch);


inMatch.retainAll(notInMatchFinal);


result = String.join(" ", intersectionSet)+", "+String.join(" ", inMatch);


return result;

}


}

0/Post a Comment/Comments

#Advertisement

Top Post Ad