欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Java8中StreamAPI如何使用

本篇文章為大家展示了Java8中Stream API如何使用,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司專(zhuān)注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、薌城網(wǎng)絡(luò)推廣、微信小程序、薌城網(wǎng)絡(luò)營(yíng)銷(xiāo)、薌城企業(yè)策劃、薌城品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供薌城建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:aaarwkj.com

首先創(chuàng)建一個(gè)對(duì)象

public class Employee {

  private int id;
  private String name;
  private int age;
  private double salary;
  private Status status;

  public enum Status {
    FREE, BUSY, VOCATION;
  }

  public Employee() {
  }

  public Employee(String name) {
    this.name = name;
  }

  public Employee(String name, int age) {
    this.name = name;
    this.age = age;
  }
  
  public Employee(int id, String name, int age, double salary) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.salary = salary;
  }

  public Employee(int id, String name, int age, double salary, Status status) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.salary = salary;
    this.status = status;
  }
  //省略get,set等。。。
}

隨便初始化一些數(shù)據(jù)

List<Employee> empList = Arrays.asList(new Employee(102, "李四", 59, 6666.66, Status.BUSY),
      new Employee(101, "張三", 18, 9999.99, Status.FREE), new Employee(103, "王五", 28, 3333.33, Status.VOCATION),
      new Employee(104, "趙六", 8, 7777.77, Status.BUSY), new Employee(104, "趙六", 8, 7777.77, Status.FREE),
      new Employee(104, "趙六", 8, 7777.77, Status.FREE), new Employee(105, "田七", 38, 5555.55, Status.BUSY));

中間操作

根據(jù)條件篩選 filter

/**
* 接收Lambda, 從流中排除某些元素。
*/

@Test
void testFilter() {
  empList.stream().filter((e) -> {
    return e.getSalary() >= 5000;
  }).forEach(System.out::println);
}

跳過(guò)流的前n個(gè)元素 skip

/**
* 跳過(guò)元素,返回一個(gè)扔掉了前n個(gè)元素的流。
*/
@Test
void testSkip() {
  empList.stream().filter((e) -> e.getSalary() >= 5000).skip(2).forEach(System.out::println);
}

去除重復(fù)元素 distinct

/**
* 篩選,通過(guò)流所生成元素的 hashCode() 和 equals() 去除重復(fù)元素
*/
@Test
void testDistinct() {
  empList.stream().distinct().forEach(System.out::println);
}

截取流的前n個(gè)元素 limit

/**
* 截?cái)嗔?,使其元素不超過(guò)給定數(shù)量。
*/
@Test
void testLimit() {
  empList.stream().filter((e) -> {
    return e.getSalary() >= 5000;
  }).limit(3).forEach(System.out::println);
}

映射 map

/**
* 接收一個(gè)函數(shù)作為參數(shù),該函數(shù)會(huì)被應(yīng)用到每個(gè)元素上,并將其映射成一個(gè)新的元素
*/
@Test
void testMap() {
  empList.stream().map(e -> e.getName()).forEach(System.out::println);

  empList.stream().map(e -> {
    empList.forEach(i -> {
      i.setName(i.getName() + "111");
    });
    return e;
  }).collect(Collectors.toList());
}

自然排序 sorted

/**
* 產(chǎn)生一個(gè)新流,其中按自然順序排序
*/
@Test
void testSorted() {
  empList.stream().map(Employee::getName).sorted().forEach(System.out::println);
}

自定義排序 sorted(Comparator comp)

/**
* 產(chǎn)生一個(gè)新流,其中按自然順序排序
*/
@Test
void testSortedComparator() {
  empList.stream().sorted((x, y) -> {
   if (x.getAge() == y.getAge()) {
     return x.getName().compareTo(y.getName());
    } else {
     return Integer.compare(x.getAge(), y.getAge());
   }
  }).forEach(System.out::println);
}


最終操作

是否匹配任一元素 anyMatch

/**
 * 檢查是否至少匹配一個(gè)元素
 */
