有没有更好的方法来处理向方法传递多个参数

发布于 2024-11-03 04:16:17 字数 892 浏览 1 评论 0原文

我发现自己做了很多以下事情:

/**
 * Redirect to a MVC controller&action
 * @param controller
 * @param action
 */
public void redirect(String controller, String action) {
    redirect(controller, action, new HashMap<String, String>());
}
/**
 * Redirect to a MVC controller&action with extra URL parameters
 * @param controller
 * @param action
 * @param data
 */
public void redirect(String controller, String action, Map<String, String> data) {
    String urlParameters = "";
    for(String key : data.keySet()) {
        urlParameters += "&" + key + "=" + data.get(key);
    }
    m_binder.putLocal("RedirectParams", "IdcService=MVC_FRONTCONTROLLER&controller="+controller+"&action="+action + urlParameters);
}

要调用第二种方法,我实际上需要创建一个哈希图来在其中添加数据,我想知道是否有更方便的方法来实现这一点?

如您所见,我需要知道键和值,因此可变参数不起作用(据我所知)。

我对所有想法持开放态度,包括使用反思。

I find myself doing the following a lot:

/**
 * Redirect to a MVC controller&action
 * @param controller
 * @param action
 */
public void redirect(String controller, String action) {
    redirect(controller, action, new HashMap<String, String>());
}
/**
 * Redirect to a MVC controller&action with extra URL parameters
 * @param controller
 * @param action
 * @param data
 */
public void redirect(String controller, String action, Map<String, String> data) {
    String urlParameters = "";
    for(String key : data.keySet()) {
        urlParameters += "&" + key + "=" + data.get(key);
    }
    m_binder.putLocal("RedirectParams", "IdcService=MVC_FRONTCONTROLLER&controller="+controller+"&action="+action + urlParameters);
}

To call the second method I actually need to create a hashmap to add data in it and I was wondering if there is a more convenient way of achieving this?

As you can see I need to know both the key and the value, so varargs wouldn't work (as far as I can see).

I'm open to all ideas, including using reflection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帅冕 2024-11-10 04:16:17

为什么必须创建新地图?我想您可以传入 null,然后在第二个 redirect 方法中检查 null 映射。无论如何,检查 null 可能是个好主意。

Why do you have to create a new map? I imagine you could just pass in null and then check for a null map in your second redirect method. It's probably a good idea to check for null anyway.

祁梦 2024-11-10 04:16:17

如果问题是创建新地图很尴尬,对于小地图,您可以使用 guava ImmutbleMap.of() 方法,

ImmutableMap.of("key1", "value1", "key2", "value2");

If the problem is that it is awkward to create a new map, for small maps you can use the guava ImmutbleMap.of() methods,

ImmutableMap.of("key1", "value1", "key2", "value2");
宫墨修音 2024-11-10 04:16:17

我不久前为构建地图编写了这个便捷方法。它需要可变参数并成对地生成一个映射。对于在测试代码中创建简单的地图来说,这非常方便。您需要确保参数的数量正确,但我喜欢它,因为它使代码量更小。

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> mapOf(K key, V value, Object... morePairs) {
    Map<K, V> map = new HashMap<K, V>();
    map.put(key, value);
    for (int i=0; i<morePairs.length; i+=2) {
        map.put((K)morePairs[i], (V)morePairs[i+1]);
    }
    return map;
}

然后您可以使用以下方法创建一个映射:

Map<String, String> map = mapOf("One", "1", "Two", "2");

然而,这并不是每个人都喜欢的(因为缺乏类型安全),因此您可以更改实现以采用对:

Map<String, String> map = mapOf(pair("One", "1"), pair("Two", "2"));

将pair定义为静态方法,该方法创建一个包含两个的简单对象值,然后是 mapOf 将这些对转换为映射中的条目。

I wrote this convenience method for build maps a while back. It takes varargs and makes a map out of them in pairs. For creating simple maps in test code, this is quite convenient. You need to make sure you get the numbers of parameters right but i like it because it makes the code volume smaller.

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> mapOf(K key, V value, Object... morePairs) {
    Map<K, V> map = new HashMap<K, V>();
    map.put(key, value);
    for (int i=0; i<morePairs.length; i+=2) {
        map.put((K)morePairs[i], (V)morePairs[i+1]);
    }
    return map;
}

Then you can create a map using:

Map<String, String> map = mapOf("One", "1", "Two", "2");

This is not everyone's cup of tea however (because of lack of type-safety) so you could change the implementation to take pairs:

Map<String, String> map = mapOf(pair("One", "1"), pair("Two", "2"));

Where you define pair as a static method that creates a simple object containing two values and then mapOf which converts those pairs into Entries in a Map.

你列表最软的妹 2024-11-10 04:16:17

你所做的事情是正常的、常见的。另一种方法是允许某些参数为空,并在第二种方法中进行一些空检查。您必须决定哪种方法您认为不那么难看。

我见过一些 API,其中相同的方法至少有 10 个这样的参数减少版本,显然没有任何原因。当它像这样膨胀时,你的设计就有问题了。在那之前,你所拥有的对我来说看起来不错,尽管我不太喜欢 Map 实例化。

What you have done is normal and common. An alternative is to allow some of the parameters to be null and do some null-checking in the second method. You will have to decide which approach you feel is less ugly.

I have seen APIs where the same method has at least 10 of these parameter-reduced versions for apparently no reason at all. When it balloons up like that, there is something very wrong with your design. Until then, what you have looks fine to me, although I don't really like the Map instantiation much.

逆夏时光 2024-11-10 04:16:17

我想说这是一种非常常见的方法。

我目前能想到的唯一其他(不太)方便的方法是:

public void redirect(String[]... params)
{
   //build the query string
}

then call

redirect(new String[][]{{"a","1"}, {"b", "2"}} );
redirect();

注意,这使用了可变参数,但使用起来不太安全(您可以传递长度为 1 甚至 0 的数组)。

I'd say that's a pretty common aproach.

The only other (not quite) convenient method I currently could think of would be:

public void redirect(String[]... params)
{
   //build the query string
}

then call

redirect(new String[][]{{"a","1"}, {"b", "2"}} );
redirect();

Note that this uses varargs but it's not quite safe to use (you could pass arrays of length 1 or even 0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文