Java 可以使用一些注释详细说明答案的上半部分(见上文StringBuilder)。我会很快通知您它是否有效。e.getValue().forEach(s->sb.append(s).append(“,”)需要android中的API>24。但目前的最低标准是16

Java 可以使用一些注释详细说明答案的上半部分(见上文StringBuilder)。我会很快通知您它是否有效。e.getValue().forEach(s->sb.append(s).append(“,”)需要android中的API>24。但目前的最低标准是16,java,android,string,Java,Android,String,可以使用一些注释详细说明答案的上半部分(见上文StringBuilder)。我会很快通知您它是否有效。e.getValue().forEach(s->sb.append(s).append(“,”)需要android中的API>24。但目前的最低标准是16。你对此有什么建议吗?让我知道您的想法。您可以使用字符串串联:string output=“”;输出+=e.getKey()+',';输出+=s+','分组抛出错误,除此之外一切正常(我通过传递硬编码的mapstates={“s1”:[“d1”


可以使用一些注释详细说明答案的上半部分(见上文
StringBuilder
)。我会很快通知您它是否有效。
e.getValue().forEach(s->sb.append(s).append(“,”)
需要android中的API>24。但目前的最低标准是16。你对此有什么建议吗?让我知道您的想法。您可以使用字符串串联:
string output=“”;输出+=e.getKey()+',';输出+=s+','分组抛出错误,除此之外一切正常(我通过传递硬编码的map
states={“s1”:[“d1”、“d2”、“d3”],“s2”:[“d4”]}
进行检查)。
private String getSimplfiedDistrictsString(String districts){
    String finalString = "";
    if(districts.equals(allDistricts)){ // 'allDistricts' is a string which contains all Indian districts name ( comma separated )
        finalString = "Every district of India";
    }
    //Looking for function which simplify states name also
    //For example if district contain all district of Rajasthan state then in 'finalString' name of all districts should be replaced with only 'Rajasthan'
    //But if 'districts' variable contains some districts of any state but not all then they should remain as it is
    return finalString;
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Set<String> districtSetRajasthan = new HashSet<>();
        districtSetRajasthan.add("Dist1");
        districtSetRajasthan.add("Dist2");
        districtSetRajasthan.add("Dist3");

        Set<String> districtSetGujrat = new HashSet<>();
        districtSetGujrat.add("Dist4");
        districtSetGujrat.add("Dist5");

        Map<String, Set<String>> mapStates = new LinkedHashMap<String, Set<String>>();
        mapStates.put("Rajasthan", districtSetRajasthan);
        mapStates.put("Gujrat", districtSetGujrat);

        // Tests
        String textViewString = "Dist1,Dist2,Dist3,Dist4";
        System.out.println(getStateDistString(textViewString, mapStates));

        textViewString = "Dist1,Dist2,Dist3,Dist4,Dist5";
        System.out.println(getStateDistString(textViewString, mapStates));
    }

    static String getStateDistString(String districts, Map<String, Set<String>> mapStates) {
        // Split districts on comma and put the elements of the resultListing array into
        // a HashSet
        Set<String> districtSetTextView = Arrays.stream(districts.split(","))
                                                .collect(Collectors.toSet());

        List<String> resultList = new ArrayList<>();

        for (String state : mapStates.keySet()) {
            Set<String> allDistrictsOfState = mapStates.get(state);
            if (districtSetTextView.containsAll(allDistrictsOfState)) {
                resultList.add(state);
                districtSetTextView.removeAll(allDistrictsOfState);
            }
        }

        // Add the remaining values of districtSetTextView to resultList
        resultList.addAll(districtSetTextView);

        return String.join(",", resultList);
    }
}
Rajasthan,Dist4
Rajasthan,Gujrat
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Set<String> districtSetRajasthan = new HashSet<>();
        districtSetRajasthan.add("Dist1");
        districtSetRajasthan.add("Dist2");
        districtSetRajasthan.add("Dist3");

        String textViewString = "Dist1,Dist2,Dist3";

        // Split the string of the TextView on comma and put the elements of the
        // resulting array into a HashSet
        Set<String> districtSetTextView = Arrays.stream(textViewString.split(","))
                                                .collect(Collectors.toSet());

        // Check if districtSetTextView contains all districts of Rajasthan
        if (districtSetTextView.containsAll(districtSetRajasthan)) {
            System.out.println("Rajasthan");
        } else {
            System.out.println(textViewString);
        }
    }
}
Rajasthan
Map<String, List<String>> allStates;  

String getSimplifiedDistrictsString(String districtsString) {
    // in this part you are grouping districts by their states
    // example:
    // allStates = {"s1": ["d1", "d2", "d3"], "s2": ["d4", "d5", "d6"]}
    // districtsString = "d1,d2,d3,d4"
    // states = {"s1": ["d1", "d2", "d3"], "s2": ["d4"]}
    Map<String, List<String>> states = Stream.of(districtsString.split(","))
            .collect(Collectors.groupingBy(d -> {
                // d - district name
                for (var e : allStates.entrySet()) {
                    // e.key() - state name
                    // e.value() - list of districts 
                    // if the state contains specific district then group by that state
                    if (e.getValue().contains(d)) {
                        return e.getKey();
                    }
                }
                throw new RuntimeException("State of district not found: " + d);
            }));
    StringBuilder sb = new StringBuilder();
    for (var e : states.entrySet()) {
        if (e.getValue().containsAll(allStates.get(e.getKey()))) {
            sb.append(e.getKey()).append(","); // append name of a state
        } else {
            e.getValue().forEach(s -> sb.append(s).append(",")); // append names of all districts
        }
    }
    return sb.toString();
}