java - 搜索对象数组列表的特定属性

标签 java

我有一个对象数组列表 ArrayList<Tile> list Tile 的属性为 new Tile("colour", value)我想创建一个搜索功能,在其中遍历每个 Colour Tile 的属性和每个 Value arraylist 中每种颜色的属性(就像每个循环的嵌套),是否有一种简单的方法可以做到这一点?

最佳答案

假设 Tile 类有两个属性 String colourint value。它有一个 toString(java.lang.Object 类 的覆盖方法),如下所示:

@Override public String toString() {
    return colour + ":" + value;
}

制作一些瓷砖:

Tile t1 = new Tile("red", 7); // constructor takes a colour and a value
Tile t2 = new Tile("red", 2);
Tile t3 = new Tile("blue", 9);
Tile t4 = new Tile("white", 17);
Tile t5 = new Tile("blue", 3);
Tile t6 = new Tile("red", 15);
Tile t7 = new Tile("white", 10);


场景 1:

该函数将 Tile 对象列表和 String 颜色作为输入,并返回具有输入颜色(及其值)的所有图 block 。有两种方法可以做到这一点,这些方法以两种方法显示:

private static List<Tile> getTilesWithColor1(List<Tile> tilesList, String searchColor) {
    return tilesList.stream()
                     .filter(tile -> tile.getColour().equals(searchColor))
                     .collect(Collectors.toList());
}

private static List<Tile> getTilesWithColor2(List<Tile> tilesList, String searchColor) {
    List<Tile> result = new ArrayList<>();
    for (Tile t : tilesList) {
        if (t.getColour().equals(searchColor)) {
            result.add(t);
        }
    }
    return result;
}
  • 输入:tilesListcolour="red"
  • 输出(两种方法相同):[red:7, red:2, red:15]

I want to make a search function where I iterate though each Colour attribute of the Tile and each Value attribute within each colour in the arraylist (like a nested for each loop), is there an easy way of doing this?

可以更改此函数以添加其他条件或过滤器以获得所需的结果。


场景 2:

获取所有颜色及其值:

private static Map<String, List<Integer>> getTileColorsAndValues(List<Tile> tilesList) {
    return tilesList.stream()
                     .collect(Collectors.groupingBy(Tile::getColour,
                         Collectors.mapping(Tile::getValue, Collectors.toList())));
}
  • 输入:tilesList
  • 输出:{red=[7, 2, 15], white=[17, 10], blue=[9, 3]}

请注意,可以从生成的 map 中像这样获取“红色”图 block 内的值:

List<Integer> valuesList = map.get("red");


场景 3:

按颜色获取所有图 block :

private static Map<String, List<Tile>> getTilesByColorsAndValues(List<Tile> tilesList) {
    return tilesList.stream()
                    .collect(Collectors.groupingBy(Tile::getColour));
}
  • 输入:tilesList
  • 输出:{red=[red:7, red:2, red:15], white=[white:17, white:10], blue=[blue:9, blue:3]}

请注意,可以从生成的 map 中像这样获取“红色”瓦片内的瓦片:

List<Tile> tilesList = map.get("red");



示例代码:

import java.util.*;
import java.util.stream.*;
import java.util.function.*;

public class TilesExample {

    public static void main(String [] args) {

        Tile t1 = new Tile("red", 7);
        Tile t2 = new Tile("red", 2);
        Tile t3 = new Tile("blue", 9);
        Tile t4 = new Tile("white", 17);
        Tile t5 = new Tile("blue", 3);
        Tile t6 = new Tile("red", 15);
        Tile t7 = new Tile("white", 10);
        List<Tile> tilesList = Arrays.asList(t1, t2, t3, t4, t5, t6, t7);

        System.out.println(getTilesWithColor1(tilesList, "red"));
        System.out.println(getTilesWithColor2(tilesList, "red"));

        System.out.println(getTileColorsAndValues(tilesList));

        System.out.println(getTilesByColorsAndValues(tilesList));
    }

    private static Map<String, List<Tile>> getTilesByColorsAndValues(List<Tile> tilesList) {
        return tilesList.stream()
                        .collect(Collectors.groupingBy(Tile::getColour));
    }

    private static Map<String, List<Integer>> getTileColorsAndValues(List<Tile> tilesList) {
        return tilesList.stream()
                        .collect(Collectors.groupingBy(Tile::getColour,
                            Collectors.mapping(Tile::getValue, Collectors.toList())));
    }

    private static List<Tile> getTilesWithColor1(List<Tile> tilesList, String searchColor) {
        return tilesList.stream()
                         .filter(tile -> tile.getColour().equals(searchColor))
                         .collect(Collectors.toList());
    }

    private static List<Tile> getTilesWithColor2(List<Tile> tilesList, String searchColor) {
        List<Tile> result = new ArrayList<>();
        for (Tile t : tilesList) {
            if (t.getColour().equals(searchColor)) {
                result.add(t);
            }
        }
        return result;
    }
}

关于java - 搜索对象数组列表的特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52660914/

相关文章:

java - Android 中的下拉菜单(Xamarin C# 或 Java)

java - p :dataTable - refresh datatable after delete row

java - 如何修改不同窗体上的控件?

java - 使用 Java 中的属性文件中的值更新数据库

java - 如何使用 Spring Security 注入(inject)默认安全 header

java - Java中的选择排序算法

java - DBUnit 有没有办法自动创建表?

java - 配置 log4j org.apache.log4j.rolling.RollingFileAppender,以便在日志数量超过设置的索引量后删除压缩日志

java - 带有外部参数的 PriorityQueue

java - 如何将 double 据转换为字节数组,以及如何检查其输出数据是否已正确转换