Home > Enterprise >  TreeMap with List
TreeMap with List

Time:11-27

'''

@Override
    public TreeMap<String, List<String>> getCityWithPincode() {
        List<User> listOfUser = findAllUser();

        TreeMap<String, List<String>> tm = new TreeMap<>();

        for(User user : listOfUser) {
            
            if(tm.isEmpty()) {
                tm.put(user.getCity(), Arrays.asList(user.getPincode()));
            }
            else {
                
                if(tm.containsKey(user.getCity())) {
                    List<String> list = tm.get(user.getCity());
                    
                    list.add(user.getPincode());
                    
                    tm.put(user.getCity(), list);
                }
                else {
                    tm.put(user.getCity(), Arrays.asList(user.getPincode()));
                }
                
            }
            
        }

        return tm;
    }

'''

Im trying collect city with there city pincode and in one city have somany pincode right so i have created treemap for collecting this details and key is a city and value is a list of pincode but this not working...

Error is :

{
  "timestamp": "2022-11-27T06:37:21.712 00:00",
  "status": 500,
  "error": "Internal Server Error",
  "trace": "java.lang.UnsupportedOperationException\r\n\tat java.base/java.util.AbstractList.add(AbstractList.java:153)\r\n\tat java.base/java.util.AbstractList.add(AbstractList.java:111)\r\n\tat com.admin.panel.service.UserServiceImpl.getCityWithPincode(UserServiceImpl.java:182)\r\n\tat com.admin.panel.controller.UserController.getCityWithPincode(UserController.java:167)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)\r\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:568)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:670)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:779)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.base/java.lang.Thread.run(Thread.java:833)\r\n",
  "message": "No message available",
  "path": "/user/citywithpincode/cal"
}

CodePudding user response:

You should use new ArrayList<>() instead of Arrays.asList(). You cannot add elements to the list returned by Arrays.asList().

And you can simplify the code by using Map.computeIfAbsent().

public TreeMap<String, List<String>> getCityWithPincode() {
    List<User> listOfUser = findAllUser();

    TreeMap<String, List<String>> tm = new TreeMap<>();

    for(User user : listOfUser) {
        tm.computeIfAbsent(user.getCity(), k -> new ArrayList<>()).add(user.getPincode());
    }
    return tm;
}

CodePudding user response:

Looking into the stacktrace, the add method on AbstractList throws UnsupportedOperationException. You cannot add to the list returned by Arrays.asList.

Either create an empty list of your choice and add to it, or wrap the Arrays.asList call with a mutable list say an ArrayList

tm.put(user.getCity(), new ArrayList<>(Arrays.asList(user.getPincode())));

CodePudding user response:

Solution based on Stream API:

  • stream the list of users
  • Use Collectors.groupingBy() with Function<User, String> as the key extractor, Supplier<Map> as the map factory and Collectors.mapping as the downstream collector:
    • get the user's city for a key User::getCity
    • TreeMap::new - create instance of the sorted map
    • build list of pincode with the help of mapping users to their pincodes and storing the list: Collectors.mapping(User::getPinCode, Collectors.toList())
@Override
public TreeMap<String, List<String>> getCityWithPincode() {
    return findAllUser()
            .stream()
            .collect(Collectors.groupingBy(
                User::getCity,   // key - city name
                TreeMap::new,    // create TreeMap instance
                Collectors.mapping(
                    User::getPinCode, Collectors.toList()
                )               // List<String> pin codes
            ));
}
  • Related