@Test
void testAnyMatch() {
  boolean b = empList.stream().anyMatch((e) -> e.getStatus().equals(Status.BUSY));
  System.out.println("boolean is : " + b);
}

是否匹配所有元素 allMatch

/**
 * 檢查是否匹配所有元素
 */
@Test
void testAllMatch() {
  boolean b = empList.stream().allMatch((e) -> e.getStatus().equals(Status.BUSY));
  System.out.println("boolean is : " + b);
}

是否未匹配所有元素 noneMatch

/**
 * 檢查是否沒(méi)有匹配的元素
 */
@Test
void testNoneMatch() {
  boolean b = empList.stream().noneMatch((e) -> e.getStatus().equals(Status.BUSY));
  System.out.println("boolean is : " + b);
}

返回第一個(gè)元素 findFirst

/**
 * 返回第一個(gè)元素
 */
@Test
void testFindFirst() {
  Optional<Employee> op = empList.stream().sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
      .findFirst();
  if (op.isPresent()) {
    System.out.println("first employee name is : " + op.get().getName().toString());
  }
}

返回流中任意元素 findAny

/**
 * 返回當(dāng)前流中的任意元素
 */
@Test
void testFindAny() {
  Optional<Employee> op = empList.stream().sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
      .findAny();
  if (op.isPresent()) {
    System.out.println("any employee name is : " + op.get().getName().toString());
  }
}

返回流的總數(shù) count

/**
 * 返回流中元素的總個(gè)數(shù)
 */
@Test
void testCount() {
  long count = empList.stream().filter((e) -> e.getStatus().equals(Status.FREE)).count();
  System.out.println("Count is : " + count);
}

返回流中的最大值 max

/**
 * 返回流中最大值
 */
@Test
void testMax() {
  Optional<Double> op = empList.stream().map(Employee::getSalary).max(Double::compare);
  System.out.println(op.get());
}

返回流中的最小值 min

/**
 * 返回流中最小值
 */
@Test
void testMin() {
  Optional<Employee> op2 = empList.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
  System.out.println(op2.get());
}

歸約 reduce
歸約是將集合中的所有元素經(jīng)過(guò)指定運(yùn)算,折疊成一個(gè)元素輸出

/**
 * 可以將流中元素反復(fù)結(jié)合起來(lái),得到一個(gè)值。返回T
 */
@Test
void testReduce() {
  Optional<Double> op = empList.stream().map(Employee::getSalary).reduce(Double::sum);

  System.out.println(op.get());
}

/**
 * 可以將流中元素反復(fù)結(jié)合起來(lái),得到一個(gè)值,返回Optional< T>
 */
@Test
void testReduce1() {
  Optional<Integer> sum = empList.stream().map(Employee::getName).flatMap(Java8Stream::filterCharacter)
      .map((ch) -> {
        if (ch.equals('六'))
          return 1;
        else
          return 0;
      }).reduce(Integer::sum);
  System.out.println(sum.get());
}

將元素收集到 list 里 Collectors.toList()

/**
 * 把流中的元素收集到list里。
 */
@Test
void testCollectorsToList() {
  List<String> list = empList.stream().map(Employee::getName).collect(Collectors.toList());
  list.forEach(System.out::println);
}

將元素收集到 set 里 Collectors.toSet()

/**
 * 把流中的元素收集到set里。
 */
@Test
void testCollectorsToSet() {
  Set<String> list = empList.stream().map(Employee::getName).collect(Collectors.toSet());
  list.forEach(System.out::println);
}

把流中的元素收集到新創(chuàng)建的集合里 Collectors.toCollection(HashSet::new)

/**
 * 把流中的元素收集到新創(chuàng)建的集合里。
 */
@Test
void testCollectorsToCollection() {
  HashSet<String> hs = empList.stream().map(Employee::getName).collect(Collectors.toCollection(HashSet::new));
  hs.forEach(System.out::println);
}

根據(jù)比較器選擇最大值 Collectors.maxBy()

/**
 * 根據(jù)比較器選擇最大值。
 */
