Java 8 : Stream API 練習

來源:互聯網
上載者:User

標籤:log   cto   ati   property   class   test   屬性   setname   first   

//店鋪屬性類public class Property {String  name;    // 距離,單位:米    Integer distance;    // 銷量,月售    Integer sales;    // 價格,這裡簡單起見就寫一個層級代表價格區間    Integer priceLevel;    public Property(String name, int distance, int sales, int priceLevel) {        this.name = name;        this.distance = distance;        this.sales = sales;        this.priceLevel = priceLevel;    }public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getDistance() {return distance;}public void setDistance(Integer distance) {this.distance = distance;}public Integer getSales() {return sales;}public void setSales(Integer sales) {this.sales = sales;}public Integer getPriceLevel() {return priceLevel;}public void setPriceLevel(Integer priceLevel) {this.priceLevel = priceLevel;}@Overridepublic String toString() {return "Property [name=" + name + ", distance=" + distance + ", sales=" + sales + ", priceLevel=" + priceLevel+ "]";}    }

  

//JAVA8Stream它提供了對集合操作的增強  與I/O不同public class TestJAVA8Stream {public static void main(String[] args) {/*// Stream操作 List排序System.out.println("=====JAVA8Stream操作============方法1找到距離我最近的店鋪");Property p1 = new Property("木桶飯", 1000, 500, 2);    Property p2 = new Property("黃燜雞", 2300, 1500, 3);    Property p3 = new Property("麥當勞", 580, 3000, 1);    Property p4 = new Property("肯德基", 6000, 200, 4);    List<Property> properties = Arrays.asList(p1, p2, p3, p4);String name2 = properties.stream()                .sorted(Comparator.comparingInt(x -> x.distance))                .findFirst()                .get().name;System.out.println("距離我最近的店鋪是:" + name2);*//*System.out.println("======傳統操作List排序===========方法二找到距離我最近的店鋪");Property p1 = new Property("木桶飯", 1000, 500, 2);    Property p2 = new Property("黃燜雞", 2300, 1500, 3);    Property p3 = new Property("麥當勞", 580, 3000, 1);    Property p4 = new Property("肯德基", 6000, 200, 4);    List<Property> properties = Arrays.asList(p1, p2, p3, p4);    Collections.sort(properties, (x, y) -> x.distance.compareTo(y.distance));    String name = properties.get(0).name;    System.out.println("距離我最近的店鋪是:" + name);*/System.out.println("=====迭代=======");Property p1 = new Property("木桶飯", 1000, 500, 2);    Property p2 = new Property("黃燜雞", 2300, 1500, 3);    Property p3 = new Property("woi麥當勞", 580, 3000, 1);    Property p4 = new Property("肯德基", 6000, 200, 4);    List<Property> properties = Arrays.asList(p1, p2, p3, p4);int count = 0;//月銷量大於1000的店鋪個數for (Property property : properties) {    if(property.sales > 1000){        count++;    }}System.out.println("月銷量大於1000的店鋪個數:"+count);//使用內部迭代進行計算long count2 = properties.stream().filter(p -> p.sales > 1000).count();System.out.println("月銷量大於1000的店鋪個數:"+count2);long count3 =properties.stream().filter(p -> p.distance < 1000).count();//篩選出距離我在1000米內的店鋪long count4 =properties.stream().filter(p -> p.name.length() > 5).count();//篩選出名稱大於5個字的店鋪Property property = properties.stream().max(Comparator.comparingInt(p -> p.priceLevel)).get();//篩選出價格最低的店鋪//擷取距離我最近的2個店鋪List<Property> properties_s = properties.stream()            .sorted(Comparator.comparingInt(x -> x.distance))            .limit(2)            .collect(Collectors.toList());//擷取所有店鋪的名稱List<String> names = properties.stream()            .map(p -> p.name)            .collect(Collectors.toList());//擷取每個店鋪的價格等級Map<String, Integer> map = properties.stream()        .collect(Collectors.toMap(Property::getName, Property::getPriceLevel));//所有價格等級的店鋪列表Map<Integer, List<Property>> priceMap = properties.stream()            .collect(Collectors.groupingBy(Property::getPriceLevel));}}

  

Java 8 : Stream API 練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.