Select one item from a list according to preference

I am assigned an unordered list of objects of this form

class Element {
  String property; // has value from the set ("A", "B", "C", "D")
}

where two elements do not have the same value property.
I want to select one element according to a set of rules, such as the following, and return its value property:

  • If there is an item that propertyhas a value "A", select that item.
  • Otherwise, if there is an item that propertyhas a value "B", then select that item.
  • Otherwise, if there is an item that propertyhas a value "C", then select that item.
  • Finally, if no items have been selected, throw an exception.

This is one of the solutions I came up with for this problem:

String chooseElementProperty(List<Element> elements) {
  // Iterates the list and creates a set of all the property values.
  // example: [{property: 'A'}, {property: 'B'}] -> {'A', 'B'}
  Set<String> propertyValues = pluckPropertyValues(elements);

  if (propertyValues.contains("A"))
    return "A";
  if (propertyValues.contains("B"))
    return "B";
  if (propertyValues.contains("C"))
    return "C";
  throw new IllegalArgumentException("Invalid input!");
}

: / , , , "B" over "A"?

+4
5

, , chooseElementProperty, :

String chooseElementProperty(List<Element> elements, List<String> prefs) {
  // Iterates the list and creates a set of all the property values.
  // example: [{property: 'A'}, {property: 'B'}] -> {'A', 'B'}
  Set<String> propertyValues = pluckPropertyValues(elements);

  for (String s: prefs) {
      if (propertyValues.contains(s))
          return s;
  }
  throw new IllegalArgumentException("Invalid input!");
}

List<String>, varargs String[] String..., .

+4

:

List<String> targets = Arrays.asList("A", "B", "C");
return elements.stream()
  .map(this::pluckPropertyValues)
  .flatMap(Set::stream)
  .filter(targets::contains)
  .sorted(Comparator.comparing(targets::indexOf))
  .findFirst()
  .orElseThrow(IllegalArgumentException::new);

, .

: , ( , )

+3

, , comparator propery,

Collections.sort(elements, new Comparator<Element>() {
    @Override
    public int compare(Element a, Element b) {
        return a.property.compareTo(b.property);
    }
});
List<String> check = Arrays.asList("A", "B", "C");
String tmp = elements.get(0).property;
if check.contains(tmp) 
    return tmp
else
    throw new IllegalArgumentException("Invalid input!");

:

Collections.sort(elements, (e1, e1) -> e1.property.compareTo(e2.property));
+3

, , , "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) 
    {
        // Change this to change the preferences
        // (or pass it in as a parameter)
        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) 
{
    // Change this to change the preferences
    // (or pass it in as a parameter)
    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!");
}
+2

.

contains(), indexOf()

if (propertyValues.indexOf("A") > -1)
   return "A";
if (propertyValues.indexOf("B") > -1)
   return "B";
if (propertyValues.indexOf("C") > -1)
   return "C";
+1

Source: https://habr.com/ru/post/1679535/


All Articles