在多层嵌套Map中查找值匹配的路径

package com.demos.practiceone.util;

import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;

public class NestedMapFinder {

    private final LinkedList orderChain = new LinkedList<>();

    private boolean doesFindIt = false;

    public boolean isFound() {
        return this.doesFindIt;
    }

    public  LinkedList find(Map map, T target) {
        for (Map.Entry e : map.entrySet()) {
            Object k = e.getKey();
            Object v = e.getValue();
            orderChain.add(k);
            if ((target == null && v == null) || Objects.equals(target, v)) {
                this.doesFindIt = true;
                break;
            } else {
                if (v instanceof Map) {
                    this.find((Map) v, target);
                } else {
                    orderChain.removeLast();
                }
            }
        }
        if (!this.doesFindIt && orderChain.size() != 0) {
            orderChain.removeLast();
        }
        return orderChain;
    }
}







你可能感兴趣的:(在多层嵌套Map中查找值匹配的路径)