How simplify string of Indian districts in Java? ( Problem Extended )

Viewed 39

I have a string of India districts. For example String districts = district1,district2,district3,...so on (maybe all or some of districts of India).
Assume,
district1,district2,district3 are Rajasthan's district
And
district4,district5,district6 are Gujrat's districts

What is scenario
I want to show districts variable in TextView but if this string contains all districts of any state(s) then only state name should be shown in place of all districts of it. It will reduce the size of string and will look much better.
For detailed explanation refer to base question


Achieved Case 1
Input = district1,district2,district3,district4
Output = Rajasthan,district4

Achieved Case 2
Input = district1,district2,district3,district4,district5
Output = Rajasthan,district4,district5

Achieved Case 2
Input = district1,district2,district3,district4,district5,district6
Output = Rajasthan,Gujrat

Above cases achieved by @Live and Let Live answer


Expected Case 1
Input = district1,district2,district3,district4
Output = Rajasthan, Gujrat (district4)

Expected Case 2
Input = district1,district2,district3,district4,district5
Output = Rajasthan,Gujrat (district4,district5)

Expected Case 3
Input = district1,district2,district3,district4,district5,district6
Output = Rajasthan,Gujrat

1 Answers

Extending on my earlier answer, given below is how you can achieve this requirement:

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);
            } else {
                List<String> temp = new ArrayList<>();
                for (String dist : districtSetTextView) {
                    if (allDistrictsOfState.contains(dist)) {
                        temp.add(dist);
                    }
                }
                if (temp.size() > 0) {
                    String x = state + "(" + String.join(",", temp) + ")";
                    resultList.add(x);
                    districtSetTextView.removeAll(temp);
                }
            }
        }

        return String.join(",", resultList);
    }
}

Output:

Rajasthan,Gujrat(Dist4)
Rajasthan,Gujrat

What's new in this solution:

  1. Create a temporary List<String> temp to store the districts of a state if the full set of districts is not present in textViewString.
  2. Create a string as state + "(" + strings of temp joined on comma + ")" and add the same to resultList. Like the original solution, remove all districts of temp from districtSetTextView.
Related