java - Map<Integer, String> 还是 String[]?

标签 java arrays string performance dictionary

我正在创建一个 Java EE 客户端,我需要在其中调用节点 (js) 服务器以获取响应。所以我做了一个类,通过它向节点服务器发出请求。 每次收到响应时,我都需要发回响应代码和响应本身。所以我想创建一个包含响应代码和响应的字符串数组。

这是我的类(class):

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.core.impl.provider.entity.StringProvider;

public class RequestHandler {

/**
 * Makes a HTTP Request for a given url. The type
 * determines which type of request, post or get,
 * will be made. The params cane be in form of
 * name=value pair or JSON format.
 * 
 * @param type Request method. 1 - Get, 2 - Post.
 * @param url url string to which request has to be 
 * made.
 * @param path path of the resource or service for a
 * url.
 * @param params request parameters. Can be either
 * name=value pair or JSON request.
 * 
 * @return String representation of the response.
 * 
 */
public static String[] makeRequest(int type, String url, String path, String params) {
    String[] response = new String[2];
    ClientResponse clientResponse = null;
    try {
        ClientConfig config = new DefaultClientConfig();
        config.getClasses().add(StringProvider.class); 
        Client client = Client.create(config);

        WebResource service =
                client.resource(url); 
        client.addFilter(new LoggingFilter());
        service.path("rest");

        // 1 - GET, 2 - POST
        switch (type) {
            case 1: {
                System.out.println("Making GET request to: " + url + path);
                System.out.println("Request Params: " + params);
                clientResponse = service.path(path).type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); //TODO Code to be corrected, include params
                break;
            }
            case 2: {
                System.out.println("Making POST request to: " + url + path);
                System.out.println("Request Params: " + params);
                clientResponse = service.path(path).type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, params);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        response[0] = "500";
        response[1] = "Internal Server Error";
        return response;
    }

    response[0] = String.valueOf(clientResponse.getStatus());
    response[1] = clientResponse.getEntity(String.class);

    System.err.println("Response status: " + response[0]);

    return response;
    }//end of makeRequest()

}//end of class

但我不相信创建过多的字符串对象会导致性能问题。所以我想创建一个 Map 来发回响应代码和响应。

response.put(500, "Internal Server Error");

但同样,使用整数创建映射并在每次响应代码时进行检查会产生 Integer 对象的装箱和拆箱开销,这同样会影响性能。

HashMap<Integer, String> response = RequestHandler.makeRequest(2, urlString, "/login", params);

if (response.containsKey(500)) {
 return Message.INTERNAL_SERVER_ERROR;
}

我应该使用哪一个以获得更好的性能?还是有更好的选择?

最佳答案

假设 HashMap 包含一个数组,加上一堆管理项(大小、阈值、负载因子),String[] 数组会更小(因此,从某种意义上说,比 HashMap 更有效)。

但是,请避免 premature optimisation .

关于java - Map<Integer, String> 还是 String[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510118/

相关文章:

Java IO 无法读取输入文件?

java - 如何将 Javascript 数组结果输入到 Java 中?

python - 使 np.sums 的 for 循环运行得更快的方法

javascript - 有没有办法通过 JavaScript 从 innerText 创建单个单词的数组?

确定不定冠词的 Javascript 库

string - "Contains"Delphi 字符串助手是否区分大小写?

java - 是否可以以编程方式配置 JAXB?

arrays - 解析带有引用字段的字符串,如 Powershell 中的 CSV 行

c++ - 如何更改此 vector 函数以接受更多元素?

java - 在接收器外部的应用程序链接中打开应用程序