优化以下代码,考虑onWorkingEmployee中Name会重复的问题,去重: StringBuilder userName = new StringBuilder(); for (OnWorkingEmployeeVO onWorkingEmployee : onWorkingEmployees) { if (Objects.equals(onWorkingEmployee.getName(), onWorkingEmployees.get(onWorkingEmployees.size() - 1).getName())){ userName.append(onWorkingEmployee.getName()); }else{ userName.append(onWorkingEmployee.getName()).append(","); } }
时间: 2024-04-20 13:25:52 浏览: 101
可以使用Set来去重,然后再将结果拼接成字符串。以下是优化后的代码:
Set<String> uniqueNames = new HashSet<>();
for (OnWorkingEmployeeVO onWorkingEmployee : onWorkingEmployees) {
uniqueNames.add(onWorkingEmployee.getName());
}
StringBuilder userName = new StringBuilder();
for (String name : uniqueNames) {
userName.append(name).append(",");
}
// 去除最后一个逗号
if (userName.length() > 0) {
userName.deleteCharAt(userName.length() - 1);
}
相关问题
try { // 获取当前日期 LocalDate currentDate = LocalDate.now(); // 定义日期格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 转换为指定格式的字符串 String dateString = currentDate.format(formatter); // 构建URL对象 URL url = new URL(API_BASE_URL + "?begin=" + dateString + "&Methodname=" + URLEncoder.encode(str, "UTF-8")); LOGGER.info("车辆管理请求路径:"+url); // 创建HttpURLConnection对象 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为GET connection.setRequestMethod("GET"); // 设置Basic认证信息 String auth = API_USERNAME + ":" + API_PASSWORD; byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8); String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(authBytes); connection.setRequestProperty("Authorization", authHeaderValue); // 设置字符编码为UTF-8 connection.setRequestProperty("Accept-Charset", "UTF-8"); // 发送请求 int responseCode = connection.getResponseCode(); // 读取响应数据 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); StringBuilder responseBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { responseBuilder.append(line); } String response = responseBuilder.toString(); LOGGER.info("车辆管理响应结果:"+response); // 关闭连接 connection.disconnect(); // 返回响应数据 ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(response); String ls = rootNode.get("data").get(0).get("ls").asText().replaceAll("[^\u4e00-\u9fa5^\\d]", ""); LOGGER.info("车辆管理返回结果:"+ls); return ls; } catch (Exception e) { LOGGER.error("车辆管理调用失败:"+e.getMessage()); return "Error: " + e.getMessage(); }这段代码有没有可能产生跨域问题
这段代码不会产生跨域问题,因为它是在Java后端执行的,不涉及到浏览器和前端页面。跨域问题通常发生在浏览器端,当浏览器通过Ajax请求跨域资源时,会触发同源策略的限制。但是,如果在Java后端通过HttpURLConnection请求跨域资源,通常不会受到同源策略的影响,因为Java后端没有同源策略的限制。
try { // 获取当前日期 LocalDate currentDate = LocalDate.now(); // 定义日期格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 转换为指定格式的字符串 String dateString = currentDate.format(formatter); // 构建URL对象 URL url = new URL(API_BASE_URL + "?begin=" + dateString + "&Methodname=" + URLEncoder.encode(str, "UTF-8")); // 创建HttpURLConnection对象 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为GET connection.setRequestMethod("GET"); // 设置Basic认证信息 String auth = API_USERNAME + ":" + API_PASSWORD; byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8); String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(authBytes); connection.setRequestProperty("Authorization", authHeaderValue); // 设置字符编码为UTF-8 connection.setRequestProperty("Accept-Charset", "UTF-8"); // 发送请求 int responseCode = connection.getResponseCode(); // 读取响应数据 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); StringBuilder responseBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { responseBuilder.append(line); } String response = responseBuilder.toString(); // 关闭连接 connection.disconnect(); // 返回响应数据 ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(response); String ls = rootNode.get("data").get(0).toString(); return ls; } catch (Exception e) { e.printStackTrace(); return "Error: " + e.getMessage(); }其中ls的值是"{\"ls\":\"603\"}",修改一下,ls只需要”603“
在获取到 `ls` 的值时,可以使用 `JsonNode` 的 `get()` 方法获取到 `ls` 字段对应的值,然后使用 `String` 的 `replaceAll()` 方法去除多余的字符,具体修改如下:
```
try {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 转换为指定格式的字符串
String dateString = currentDate.format(formatter);
// 构建URL对象
URL url = new URL(API_BASE_URL + "?begin=" + dateString + "&Methodname=" + URLEncoder.encode(str, "UTF-8"));
// 创建HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为GET
connection.setRequestMethod("GET");
// 设置Basic认证信息
String auth = API_USERNAME + ":" + API_PASSWORD;
byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8);
String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(authBytes);
connection.setRequestProperty("Authorization", authHeaderValue);
// 设置字符编码为UTF-8
connection.setRequestProperty("Accept-Charset", "UTF-8");
// 发送请求
int responseCode = connection.getResponseCode();
// 读取响应数据
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseBuilder.append(line);
}
String response = responseBuilder.toString();
// 关闭连接
connection.disconnect();
// 返回响应数据
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(response);
String ls = rootNode.get("data").get(0).get("ls").asText().replaceAll("[^\u4e00-\u9fa5^\\d]", "");
return ls;
} catch (Exception e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
```
在上述代码中,使用了 `JsonNode` 的 `asText()` 方法将 `ls` 的值转化为字符串类型,然后使用 `replaceAll()` 方法去除多余字符,只保留数字和中文。
阅读全文