Skip to content

组合一组对象为 Map , 偶数位对象为键, 奇数位对象为值. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wave-gbt opened this issue Apr 28, 2019 · 1 comment

Comments

@wave-gbt
Copy link
Owner

wave-gbt commented Apr 28, 2019

public static Map asMap(Object... objects) {
        Map m = new HashMap(16);
        for (int i = 0; i < objects.length; i += 2) {
            if (i + 1 < objects.length) {
                m.put(objects[i], objects[i + 1]);
            } else {
                m.put(objects[i], null);
            }
        }
        return m;
    }
@wave-gbt
Copy link
Owner Author

wave-gbt commented Sep 27, 2019

public static Map toMap(Object... objs) {
        Map map = new HashMap();
        Object key = null;

        for(int i = 0; i < objs.length; ++i) {
            if (i % 2 == 0) {
                key = objs[i];
            } else {
                map.put(key, objs[i]);
            }
        }

        return map;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant