參考鏈接: 使用Lambda表達式檢查字符串在Java中是否僅包含字母
public static void main(String[] args) {
? ? List<User> userList = new ArrayList<User>();
? ? User user0 = new User("han1", "男1", 20);
? ? User user1 = new User("han1", "男", 25);
? ? User user2 = new User("han2", "男", 21);
? ? User user3 = new User("han3", "男", 22);
? ? User user4 = new User("han4", "男", 23);
? ? User user5 = new User("han5", "男", 24);
? ? userList.add(user0);
? ? userList.add(user1);
? ? userList.add(user2);
? ? userList.add(user3);
? ? userList.add(user4);
? ? userList.add(user5);
? ? /**
? ? ?* list 轉map
? ? ?* 注意:要是key重復的話 會報錯Duplicate key ....
? ? ?* key name? 都是han1
? ? ?* 可以用 (k1,k2)->k1 來設置,如果有重復的key,則保留key1,舍棄key2
? ? ?*/
//? ? ? ? Arrays.stream(wechatPrintInfos.toArray()).forEach(e -> {
//? ? ? ? ? ? OrderNolist.add(e.);
//? ? ? ? });
//? ? ? ? Map<Long, OrderAddress> addressListMap = addressList.stream().collect(Collectors.toMap(OrderAddress::getOrderNo, p -> p, (key1, key2) -> key1));
//? ? ? ? Map<Integer, String> dictMap = Arrays.stream(addressList).filter(k -> null != k.getOrderNo()).collect(Collectors.toMap(e -> e.getDictKey().intValue(), DictionaryValueVo::getDictValue));
//? ? ? ? orderAcceptList.stream().forEach(row->{
//? ? ? ? ? ? orderAcceptService.updateByPrimaryKey(row);
//? ? ? ? });
?
//? ? ? ? List<Long> OrderNolist = new ArrayList<Long>();
//? ? ? ? printService.queryPrintInfo(customerCode).stream().forEach(p ->
//? ? ? ? ? ? OrderNolist.add(p.getOrderNo())
//? ? ? ? );
?
? ? System.out.println("userList="+JSONObject.toJSONString(userList));
? ? Map<String,Object> compMap= userList.stream().collect(Collectors.toMap(User::getUserName, a -> a.getAge() + "===" + a.getSex(), (k1, k2) -> k1));
? ? for(Object obj : compMap.keySet()) {
? ? ? ? String key = (String) obj;//取到每一個key值
? ? ? ? String value = (String) compMap.get(key);
? ? ? ? System.out.println(key + "=" + value);
? ? }
?
? ? /**
? ? ?* list中以某個屬性分組,比如用name分組
? ? ?*/
? ? Map<String,List<User>> map= userList.stream().collect(Collectors.groupingBy(User::getUserName));
? ? System.out.println(map);
?
}
?
下面是看的別人的Demo?
? ? package com.mc.day1.lambda;
? ??
? ? import org.junit.Before;
? ? import org.junit.Test;
? ??
? ? import java.util.ArrayList;
? ? import java.util.Arrays;
? ? import java.util.List;
? ? import java.util.Optional;
? ? import java.util.stream.Stream;
? ??
? ? /**
? ? ?* =================規約與收集==================練習
? ? ?*/
? ? public class Lambda4 {
? ? ? ??
? ? ? ? List<Transaction> transactions = null;
? ? ? ??
? ? ? ? @Before
? ? ? ? public void before(){
? ? ? ? ? ? Trader raoul = new Trader("Raoul", "Cambridge");
? ? ? ? ? ? Trader mario = new Trader("Mario", "Milan");
? ? ? ? ? ? Trader alan = new Trader("Alan", "Cambridge");
? ? ? ? ? ? Trader brian = new Trader("Brian", "Cambridge");
? ? ? ? ? ??
? ? ? ? ? ? transactions = Arrays.asList(
? ? ? ? ? ? ? ? ? ? new Transaction(brian, 2011, 300),
? ? ? ? ? ? ? ? ? ? new Transaction(raoul, 2012, 1000),
? ? ? ? ? ? ? ? ? ? new Transaction(raoul, 2011, 400),
? ? ? ? ? ? ? ? ? ? new Transaction(mario, 2012, 710),
? ? ? ? ? ? ? ? ? ? new Transaction(mario, 2012, 700),
? ? ? ? ? ? ? ? ? ? new Transaction(alan, 2012, 950)
? ? ? ? ? ? );
? ? ? ? }
? ? ? ??
? ? ? ? //1. 找出2011年發生的所有交易, 并按交易額排序(從低到高)
? ? ? ? @Test
? ? ? ? public void test1(){
? ? ? ? ? ? transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .filter((t) -> t.getYear() == 2011)
? ? ? ? ? ? ? ? ? ? ? ? .sorted((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()))
? ? ? ? ? ? ? ? ? ? ? ? .forEach(System.out::println);
? ? ? ? }
? ? ? ??
? ? ? ? //2. 交易員都在哪些不同的城市工作過?
? ? ? ? @Test
? ? ? ? public void test2(){
? ? ? ? ? ? transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .map((t) -> t.getTrader().getCity())
? ? ? ? ? ? ? ? ? ? ? ? .distinct()
? ? ? ? ? ? ? ? ? ? ? ? .forEach(System.out::println);
? ? ? ? }
? ? ? ??
? ? ? ? //3. 查找所有來自劍橋的交易員,并按姓名排序
? ? ? ? @Test
? ? ? ? public void test3(){
? ? ? ? ? ? transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .filter((t) -> t.getTrader().getCity().equals("Cambridge"))
? ? ? ? ? ? ? ? ? ? ? ? .map(Transaction::getTrader)
? ? ? ? ? ? ? ? ? ? ? ? .sorted((t1, t2) -> t1.getName().compareTo(t2.getName()))
? ? ? ? ? ? ? ? ? ? ? ? .distinct()
? ? ? ? ? ? ? ? ? ? ? ? .forEach(System.out::println);
? ? ? ? }
? ? ? ??
? ? ? ? //4. 返回所有交易員的姓名字符串,按字母順序排序
? ? ? ? @Test
? ? ? ? public void test4(){
? ? ? ? ? ? transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .map((t) -> t.getTrader().getName())
? ? ? ? ? ? ? ? ? ? ? ? .sorted()
? ? ? ? ? ? ? ? ? ? ? ? .forEach(System.out::println);
? ? ? ? ? ??
? ? ? ? ? ? System.out.println("-----------------------------------");
? ? ? ? ? ??
? ? ? ? ? ? String str = transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .map((t) -> t.getTrader().getName())
? ? ? ? ? ? ? ? ? ? ? ? .sorted()
? ? ? ? ? ? ? ? ? ? ? ? .reduce("", String::concat);
? ? ? ? ? ??
? ? ? ? ? ? System.out.println(str);
? ? ? ? ? ??
? ? ? ? ? ? System.out.println("------------------------------------");
? ? ? ? ? ??
? ? ? ? ? ? transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .map((t) -> t.getTrader().getName())
? ? ? ? ? ? ? ? ? ? ? ? .flatMap(Lambda4::filterCharacter)
? ? ? ? ? ? ? ? ? ? ? ? .sorted((s1, s2) -> s1.compareToIgnoreCase(s2))
? ? ? ? ? ? ? ? ? ? ? ? .forEach(System.out::print);
? ? ? ? }
? ? ? ??
? ? ? ? public static Stream<String> filterCharacter(String str){
? ? ? ? ? ? List<String> list = new ArrayList<>();
? ? ? ? ? ??
? ? ? ? ? ? for (Character ch : str.toCharArray()) {
? ? ? ? ? ? ? ? list.add(ch.toString());
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? return list.stream();
? ? ? ? }
? ? ? ??
? ? ? ? //5. 有沒有交易員是在米蘭工作的?
? ? ? ? @Test
? ? ? ? public void test5(){
? ? ? ? ? ? boolean bl = transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .anyMatch((t) -> t.getTrader().getCity().equals("Milan"));
? ? ? ? ? ??
? ? ? ? ? ? System.out.println(bl);
? ? ? ? }
? ? ? ??
? ? ? ??
? ? ? ? //6. 打印生活在劍橋的交易員的所有交易額
? ? ? ? @Test
? ? ? ? public void test6(){
? ? ? ? ? ? Optional<Integer> sum = transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .filter((e) -> e.getTrader().getCity().equals("Cambridge"))
? ? ? ? ? ? ? ? ? ? ? ? .map(Transaction::getValue)
? ? ? ? ? ? ? ? ? ? ? ? .reduce(Integer::sum);
? ? ? ? ? ??
? ? ? ? ? ? System.out.println(sum.get());
? ? ? ? }
? ? ? ??
? ? ? ??
? ? ? ? //7. 所有交易中,最高的交易額是多少
? ? ? ? @Test
? ? ? ? public void test7(){
? ? ? ? ? ? Optional<Integer> max = transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .map((t) -> t.getValue())
? ? ? ? ? ? ? ? ? ? ? ? .max(Integer::compare);
? ? ? ? ? ??
? ? ? ? ? ? System.out.println(max.get());
? ? ? ? }
? ? ? ??
? ? ? ? //8. 找到交易額最小的交易
? ? ? ? @Test
? ? ? ? public void test8(){
? ? ? ? ? ? Optional<Transaction> op = transactions.stream()
? ? ? ? ? ? ? ? ? ? ? ? .min((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()));
? ? ? ? ? ??
? ? ? ? ? ? System.out.println(op.get());
? ? ? ? }
? ??
? ? }