So I have:
所以我有:
private static ArrayList<AbstractAnalyser> analysers = new ArrayList<>();
public static String getAnalyser(String analyser){
if(analysers.contains(analyser)){
return "The full name of the analyser";
}
return null;
}
So what I want is..
所以我想要的是......
If the the arraylist contains the parameter analyser, I want it to return the full name of the object what is in the arraylist.
如果arraylist包含参数分析器,我希望它返回arraylist中对象的全名。
Let's say these values are in the arraylist:
假设这些值在arraylist中:
analyser, method and second. <- random names
分析仪,方法和第二。 < - 随机名称
If the parameter is analyser and the arraylist contains analyser. The method needs the return that name.
如果参数是分析器,则arraylist包含分析器。该方法需要返回该名称。
Even when the parameter is "analy".
即使参数是“分析”。
1 个解决方案
#1
2
I would use a Map instead of a List:
我会使用Map而不是List:
private static Map<String, AbstractAnalyser> analysers = new HashMap<>();
public static AbstractAnalyser getAnalyser(String analyserName){
AbstractAnalyser result = null;
if ((analyserName != null) && (analyserName.trim().length() > 0)) {
if (analysers.containsKey(analyserName)) {
result = analysers.get(analyserName);
} else {
for (String key : analysers.keySet()) {
// put the logic to find the one you want here.
}
}
}
return result;
}
But, if you must, you can do it this way if the AbstractAnalyser has a way to give you its name:
但是,如果必须,如果AbstractAnalyser能够为您提供名称,则可以这样做:
private static List<AbstractAnalyser> analysers = new ArrayList<>();
public static AbstractAnalyser getAnalyser(String analyserName){
AbstractAnalyser result = null;
if ((analyserName != null) && (analyserName.trim().length() > 0)) {
for (AbstractAnalyser analyser : analysers) {
// Here's how you look by name
if (analyser.getName().equals(analyserName)) {
result = analyser;
break;
} else {
// put special logic to find the one you want here.
}
}
}
return result;
}
Using the Map is always more efficient when you give the exact name because the lookup is O(1)
. The List lookup is always O(N)
.
当您给出确切的名称时,使用Map总是更有效,因为查找是O(1)。列表查找始终为O(N)。
#1
2
I would use a Map instead of a List:
我会使用Map而不是List:
private static Map<String, AbstractAnalyser> analysers = new HashMap<>();
public static AbstractAnalyser getAnalyser(String analyserName){
AbstractAnalyser result = null;
if ((analyserName != null) && (analyserName.trim().length() > 0)) {
if (analysers.containsKey(analyserName)) {
result = analysers.get(analyserName);
} else {
for (String key : analysers.keySet()) {
// put the logic to find the one you want here.
}
}
}
return result;
}
But, if you must, you can do it this way if the AbstractAnalyser has a way to give you its name:
但是,如果必须,如果AbstractAnalyser能够为您提供名称,则可以这样做:
private static List<AbstractAnalyser> analysers = new ArrayList<>();
public static AbstractAnalyser getAnalyser(String analyserName){
AbstractAnalyser result = null;
if ((analyserName != null) && (analyserName.trim().length() > 0)) {
for (AbstractAnalyser analyser : analysers) {
// Here's how you look by name
if (analyser.getName().equals(analyserName)) {
result = analyser;
break;
} else {
// put special logic to find the one you want here.
}
}
}
return result;
}
Using the Map is always more efficient when you give the exact name because the lookup is O(1)
. The List lookup is always O(N)
.
当您给出确切的名称时,使用Map总是更有效,因为查找是O(1)。列表查找始终为O(N)。