Java a function Enummap returns multiple values

Source: Internet
Author: User

In the development process, it is often the case that a function needs to return multiple values, this is a problem!!

The solution to this problem online:

1. Use map return value; The problem with this method is that you don't know how to return the value of the key, which can only be viewed through doc or through source code.

2. Pass in a reference and modify the referenced property value. Problem: not practical.

3, through the generic structure of a python-like tuple class, or construct a javabean, the problem is "one-time", not graceful.

Personal solutions:

Using Enummap as the return value type, define an enum yourself and define the property names that might be returned as enum values.

Package Com.cy;import java.util.enummap;/** * Created-on 2014/7/10. */public interface Testservice {    enum userinfoproperty {        room,cellphone,name    } public    enummap< Userinfoproperty,object> getuserinfobyname (String name);}

This class is implemented

public class Testserviceimpl implements Testservice {    @Override public    enummap<userinfoproperty, Object > getuserinfobyname (String name) {        enummap<userinfoproperty,object> retmap = new enummap< Userinfoproperty, object> (userinfoproperty.class);        Retmap.put (Userinfoproperty.room, "0003");        Retmap.put (Userinfoproperty.cellphone, "00004");        Retmap.put (userinfoproperty.name,name);        return retmap;}    }

This class is the main entrance

public class App {public    static void Main (string[] args)    {        Testservice testservice = new Testserviceimpl ();        String name = "TestName";        enummap<testservice.userinfoproperty,object> userInfo = testservice.getuserinfobyname (name);         Userinfo.entryset (). iterator ();        System.out.println (UserInfo.get (TestService.UserInfoProperty.Name));        System.out.println (UserInfo.get (TestService.UserInfoProperty.ROOM));        System.out.println (UserInfo.get (TestService.UserInfoProperty.CELLPHONE));}    }

Java a function Enummap returns multiple values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.