Java 8 : Stream API 練習
阿新 • • 發佈:2017-08-25
rri setname 增強 內部 new collector names pri ons
//店鋪屬性類 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; } @Override public 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 練習