, , , "A", "B", "C", , , , , :
/ , , , "B" "A"?
, ... ""... , . , , , :
import java.util.Arrays;
import java.util.List;
class Element
{
String property;
Element(String property)
{
this.property = property;
}
}
public class PickByPreference
{
public static void main(String[] args)
{
List<Element> elements = Arrays.asList(
new Element("B"),
new Element("C"),
new Element(null),
new Element("A"));
System.out.println(chooseElementProperty(elements));
}
static String chooseElementProperty(List<Element> elements)
{
List<String> preferences = Arrays.asList("A", "B", "C");
for (String preference : preferences)
{
for (Element element : elements)
{
if (preference.equals(element.property))
{
return element.property;
}
}
}
throw new IllegalArgumentException("Invalid input!");
}
}
Edit: Set , .
static String chooseElementProperty(List<Element> elements)
{
List<String> preferences = Arrays.asList("A", "B", "C");
Set<String> propertyValues = pluckPropertyValues(elements);
for (String preference : preferences)
{
if (propertyValues.contains(preference))
{
return element.property;
}
}
throw new IllegalArgumentException("Invalid input!");
}