在Java中HashMap<Byte, ArrayList<byte[][]>>[] deathPool = new HashMap[2];这个怎么用?
时间: 2023-10-09 07:12:08 浏览: 42
这是一个定义了两个元素的HashMap数组,每个元素都是一个Byte类型的key和一个ArrayList<byte[][]>类型的value的映射。可以使用以下方式来使用它:
1. 初始化HashMap数组
```
deathPool[0] = new HashMap<Byte, ArrayList<byte[][]>>();
deathPool[1] = new HashMap<Byte, ArrayList<byte[][]>>();
```
2. 向HashMap中添加元素
```
Byte key = 1;
ArrayList<byte[][]> value = new ArrayList<byte[][]>();
deathPool[0].put(key, value);
```
3. 从HashMap中获取元素
```
Byte key = 1;
ArrayList<byte[][]> value = deathPool[0].get(key);
```
需要注意的是,由于Java中的泛型类型擦除,不能直接使用`new HashMap<Byte, ArrayList<byte[][]>>[2]`来初始化HashMap数组,因此需要使用`deathPool[0] = new HashMap<Byte, ArrayList<byte[][]>>()`来为每个元素初始化一个HashMap。
相关问题
HashMap<Byte, ArrayList<byte[][]>>[] deathPool = new HashMap<Byte, ArrayList<byte[][]>>[2]; Java中这段代码报错怎么办?
在 Java 中,泛型数组的创建是不被允许的,因为它可能会导致类型擦除问题。对于这个问题,可以使用一个 ArrayList 数组来代替泛型数组。修改代码如下:
```
ArrayList<HashMap<Byte, ArrayList<byte[][]>>>[] deathPool = new ArrayList[2];
```
或者,也可以使用参数化类型来代替泛型数组。修改代码如下:
```
HashMap<Byte, ArrayList<byte[][]>>[] deathPool = (HashMap<Byte, ArrayList<byte[][]>>[]) new HashMap<?, ?>[2];
```
这里使用了强制类型转换,不过要注意,这种方式会产生一个未经检查的警告。
public String bxJsonTobin(List<LightGroupReq> lightGroupReqs, Integer binHashCode, List<String> lightGroupList) { LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs, VehicleConstants.VEHICLE_X); ArrayList<String> list = new ArrayList<>(); ArrayList<String> writeBinList = new ArrayList<>(); Map<Integer, Frames> portMap = new HashMap<>(); List<Frames> frameList = new ArrayList<>(); for (int i = 1; i <= 78; i++) { frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } groupingFrames.put((long) groupingFrames.size(), frameList); long timeStamp = 0; int[] temp = new int[79]; Arrays.fill(temp, -1); for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) { List<Frames> framesList = entry.getValue(); for (int i = 0; i < framesList.size(); i++) { temp[framesList.get(i).getGroupIndex()] = 1; } for (int i = 1; i < temp.length; i++) { if (temp[i] == -1) { portMap.put(i, new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } } framesList.addAll(portMap.values()); portMap.clear(); framesList = framesList.stream().sorted(Comparator.comparing(Frames::getGroupIndex)).collect(Collectors.toList()); if (timeStamp > 7) { timeStamp = 0; } for (int j = 0; j < framesList.size(); j++) { list.add(BinaryFileUtils.bxFramesToHex(framesList.get(j), timeStamp, writeBinList)); } timeStamp++; Arrays.fill(temp, -1); } return fileService.uploadBin(list, binHashCode); }优化这段代码
首先,可以将一些常量抽取出来,避免魔数出现在代码中,增加代码的可读性。例如:
```
private static final int FRAME_COUNT = 78;
private static final int BYTE_COUNT = 7;
private static final int RED = 63;
private static final int GREEN = 63;
private static final int BLUE = 2550;
private static final int BRIGHTNESS = 15;
private static final int SPEED = 15;
private static final int MODE = 2;
```
接下来,可以将一些变量和对象的声明放到使用它们的地方,避免不必要的变量和对象声明,提高代码的可读性和性能。例如:
```
for (Map.Entry<Long, List<Frames>> entry : jsonToBinPub(lightGroupReqs, VehicleConstants.VEHICLE_X).entrySet()) {
int[] temp = new int[FRAME_COUNT + 1];
Arrays.fill(temp, -1);
List<Frames> framesList = entry.getValue();
for (Frames frame : framesList) {
temp[frame.getGroupIndex()] = 1;
}
Map<Integer, Frames> portMap = IntStream.rangeClosed(1, FRAME_COUNT)
.filter(i -> temp[i] == -1)
.boxed()
.collect(Collectors.toMap(Function.identity(), i -> new Frames(BYTE_COUNT, RED, GREEN, BLUE, BRIGHTNESS, SPEED, 0, i, 0, MODE)));
framesList.addAll(portMap.values());
framesList.sort(Comparator.comparing(Frames::getGroupIndex));
long timeStamp = entry.getKey() % 8;
for (Frames frame : framesList) {
list.add(BinaryFileUtils.bxFramesToHex(frame, timeStamp, writeBinList));
}
}
```
上述代码中的改进包括:
1. 将 `groupingFrames` 的声明去掉,直接使用 `jsonToBinPub` 的返回值;
2. 将 `temp` 的声明放到了 for 循环里面,减少了不必要的变量声明;
3. 使用 Java 8 中的 Stream API 来生成 `portMap`,增加了代码的可读性;
4. 将 `framesList` 的排序和遍历合并到了一起;
5. 将 `timeStamp` 的计算放到了 for 循环里面,避免了重复计算。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)