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

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

java - 如何使用Java中的流比較和操作一個(gè)列表中的兩個(gè)相鄰元素?

java - 如何使用Java中的流比較和操作一個(gè)列表中的兩個(gè)相鄰元素?

慕無(wú)忌1623718 2022-05-12 18:52:43
背景是我有兩個(gè)字符串類(lèi)型變量 str1 和 str2 作為輸入。最后,我必須返回一個(gè)列表,其中包含 str1 的連續(xù)前綴,該前綴小于 str2 中的相關(guān)前綴。我有這樣的Java代碼:public List<Character> getPrefix(String str1, String str2) {    int index = 0;    List<Character> res = new ArrayList<>();    //str1 = "1243"    //str2 = "2324"    // The answer will be "12".    while (index < str1.length() && index < str2.length() && str1.charAt(index) <= str2.charAt(index)) {        res.add(str1.charAt(index));        index++;    }     return res;}//the return type could either be List<String> or List<Character>我被要求在流中轉(zhuǎn)換此代碼而不使用 while 或 for 循環(huán),只是在流方法中。我打算像這樣轉(zhuǎn)換這段代碼List<String> list = new ArrayList<>();list.add(str1);list.add(str2);List<String> res = list.stream().filter().reduce();我發(fā)現(xiàn)filter()方法可以選擇與給定謂詞匹配的元素,并且reduce()方法可以使用標(biāo)識(shí)和累加器來(lái)獲得一個(gè)最終結(jié)果。但是我發(fā)現(xiàn)我既沒(méi)有辦法操作一個(gè)列表中的兩個(gè)相鄰元素,也沒(méi)有辦法獲得一個(gè)指針來(lái)比較和遍歷列表中每個(gè)元素中的每個(gè)字符(該元素是字符串類(lèi)型)。那么有什么方法可以操作一個(gè)列表中的兩個(gè)相鄰元素,以便我可以比較它們?cè)谕晃恢玫淖址?
查看完整描述

2 回答

?
www說(shuō)

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

你可以:

  1. 生成索引流

  2. 使用索引獲取兩個(gè)字符串的字符

  3. 有效時(shí)選擇字符

// The magic

public static List<Character> getPrefix(String str1, String str2) {


    return IntStream

        .range(0, Math.min(str1.length(), str2.length()))

        .mapToObj(i -> new char[] { str1.charAt(i), str2.charAt(i) })

        .takeWhile(a -> a[0] < a[1])

        .map(a -> a[0])

        .collect(Collectors.toList());


}


查看完整回答
反對(duì) 回復(fù) 2022-05-12
?
冉冉說(shuō)

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

看看代碼,也許這就是你想要的。仍然可以進(jìn)一步增強(qiáng)它并且不能解決第一個(gè)字符串以大于第二個(gè)的值開(kāi)始的情況。這也可以實(shí)現(xiàn),但需要額外的工作。(不能一次性完成,因?yàn)楣?yīng)商消耗了一個(gè)元素來(lái)進(jìn)行鏈接 dropWhile 和 takeWhile 所需的檢查)。簡(jiǎn)單地說(shuō),通過(guò)供應(yīng)商,您可以將流中的元素與其他數(shù)據(jù)結(jié)構(gòu)中的元素進(jìn)行比較。


import java.util.LinkedList;

import java.util.function.Supplier;

import java.util.stream.Collectors;


public class Pre1 {


    public static void main(String[] args) {

        System.out.println(new Pre1().getPre("1234", "2315"));

        System.out.println(new Pre1().getPre("941234", "712315"));

        System.out.println(new Pre1().getPre("2345", "341"));   

    }


    public String getPre(String s1, String s2) {

        //second list is used as supplier

        LinkedList<Integer> l2 = s2.chars().boxed()

            .map(t->Character.getNumericValue(t))

            .collect(Collectors.toCollection(LinkedList<Integer>::new));

        //l2.forEach(System.out::println);

        Supplier<Integer> supplier = () -> {

        //   System.out.println(l2.peek());

             return l2.isEmpty() ? 0 : l2.pollFirst();

        };


        return s1.chars().boxed()

             .map(t->Character.getNumericValue(t))

             .takeWhile(t->t<supplier.get())

             .map(t->String.valueOf(t))

             .collect(Collectors.joining());

    }

}

輸出


12

nothing 

23


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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