@Test
void testCollectorsMaxBy() {
  Optional<Double> max = empList.stream().map(Employee::getSalary).collect(Collectors.maxBy(Double::compare));
  System.out.println(max.get());
}

根據(jù)比較器選擇最小值 Collectors.minBy()

/**
 * 根據(jù)比較器選擇最小值。
 */
@Test
void testCollectorsMinBy() {
  Optional<Double> max = empList.stream().map(Employee::getSalary).collect(Collectors.minBy(Double::compare));
  System.out.println(max.get());
}

對(duì)流中元素的某個(gè)字段求和 Collectors.summingDouble()

/**
 * 對(duì)流中元素的整數(shù)屬性求和。
 */
@Test
void testCollectorsSummingDouble() {
  Double sum = empList.stream().collect(Collectors.summingDouble(Employee::getSalary));
  System.out.println(sum);
}

對(duì)流中元素的某個(gè)字段求平均值 Collectors.averagingDouble()

/**
 * 計(jì)算流中元素Integer屬性的平均值。
 */
@Test
void testCollectorsAveragingDouble() {
  Double avg = empList.stream().collect(Collectors.averagingDouble(Employee::getSalary));
  System.out.println(avg);
}

分組,類(lèi)似sql的 group by Collectors.groupingBy

/**
 * 分組
 */
@Test
void testCollectorsGroupingBy() {
  Map<Status, List<Employee>> map = empList.stream().collect(Collectors.groupingBy(Employee::getStatus));

  System.out.println(map);
}

多級(jí)分組

/**
 * 多級(jí)分組
 */
@Test
void testCollectorsGroupingBy1() {
  Map<Status, Map<String, List<Employee>>> map = empList.stream()
      .collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> {
        if (e.getAge() >= 60)
          return "老年";
        else if (e.getAge() >= 35)
          return "中年";
        else
          return "成年";
      })));
  System.out.println(map);
}

字符串拼接 Collectors.joining()

/**
 * 字符串拼接
 */
@Test
void testCollectorsJoining() {
  String str = empList.stream().map(Employee::getName).collect(Collectors.joining(",", "----", "----"));
  System.out.println(str);
}

public static Stream<Character> filterCharacter(String str) {

  List<Character> list = new ArrayList<>();
  for (Character ch : str.toCharArray()) {
    list.add(ch);
  }
  return list.stream();
}

上述內(nèi)容就是Java8中Stream API如何使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:Java8中StreamAPI如何使用
文章起源:http://aaarwkj.com/article8/gjdsip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、搜索引擎優(yōu)化、面包屑導(dǎo)航、定制網(wǎng)站小程序開(kāi)發(fā)、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
av小说亚洲激情乱| 亚洲国产欧美日韩在线| 免费直接在线看亚洲黄色| 欧美午夜精品一二三区| 能在线播放的国产三级| 97视频精品免费观看| 成熟人妻一区二区三区人妻| 亚洲精品黄色在线观看| 日本人妻三级精品久久| 亚洲经典日韩欧美一区| 日韩一区二区三区成人| 色琪琪原网另类欧美日韩| 日韩av一区二区三区在线| 国产精品一区二区av麻豆| 黄色污网站在线观看免费| 亚洲精品中文字幕乱码| 精品人妻av中文字幕| 粉嫩护士国产在线观看 | 熟女人妻精品一二三四| 国产一区免费二区三区四区| 国产国产成人精品久久| 日本大片一区二区免费看| 亚洲精品自拍一二三四区| 人妻中文字幕在线看粉嫩| 国产综合欧美日韩在线91| 日本亚洲一区二区在线| 激情综合婷婷中文字幕| 国产日韩精品一区二区三区在线| 日本亚洲精品在线观看| 亚洲不卡在线免费av| 少妇一区二区三区免费| 快播av手机在线播放| 欧美熟女av在线观看| 日本欧美高清一区二区| 欧美 国产 综合 日韩| 国产成人精品高清国产三级| 欧美一区二区三区十区| 中文字幕日韩一区二区| 最新91熟女九色地址| 一区二区三区三级视频| 国产福利在线观看网站|