我如何对一个Arraylist进行排序,使其集合按一个值列表排序

问题描述 投票:1回答:1

我有一个 阵列列表 其中包含了菜谱,而每个菜谱都包含了一个配料列表。食谱显示在Android应用的UI列表中,用户希望根据他们最喜欢的食材对列表进行排序。

例如这是他们最喜欢的食材列表。

1 - Cheese
2 - Potato
3 - Tuna
4 - Chicken

因此,食谱列表应首先显示所有的成分,包含奶酪,然后土豆等。java stream()?

以下是我的模型类目前的情况。

public class Recipe {

    String[] ingredients;
    private String description;
    private String title;


    public String[] getIngredients() {
        return ingredients;
    }

    public void setIngredients(String[] ingredients) {
        this.ingredients = ingredients;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

.

public class Ingredient {

    String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
java
1个回答
1
投票

你的Recipe类没有使用你的Recipe类,所以你的模型可以修改为使用Ingredient类。

public class Recipe {
    List<Ingredient> ingredients;
    ...
}

你可以创建你自己的比较器来排序,如果一个Recipe有一些成分。

public class ContainsIngredientComparator implements Comparator<Recipe> {

    private Ingredient ingredient;

    public ContainsIngredientComparator(Ingredient ingredient) {
        this.ingredient = ingredient;
    }   

    @Override
    public int compare(Recipe r1, Recipe r2) {
        if (r1.getIngredients().contains(ingredient)) {
            if (r2.getIngredients().contains(ingredient)) {
                return 0;
            } else {
                return -1;
            }
        } else if (r2.getIngredients().contains(ingredient)) {
            return 1;
        } else {
            return 0;
        }
    }
}

现在,你可以为每个原料创建一个比较器,并将所有的比较器链起来。你可以使用 Stream API 将最喜欢的配料映射到比较器上,并使用 thenComparing 来连锁比较器。

Optional<ContainsIngredientComparator> favoritesComparator = favorites.stream()
        .map(ContainsIngredientComparator::new)
        .reduce((c1, c2) -> (ContainsIngredientComparator) c1.thenComparing(c2));

你可以使用结果比较器来对列表进行排序。

List<Recipe> recipeList = // ... get the recipeList
if (favoritesComparator.isPresent()) {
    recipeList.sort(favoritesComparator.get());
}

1
投票

你可以创建比较器Potato和比较器Cheese,然后根据它们进行排序。

  static class Recipie {
        List<String> list;

        public Recipie(List<String> list) {
            this.list = list;
        }

        @Override
        public String toString() {
            return list.toString();
        }
    }

    public static void main(String[] args) {
               List<Recipie> recipies = new ArrayList<>();
        recipies.add(new Recipie(Arrays.asList("Cheese", "Potato", "Onions")));
        recipies.add(new Recipie(Arrays.asList("Tuna", "Potato", "Chicken")));
        recipies.add(new Recipie(Arrays.asList("Potato", "Cheese")));
        recipies.add(new Recipie(Arrays.asList("Chicken")));
        recipies.add(new Recipie(Arrays.asList("Chicken", "Potato")));
        recipies.add(new Recipie(Arrays.asList("Cheese", "Tomato")));

        List<Recipie> result = recipies.stream().sorted(Comparator.comparing(r -> !r.list.contains("Potato")))
                .sorted(Comparator.comparing(r -> !r.list.contains("Cheese"))).collect(Collectors.toList());

        System.out.println(result);
    }

产量

[[Cheese, Potato, Onions], [Potato, Cheese], [Cheese, Tomato], [Tuna, Potato, Chicken], [Chicken, Potato], [Chicken]]
© www.soinside.com 2019 - 2024. All rights reserved.