gpt4 book ai didi

java - Map 还是 String[]?

转载 作者:行者123 更新时间:2023-11-29 03:46:50 24 4
gpt4 key购买 nike

我正在创建一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com