Skip to main content
added 4 characters in body
Source Link
Anonymous Coward
  • 3.2k
  • 1
  • 25
  • 39
/** Will return a set of words which are in the array and the sentence. */
public Set<String> checkWords( String sentence, String[] words )
{
  Set<String> result = new HashSet<>()
  StringString[] wordsInSentence = sentence.split(" ");
  for( String word : wordsInSentence )
  {
    for( String word2 : words )
    {
      if ( word.equals(word2) )
      {
        result.add( word );
        break;
      }
    }
  }
}

It would be better if you used a Set instead of an array though. It better conveys the meaning and will be more efficient with a HashSet.

/** Will return a set of words which are in the set and the sentence. */
public Set<String> checkWords( String sentence, Set<String> words )
{
  Set<String> result = new HashSet<>()
  StringString[] wordsInSentence = sentence.split(" ");
  for( String word : wordsInSentence )
  {
    if ( words.contains(word) )
      result.add( word );
  }
}
/** Will return a set of words which are in the array and the sentence. */
public Set<String> checkWords( String sentence, String[] words )
{
  Set<String> result = new HashSet<>()
  String wordsInSentence = sentence.split(" ");
  for( String word : wordsInSentence )
  {
    for( String word2 : words )
    {
      if ( word.equals(word2) )
      {
        result.add( word );
        break;
      }
    }
  }
}

It would be better if you used a Set instead of an array though. It better conveys the meaning and will be more efficient with a HashSet.

/** Will return a set of words which are in the set and the sentence. */
public Set<String> checkWords( String sentence, Set<String> words )
{
  Set<String> result = new HashSet<>()
  String wordsInSentence = sentence.split(" ");
  for( String word : wordsInSentence )
  {
    if ( words.contains(word) )
      result.add( word );
  }
}
/** Will return a set of words which are in the array and the sentence. */
public Set<String> checkWords( String sentence, String[] words )
{
  Set<String> result = new HashSet<>()
  String[] wordsInSentence = sentence.split(" ");
  for( String word : wordsInSentence )
  {
    for( String word2 : words )
    {
      if ( word.equals(word2) )
      {
        result.add( word );
        break;
      }
    }
  }
}

It would be better if you used a Set instead of an array though. It better conveys the meaning and will be more efficient with a HashSet.

/** Will return a set of words which are in the set and the sentence. */
public Set<String> checkWords( String sentence, Set<String> words )
{
  Set<String> result = new HashSet<>()
  String[] wordsInSentence = sentence.split(" ");
  for( String word : wordsInSentence )
  {
    if ( words.contains(word) )
      result.add( word );
  }
}
Source Link
Anonymous Coward
  • 3.2k
  • 1
  • 25
  • 39

/** Will return a set of words which are in the array and the sentence. */
public Set<String> checkWords( String sentence, String[] words )
{
  Set<String> result = new HashSet<>()
  String wordsInSentence = sentence.split(" ");
  for( String word : wordsInSentence )
  {
    for( String word2 : words )
    {
      if ( word.equals(word2) )
      {
        result.add( word );
        break;
      }
    }
  }
}

It would be better if you used a Set instead of an array though. It better conveys the meaning and will be more efficient with a HashSet.

/** Will return a set of words which are in the set and the sentence. */
public Set<String> checkWords( String sentence, Set<String> words )
{
  Set<String> result = new HashSet<>()
  String wordsInSentence = sentence.split(" ");
  for( String word : wordsInSentence )
  {
    if ( words.contains(word) )
      result.add( word );
  }
}