我们如何使用循环 HashSet<HashMap<String, String>> 在内部搜索 HashMap<String, String> 对象?

How can we search HashMap<String, String> object inside with out loop HashSet<HashMap<String, String>>?

    HashSet<HashMap<String, String>> Assets = new HashSet<>();
    HashMap<String, String> Items=new HashMap<>();
    Items.put("AssetId","1");
    Items.put("AssetName","Table");
    Items.put("AssetType","1");
    Assets.add(Items);
    Items=new HashMap<>();
    Items.put("AssetId","2");
    Items.put("AssetName","Chair");
    Items.put("AssetType","1");
    Assets.add(Items);
    Items=new HashMap<>();
    Items.put("AssetId","3");
    Items.put("AssetName","Computer");
    Items.put("AssetType","2");
    Assets.add(Items);
    Items=new HashMap<>();
    Items.put("AssetId","4");
    Items.put("AssetName","Printer");
    Items.put("AssetType","2");
    Assets.add(Items);

    HashSet<HashMap<String, String>> AssetsFiltered= Assets.filter("AssetId","1,3");

我需要这样的东西:

或任何其他进行此类过滤的简单方法。我准备改变这个 HashSet 概念。

请注意,我不希望代码包含任何类型的循环,例如 for、while、do while 等

您尝试过使用 Stream.filter 吗?像下面这样的东西应该可以解决问题:

Set<HashMap<String, String>> AssetsFiltered = Assets.stream()
      .filter(asset -> 
              List.of("1","3").contains(asset.get("AssetId")))
      .collect(Collectors.toSet());

这也将是 NPE 安全的,以防您没有任何资产具有您要过滤的 ID。

如果您希望它更通用并且能够动态传递键和值,您可以使用如下内容:

public Set<Map<String, String>> filter(Set<Map<String, String>> assets, String key, List<String> values){
    return assets.stream()
      .filter(asset -> values.contains(asset.get(key)))
      .collect(Collectors.toSet());
}

如果您需要像您给出的示例中那样将允许的值作为单个字符串传递,您可以随时对其调用 .split(",")

我尝试使用 kotlin 代码, 首先我们需要将 HashSet 更改为 Sequence 并使用您想要的值按键过滤。

代码如下:

    val Assets: HashSet<HashMap<String?, String?>> = HashSet()
    var Items: HashMap<String?, String?> = HashMap()
    Items["AssetId"] = "1"
    Items["AssetName"] = "Table"
    Items["AssetType"] = "1"
    Assets.add(Items)
    Items = HashMap()
    Items["AssetId"] = "2"
    Items["AssetName"] = "Chair"
    Items["AssetType"] = "1"
    Assets.add(Items)
    Items = HashMap()
    Items["AssetId"] = "3"
    Items["AssetName"] = "Computer"
    Items["AssetType"] = "2"
    Assets.add(Items)
    Items = HashMap()
    Items["AssetId"] = "4"
    Items["AssetName"] = "Printer"
    Items["AssetType"] = "2"
    Assets.add(Items)


    val filter = Assets.asSequence().filter { pred -> pred["AssetId"] == "2" }
    val AssetsFiltered: HashSet<HashMap<String?, String?>> = filter.toHashSet()

    val filter2 = Assets.asSequence().filter { pred -> pred["AssetId"] == "1" || pred["AssetId"] == "2" }
    val AssetsFiltered2: HashSet<HashMap<String?, String?>> = filter2.toHashSet()

    val filter3 = Assets.asSequence().filter { pred -> pred["AssetId"]!!.toInt() >= "1".toInt() && pred["AssetId"]!!.toInt() <= "3".toInt() }
    val AssetsFiltered3: HashSet<HashMap<String?, String?>> = filter3.toHashSet()

    Log.e("Assets Test", "Test $AssetsFiltered")
    Log.e("Assets Test", "Test2 $AssetsFiltered2")
    Log.e("Assets Test", "Test2 $AssetsFiltered3")

我不知道这是否是个好主意,但我解决了以下对我有用的 code.its。

 fun filteredAssetList(key: String, value: String, IsIn: Boolean): HashSet<HashMap<String?, String?>> {

        try {

            val filter2 = Assets.asSequence().filter { pred -> getFilterCase(pred, key, value, IsIn) }
            val AssetsFiltered2: HashSet<HashMap<String?, String?>> = filter2.toHashSet()
            return AssetsFiltered2;

        } catch (e: Exception) {
            val Assets2: HashSet<HashMap<String?, String?>> = HashSet();
            return Assets2;
        }
    }

    fun getFilterCase(Item: HashMap<String?, String?>, key: String, value: String, IsIn: Boolean): Boolean {
        val lstValues: List<String> = value.split(",").map { it -> it.trim() }
        var IsFound: Boolean = false
        if (IsIn) {
            lstValues.forEach { str -> IsFound = (IsFound || Item[key].equals(str)) }
        } else {
            IsFound = true
            lstValues.forEach { str ->
                if (Item[key].equals(str)) {
                    IsFound = false
                    return@forEach
                }
            }
        }
        return IsFound
    }

参数IsIn用于包含或不包含的情况 如果 IsIntrue 它将 return 过滤值中的所有资产列表 和 如果 IsInfalse 它将 return 所有不在过滤器值中的资产列表。

但我正在使用 forEach here.LOL..