第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何根據(jù)項(xiàng)目屬性的總和對(duì)集合進(jìn)行分區(qū),直至達(dá)到給定的限制?

如何根據(jù)項(xiàng)目屬性的總和對(duì)集合進(jìn)行分區(qū),直至達(dá)到給定的限制?

慕絲7291255 2022-09-22 19:23:49
如何根據(jù)集合中每個(gè)項(xiàng)目的一個(gè)字段求和,將集合分塊為 N 個(gè)塊,直至達(dá)到給定的最大值?例如,給定以下內(nèi)容:class FileObject { public long sizeInBytes; }Collection<FileObject> files;long MAX_SIZE_THRESHOLD = 1024 * 1024 * 100; // 100 MB我想將項(xiàng)轉(zhuǎn)換為具有最少數(shù)量的內(nèi)部集合的項(xiàng),并滿足謂詞,即對(duì)于每個(gè)集合,每個(gè)元素的總和小于 。Collection<Collection<FileObject>>sizeInBytesMAX_SIZE_THRESHOLD進(jìn)一步說(shuō)明,除了上述要求之外,如果擴(kuò)展為包含時(shí)間戳,我還想按年,月和日對(duì)結(jié)果進(jìn)行分區(qū)。FileObject例如class FileObject { public long sizeInBytes; public long modifiedDate; }我希望最終結(jié)果如下所示:Map<Integer, Map<Integer, Map<Integer, Collection<FileObject>>>>其中映射中的鍵是:年、月和日(對(duì)應(yīng)于 的 ),并且集合包含該年、月、日內(nèi)的所有文件,并且每個(gè)文件的大小英寸之和小于 。FileObjectmodifiedDateMAX_SIZE_THRESHOLD是否可以在避免循環(huán)和使用使用 Stream API 或其他可用的函數(shù)構(gòu)造的同時(shí)完成這兩個(gè)操作??jī)烧叨伎梢栽谝粋€(gè)語(yǔ)句中完成嗎?
查看完整描述

1 回答

?
天涯盡頭無(wú)女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以在流交易中嘗試。下面是示例代碼:StreamEx.collapse(...)

final long MAX_SIZE_THRESHOLD = 12; // only for test purpose.


// create the sample file objects with random size for test.

Collection<FileObject> files =

    new Random().longs(0, 1000).limit(50).mapToObj(n -> new FileObject(n % 15, n))

    .collect(Collectors.toList());


// here is the final solution you can try

final MutableLong remaining = MutableLong.of(MAX_SIZE_THRESHOLD);


List<List<FileObject>> result = StreamEx.of(files).collapse((a, b) -> {

  if (b.sizeInBytes <= remaining.value() - a.sizeInBytes) {

    remaining.subtract(a.sizeInBytes);

    return true;

  } else {

    remaining.setValue(MAX_SIZE_THRESHOLD);

    return false;

  }

}, Collectors.toList()).toList();


result.forEach(System.out::println);

以下是您問(wèn)題的第2部分嵌套的解決方案:groupingBy


// import static java.util.stream.Collectors.*

Map<Integer, Map<Integer, Map<Integer, List<FileObject>>>> result2 = files.stream()

    .filter(f -> f.sizeInBytes < MAX_SIZE_THRESHOLD)

    .collect(groupingBy(f -> f.getYear(), 

                        groupingBy(f -> f.getMonth(), 

                                        groupingBy(f -> f.getDay(), toList()))));


result2.entrySet().forEach(System.out::println);

最后,這是我用來(lái)測(cè)試的:FileObject


static class FileObject {

  public long sizeInBytes;

  public long modifiedDate;


  public FileObject(long sizeInBytes, long modifiedDate) {

    this.sizeInBytes = sizeInBytes;

    this.modifiedDate = modifiedDate;

  }


  public int getYear() {

    return (int) modifiedDate / 100; // only for test purpose

  }


  public int getMonth() {

    return (int) (modifiedDate % 100) / 10; // only for test purpose

  }


  public int getDay() {

    return (int) modifiedDate % 10; // only for test purpose

  }


  @Override

  public String toString() {

    return sizeInBytes + "-" + modifiedDate;

  }

}

根據(jù)評(píng)論更新:


您將需要 .Collectors.collectAndThen


Function<List<FileObject>, List<List<FileObject>>> finisher = fileObjs -> {

  MutableLong remaining2 = MutableLong.of(MAX_SIZE_THRESHOLD);

  return StreamEx.of(fileObjs).collapse((a, b) -> {

    if (b.sizeInBytes <= remaining2.value() - a.sizeInBytes) {

      remaining2.subtract(a.sizeInBytes);

      return true;

    } else {

      remaining2.setValue(MAX_SIZE_THRESHOLD);

      return false;

    }

  }, toList()).toList();

};


Map<Integer, Map<Integer, Map<Integer, List<List<FileObject>>>>> result4 = files.stream()

    .collect(groupingBy(f -> f.getYear(),

        groupingBy(f -> f.getMonth(), 

            groupingBy(f -> f.getDay(), collectingAndThen(toList(), finisher)))));

結(jié)果類(lèi)型應(yīng)為 ,而不是 。Map<Integer, Map<Integer, Map<Integer, List<List<FileObject>>>>>Map<Integer, Map<Integer, Map<Integer, List<FileObject>>>>


順便說(shuō)一句,如果你不想寫(xiě)函數(shù)(我不:-)),試試我的庫(kù):算盤(pán)-Util:finisher


Function<List<FileObject>, List<List<FileObject>>> finisher2 = fileObjs -> Seq.of(fileObjs)

    .split(MutableLong.of(0), (f, sizeSum) -> sizeSum.addAndGet(f.sizeInBytes) <= MAX_SIZE_THRESHOLD,

        sizeSum -> sizeSum.setValue(0));


// import static com.landawn.abacus.util.stream.Collectors.MoreCollectors.*;

StreamEx.of(files)

    .toMap(f -> f.getYear(),

        groupingBy(f -> f.getMonth(),

            groupingBy(f -> f.getDay(), collectingAndThen(toList(), finisher2))));




查看完整回答
反對(duì) 回復(fù) 2022-09-22
  • 1 回答
  • 0 關(guān)注
  • 89 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)