从 IntStream 返回 LinkedHashMap,Java 8

returning LinkedHashMap from IntStream, Java 8

我有这个代码。

  private static Map<Long, List<TimePitchValue>> alternativeMethod(AudioFormat audioformat, 
      List<ChunkDTO> listChunkDTO, long index, int sizeChunk) {

    int numBytesPerSample = audioformat.getSampleSizeInBits() / 8;
    int quantitySamples = sizeChunk / numBytesPerSample;
    long baseTime = quantitySamples * index;

    Map<Long, List<TimePitchValue>> mapListTimePitchValue = new LinkedHashMap<>();

    for (int i = 0; i < quantitySamples; i++) {
      int time = i;
      List<TimePitchValue> listTimePitchValueWithTime = listChunkDTO.stream().map(chunkDTO -> {
        int value = extractValue(chunkDTO.getChunk(), numBytesPerSample, time);
        TimePitchValue timePitchValue = new TimePitchValue(chunkDTO.getPitch(), baseTime + time, value);
        return timePitchValue;
      }).collect(Collectors.toList());
      listTimePitchValueWithTime.sort(Comparator.comparingInt(TimePitchValue::getValue).reversed());
      mapListTimePitchValue.put(baseTime + time, listTimePitchValueWithTime);
    }


    return mapListTimePitchValue;
  }

为每个 time 值生成一个 List<TimePitchValue>,名称为 listTimePitchValue,我希望将指定的 listTimePitchValue 与指定的 baseTime + time 关联起来mapListTimePitchValue.

支持

  private static int extractValue(byte[] bytesSamples, int numBytesPerSample, int time) {
    byte[] bytesSingleNumber = Arrays.copyOfRange(bytesSamples, time * numBytesPerSample, (time + 1) * numBytesPerSample);
    int value = numBytesPerSample == 2
        ? (Byte2IntLit(bytesSingleNumber[0], bytesSingleNumber[1]))
        : (byte2intSmpl(bytesSingleNumber[0]));
    return value;
  }

  public static int Byte2IntLit(byte Byte00, byte Byte08) {
    return (((Byte08) << 8)
        | ((Byte00 & 0xFF)));
  }

  public static int byte2intSmpl(byte theByte) {
    return (short) (((theByte - 128) & 0xFF)
        << 8);
  }

ChunkDTO class

public class ChunkDTO {

  private final byte[] chunk;
  private final long index;
  private final Integer pitch;

  public ChunkDTO(byte[] chunk, long index, Integer pitch) {
    this.chunk = chunk;
    this.index = index;
    this.pitch = pitch;
  }

  public byte[] getChunk() {
    return chunk;
  }

  public long getIndex() {
    return index;
  }

  public Integer getPitch() {
    return pitch;
  }

  @Override
  public String toString() {
    return "ChunkDTO{" + "index=" + index + ", pitch=" + pitch 
        + ", chunk.length=" + (chunk!=null?chunk.length:"null") + 
        '}';
  }

  @Override
  public int hashCode() {
    int hash = 5;
    hash = 29 * hash + (int) (this.index ^ (this.index >>> 32));
    hash = 29 * hash + Objects.hashCode(this.pitch);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final ChunkDTO other = (ChunkDTO) obj;
    if (this.index != other.index) {
      return false;
    }
    if (!Objects.equals(this.pitch, other.pitch)) {
      return false;
    }
    return true;
  }

}

TimePitchValue class

public class TimePitchValue {

  private int pitch;
  private long time;
  private int value;

  public TimePitchValue() {
  }

  public TimePitchValue(int pitch, long time, int value) {
    this.time = time;
    this.pitch = pitch;
    this.value = value;
  }

  public int getPitch() {
    return pitch;
  }

  public void setPitch(int pitch) {
    this.pitch = pitch;
  }

  public long getTime() {
    return time;
  }

  public void setTime(long time) {
    this.time = time;
  }

  public int getValue() {
    return value;
  }

  public void setValue(int value) {
    this.value = value;
  }

  @Override
  public String toString() {
    int length = String.valueOf(value).length();
    String stringValue = new String(new char[12 - length]).replace('[=14=]', ' ') + value;
    return "TimeNoteValue{" 
        + ", value='" + stringValue + "'}";
  }

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 13 * hash + this.pitch;
    hash = 13 * hash + (int) (this.time ^ (this.time >>> 32));
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final TimePitchValue other = (TimePitchValue) obj;
    if (this.pitch != other.pitch) {
      return false;
    }
    if (this.time != other.time) {
      return false;
    }
    return true;
  }

}

问题是,是否有可能 return 直接从 IntStream 排序 Map<Long, List<TimePitchValue>> (没有先前创建 Map<Long, List<TimePitchValue>> mapListTimePitchValue = new LinkedHashMap<>(); 和后来放置 mapListTimePitchValue.put(baseTime + time, listTimePitchValue);

这对你有用吗?

public static Map<Long, List<TimePitchValue>> alternativeMethodme(
        AudioFormat audioformat, List<ChunkDTO> listChunkDTO,
        long index, int sizeChunk) {

    int numBytesPerSample = audioformat.getSampleSizeInBits() / 8;
    int quantitySamples = sizeChunk / numBytesPerSample;
    long baseTime = quantitySamples * index;

    return IntStream.range(0, quantitySamples).boxed()
            .collect(Collectors.toMap(time -> baseTime + time,
                    time -> listChunkDTO.stream()
                            .map(chunkDTO -> new TimePitchValue(
                                    chunkDTO.getPitch(),
                                    baseTime + time,
                                    extractValue(
                                            chunkDTO.getChunk(),
                                            numBytesPerSample,
                                            time)))
                            .sorted(Comparator.comparingInt(
                                    TimePitchValue::getValue)
                                    .reversed())
                            .collect(Collectors.toList()),
                            (a,b)->a,
                            LinkedHashMap::new));
}

我想我终于弄明白了。我不得不对方法等进行一些重新排列。

已更新。我添加了排序并将地图作为 LinkedHashMap

返回