从Java 8 IntStream返回LinkedHashMap [英] returning LinkedHashMap from IntStream, Java 8

查看:219
本文介绍了从Java 8 IntStream返回LinkedHashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码.

  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

都会生成一个名为listTimePitchValueList<TimePitchValue>,我希望将指定的listTimePitchValuemapListTimePitchValue中的指定baseTime + time关联.

for each time value a List<TimePitchValue> is generated with the name listTimePitchValue, and I want Associates the specified listTimePitchValue with the specified baseTime + time in 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

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

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('\0', ' ') + 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;
  }

}

问题是,是否可以直接从IntStream返回排序后的Map<Long, List<TimePitchValue>>? (以前没有创建Map<Long, List<TimePitchValue>> mapListTimePitchValue = new LinkedHashMap<>();,后来又没有创建mapListTimePitchValue.put(baseTime + time, listTimePitchValue);)

The Question is, is it possible return a sorted Map<Long, List<TimePitchValue>> from the IntStream directly? (without previously create Map<Long, List<TimePitchValue>> mapListTimePitchValue = new LinkedHashMap<>(); and later put 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));
}

我想我终于明白了.我不得不重新整理方法之类的.

I think I finally figure this out. I had to do some rearanging of methods and such.

已更新.我添加了排序并以LinkedHashMap

Updated. I added the sort and returned the map as a LinkedHashMap

这篇关于从Java 8 IntStream返回LinkedHashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