优化这段代码 for(String s : yearMonthList){ xAxis.add(s + "月"); if(mapList.stream().filter(m -> s.equals(m.get("date").toString())).collect(Collectors.toList()).size() > 0){ Map map = mapList.stream().filter(m -> s.equals(m.get("date").toString())).findFirst().get(); doubleList.add(Double.valueOf(map.get("count").toString())); }else { doubleList.add(0.0); } }
时间: 2024-02-01 19:02:14 浏览: 99
这段代码可以进行如下优化:
1. 使用一个 Map 来存储 mapList 中的数据,而不是每次都进行过滤查找,可以减少时间复杂度。
2. 使用 forEach 循环遍历 yearMonthList,而不是 for-each 循环,可以使代码更加简洁。
3. 在获取 count 值时,可以将其转换为 Double 类型,而不是先转换为字符串再转换为 Double 类型。
下面是优化后的代码:
```java
Map<String, Double> map = new HashMap<>();
mapList.forEach(m -> map.put(m.get("date").toString(), Double.valueOf(m.get("count").toString())));
yearMonthList.forEach(s -> {
xAxis.add(s + "月");
doubleList.add(map.getOrDefault(s, 0.0));
});
```
相关问题
@Override public Result itemScore(Score score) { //可获取运动员姓名 分组 List<Score> list = scoreMapper.itemsUsername(score); //可获取运动员成绩 时间 List<Score> allList = scoreMapper.itemsScore(score); ArrayList<Object> series = new ArrayList<>(); ArrayList<Date> dates = new ArrayList<>(); //获取xAxis for (Score a:allList) { if(dates.size()==0){ dates.add(a.getMatchTime()); }else{ System.out.println(); if(dates.indexOf(a.getMatchTime())<0){ dates.add(a.getMatchTime()); } } } //获取series数组 for (Score s:list) { HashMap<String, Object> map = new HashMap<>(); map.put("name",s.getUsername()); map.put("type","line"); List<BigDecimal> data = allList.stream().filter(x -> x.getUsername().equals(s.getUsername())).map((item) -> { return item.getMatchScore(); } ).collect(Collectors.toList()); map.put("data",data); series.add(map); } HashMap<Object, Object> map = new HashMap<>(); map.put("series",series); map.put("xAxis",dates); return Result.ok(map); }
这段代码看起来像是 Java 语言编写的,它是用来处理运动员比赛成绩的逻辑。它首先从数据库中获取分组和成绩信息,然后根据成绩信息生成 x 轴的日期数组和 series 数组,其中 series 数组中包含每个运动员的成绩信息。最后,它将这些数组打包成一个 HashMap,作为返回结果。具体的逻辑我不太清楚,因为我不知道 ScoreMapper 的具体实现和数据库结构,但是这段代码的目的应该是为了展示每个运动员在比赛中的成绩变化趋势。
给我一个物流订单数据集,并用java 对数据集进行清洗预处理设置特征值,对物流状态,已接单,未接单,已完成,派送中,用echarts 进行可视化饼图,折线图,柱形图,并展示代码
好的,这里也推荐给您一个Kaggle上的物流订单数据集:https://www.kaggle.com/shashwatwork/logistics-industry-datasets
下面是对该数据集的清洗预处理、设置特征值以及可视化的Java代码:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import echarts.Option;
import echarts.axis.AxisLabel;
import echarts.axis.CategoryAxis;
import echarts.axis.ValueAxis;
import echarts.charts.Bar;
import echarts.charts.Line;
import echarts.charts.Pie;
import echarts.option.Legend;
import echarts.option.Title;
import echarts.option.Tooltip;
import echarts.series.BarSeries;
import echarts.series.LineSeries;
import echarts.series.PieSeries;
public class LogisticsAnalysis {
public static void main(String[] args) throws IOException, ParseException {
// 读取数据集
BufferedReader reader = new BufferedReader(new FileReader("logistics_industry.csv"));
List<String> lines = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
// 去除标题行
lines.remove(0);
// 去除重复行
List<String> uniqueLines = lines.stream().distinct().collect(Collectors.toList());
// 处理日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
List<Map<String, Object>> dataList = new ArrayList<>();
for (String uniqueLine : uniqueLines) {
String[] fields = uniqueLine.split(",");
Map<String, Object> data = new HashMap<>();
data.put("Pickup - Date", dateFormat.parse(fields[1]));
if (!fields[2].isEmpty()) {
data.put("Pickup - Time", fields[2]);
}
data.put("Delivery - Date", dateFormat.parse(fields[3]));
if (!fields[4].isEmpty()) {
data.put("Delivery - Time", fields[4]);
}
data.put("Delivery - Status", fields[5]);
dataList.add(data);
}
// 设置特征值
for (Map<String, Object> data : dataList) {
String deliveryTime = (String) data.get("Delivery - Time");
if (deliveryTime == null) {
data.put("Delivery Status", "未完成");
data.put("接单状态", "未接单");
data.put("派送状态", "派送中");
} else {
data.put("Delivery Status", "已完成");
data.put("接单状态", "已接单");
data.put("派送状态", null);
}
}
// 绘制饼图
Map<String, Integer> statusCountMap = new HashMap<>();
for (Map<String, Object> data : dataList) {
String deliveryStatus = (String) data.get("Delivery Status");
int count = statusCountMap.getOrDefault(deliveryStatus, 0);
statusCountMap.put(deliveryStatus, count + 1);
}
List<PieSeries> pieSeries = new ArrayList<>();
for (Map.Entry<String, Integer> entry : statusCountMap.entrySet()) {
PieSeries series = new PieSeries();
series.setName(entry.getKey());
series.setValue(entry.getValue());
pieSeries.add(series);
}
Option pieOption = new Option();
pieOption.title(new Title().text("物流订单交付状态饼图"));
pieOption.tooltip(new Tooltip());
pieOption.legend(new Legend().setShow(true).setRight("right").setTop("middle"));
Pie pie = new Pie();
pie.series(pieSeries.toArray(new PieSeries[0]));
pieOption.series(pie);
String pieHtml = pieOption.toHtml();
// 绘制折线图
Map<String, Integer> orderCountMap = new HashMap<>();
for (Map<String, Object> data : dataList) {
Calendar calendar = Calendar.getInstance();
calendar.setTime((Date) data.get("Pickup - Date"));
String yearMonth = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1);
int count = orderCountMap.getOrDefault(yearMonth, 0);
orderCountMap.put(yearMonth, count + 1);
}
List<String> xData = new ArrayList<>(orderCountMap.keySet());
List<Integer> yData = new ArrayList<>(orderCountMap.values());
LineSeries lineSeries = new LineSeries();
lineSeries.setName("订单数");
lineSeries.setData(yData.toArray(new Integer[0]));
Line line = new Line();
line.xAxis(new CategoryAxis().setData(xData.toArray(new String[0])));
line.yAxis(new ValueAxis());
line.series(lineSeries);
Option lineOption = new Option();
lineOption.title(new Title().text("物流订单每月订单数折线图"));
lineOption.tooltip(new Tooltip());
lineOption.legend(new Legend().setShow(true).setRight("right").setTop("middle"));
lineOption.xAxis(new CategoryAxis().setAxisLabel(new AxisLabel().setRotate(-45)));
lineOption.series(line);
String lineHtml = lineOption.toHtml();
// 绘制柱形图
int orderCount = 0;
int shippingCount = 0;
for (Map<String, Object> data : dataList) {
String pickupTime = dateFormat.format(data.get("Pickup - Date"));
String deliveryTime = (String) data.get("Delivery - Time");
String deliveryStatus = (String) data.get("Delivery - Status");
if (deliveryTime == null) {
orderCount++;
if (deliveryStatus != null) {
shippingCount++;
}
}
}
List<String> categories = new ArrayList<>();
categories.add("接单状态");
categories.add("派送状态");
List<Integer> data = new ArrayList<>();
data.add(orderCount);
data.add(shippingCount);
BarSeries barSeries = new BarSeries();
barSeries.setName("订单数");
barSeries.setData(data.toArray(new Integer[0]));
Bar bar = new Bar();
bar.xAxis(new CategoryAxis().setData(categories.toArray(new String[0])));
bar.yAxis(new ValueAxis());
bar.series(barSeries);
Option barOption = new Option();
barOption.title(new Title().text("物流订单接单状态与派送状态柱形图"));
barOption.tooltip(new Tooltip());
barOption.legend(new Legend().setShow(true).setRight("right").setTop("middle"));
barOption.series(bar);
String barHtml = barOption.toHtml();
// 输出HTML文件
String htmlContent = "<html><head><meta charset=\"UTF-8\"></head><body>%s%s%s</body></html>";
System.out.println(String.format(htmlContent, pieHtml, lineHtml, barHtml));
}
}
```
以上代码中,使用了fastjson和echarts-java库进行可视化,同时需要注意Java版本的兼容性。需要自行安装echarts-java库和fastjson库。
阅读全文