Java:
import java.util.HashSet;
public class KeyWords {
private HashSet<String> hashSet;
// Constructor to initialize the hashSet
public KeyWords() {
this.hashSet = new HashSet<>();
}
// Method to add keywords to the hashSet
public void addKeyword(String keyword) {
hashSet.add(keyword);
}
// Extractor method
public String Extractor(String str1) {
for (String keyword : hashSet) {
if (str1.contains(keyword)) {
return keyword; // Return the first matching keyword
}
}
return ""; // Return empty string if no keyword matches
}
// Main method for testing
public static void main(String[] args) {
KeyWords keyWords = new KeyWords();
keyWords.addKeyword("example");
keyWords.addKeyword("test");
keyWords.addKeyword("java");
String result = keyWords.Extractor("This is a test string for Java programming.");
System.out.println(result); // Output: test
}
}