What I am using: I have multiple arrays
String[] cat = {"schema cat","schemacat","schemac"}
String[] dog = {"schema dog","schemadog","schemad"}
String[] horse = {"schema horse","schemachorse","schemah"}
I am getting a value to my method for example schema cat and I need to find in which array it is written. But I can sometimes get schemac or cat instead of schema cat.
Problem: So I need to find in which array is containing value.
What I have tried: I have tried writing multiple if cases but it's getting to messy and complicated. I am wondering what is the best solution for this case. I was thinking about switch, but don't know how to solve case situations.
3 Answers 3
You could simple create a method, where you give the value, the different arrays to look into, and iterate over them until you found the value
Edit : to get like the name of the array, you can add it as the first value of the array and use it as return value
static String whichArrayContains(String value, String[]... arrays) {
for (String[] array : arrays) {
if (Arrays.asList(array).contains(value)) {
return array[0];
}
}
return null;
}
And use like
public static void main(String[] args) {
String[] cat = {"car", "schema cat", "schemacat", "schemac"};
String[] dog = {"dog", "schema dog", "schemadog", "schemad"};
String[] horse = {"horse", "schema horse", "schemachorse", "schemah"};
String value = "schema cat";
System.out.println(Arrays.toString(whichArrayContains(value, cat, dog, horse))); // cat
System.out.println(Arrays.toString(whichArrayContains(value, dog, horse))); //null
}
4 Comments
The simple solution would be to use lists, and a "reverse" maps, like:
Map<String, List<String>> listsByMembers = ...
The idea would be: instead of using arrays, you use Lists (just because collections work better with collections, instead of arrays). And then, for each member of each List, you do listsByMembers.put(listEntry, theList).
Then you can do a simple lookup listsByMembers.get("cat") for example.
If we are talking about a large scale solution for real world problems, that would boil down to re-invent the wheel. For real-world scenarios, you should rather look into tools like Elastic, Solr, ...
Comments
By using java8 streams concept you can solve your problem as shown below.
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
String[] cat = {"schema cat","schemacat","schemac"};
String[] dog = {"schema dog","schemadog","schemad"};
String[] horse = {"schema horse","schemachorse","schemah"};
String testStr = "schemac";
System.out.println(isStringExist(testStr, cat));
System.out.println(isStringExist(testStr, dog));
System.out.println(isStringExist(testStr, horse));
}
public static boolean isStringExist(String testStr, String[] items) {
return Arrays.stream(items).parallel().anyMatch(testStr :: contains);
}
}
In the above code isStringExist() method accepts the String array and test string that need to be compared with each String present inside the String array items and it returns a Boolean value which confirms that the string present in the String array that is passed to the method.
schema catexists in any array or not.String.containsis what you need.