Stream相關(guān)

使用groupingBymapping

//構(gòu)建基本模型類(lèi)Person
public static class Person {
    private String name;
    private String type;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Person(String name, String type) {
        this.name = name;
        this.type = type;
    }
}

//使用
@Test
public void test24() {
    List<Person> personList = new ArrayList<>();
    personList.add(new Person("1", "8"));
    personList.add(new Person("2", "7"));
    personList.add(new Person("3", "6"));
    personList.add(new Person("2", "5"));
    personList.add(new Person("5", "4"));

    Map<String, Set<String>> map = personList
            .stream()
            .collect(Collectors.groupingBy(Person::getName,
                    Collectors.mapping(Person::getType, Collectors.toSet())));
    // toMap需要傳進(jìn)去一個(gè)BiFunction 用于發(fā)現(xiàn)key重復(fù)時(shí)采取的策略,是覆蓋還是不覆蓋
    Map<String, String> map1 = personList
            .stream()
            .collect(Collectors.toMap(Person::getName,
                    t -> t.getType(),
                    (a, b) -> a));
    System.out.println(map);
    System.out.println(map1);
}

輸出:
//map
{1=[8], 2=[5, 7], 3=[6], 5=[4]}
//map1
{1=8, 2=7, 3=6, 5=4}

使用Reduce

package reduce;

/**
 * @Description
 * @Authror taren
 * @DATE 2019/7/10 14:36
 */
public class Foo {
private String name;
private String type;
private Double typeValue;
private Integer count;

public Foo(String name, String type, Double typeValue, Integer count) {
    this.name = name;
    this.type = type;
    this.typeValue = typeValue;
    this.count = count;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Double getTypeValue() {
    return typeValue;
}

public void setTypeValue(Double typeValue) {
    this.typeValue = typeValue;
}

public Integer getCount() {
    return count;
}

public void setCount(Integer count) {
    this.count = count;
}
}

@Test
public void test2() {
    List<Foo> fooList = Lists.newArrayList(
            new Foo("A", "san", 1.0, 2),
            new Foo("A", "nas", 13.0, 1),
            new Foo("B", "san", 112.0, 3),
            new Foo("C", "san", 43.0, 5),
            new Foo("B", "nas", 77.0, 7),
            new Foo("D", "tan", 79.0, 9)
    );
    Integer e = fooList.stream().map(Foo::getCount).reduce(Integer::min).orElse(new Integer(2));
    System.out.println(e);
}
這是最簡(jiǎn)單的使用reduce了,有時(shí)間繼續(xù)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容