網(wǎng)站ftp查詢國際網(wǎng)絡(luò)銷售平臺有哪些
一.?Stream
? ? ? ? 1.?Stream也叫Stream流,是jdk8開始新增的一套API(java.util.stream.*),可以用于操作集合或者數(shù)組的數(shù)據(jù)。
? ? ? ? 2. 優(yōu)勢:Stream流大量的結(jié)合了lambda的語言風格來編程,提供了一種更加強大,更加簡潔的方式操作集合或者數(shù)組中的數(shù)據(jù)。
? ? ? ? 3. 使用步驟:
? ? ? ? ? ? ? ? ① 獲取Stream流
? ? ? ? ? ? ? ? ② Stream流常用的中間方法(支持鏈式調(diào)用)
? ? ? ? ? ? ? ? ③ Stream 流常見的終結(jié)方法
public static void main(String[] args) {//StreamList<String> list = new ArrayList<String>();Collections.addAll(list, "卡莎", "卡車", "泰坦", "璐璐", "卡拉", "卡卡卡", "伊澤");System.out.println(list);//[卡莎, 卡車, 泰坦, 璐璐, 卡拉, 卡卡卡, 伊澤]//list中方法 篩選數(shù)據(jù)List<String> list1 = new ArrayList<>();for(String str : list){if (str.contains("卡") && str.length() == 2){list1.add(str);}}System.out.println(list1);//[卡莎, 卡車, 卡拉]//使用stream流 篩選List<String> list2 = list.stream().filter(s -> s.contains("卡")).filter(s -> s.length() == 2).collect(Collectors.toList());System.out.println(list2);//v}
二.?Stream的常用方法
? ? ? ? 1.? 獲取Stream流
方法 | 說明 | |
Collection提供的獲取Stream流 | default Stream<E> stream() | 獲取當前集合的Stream流 |
Arrays類提供的獲取Stream流 | public static <T> Stream<T> stream(T[] array) | 獲取當前數(shù)組的Stream流 |
Stream提供的獲取Stream流 | public static<T> Stream<T> of(T... values) | 獲取當前接收數(shù)據(jù)的Stream流 |
public static void main(String[] args) {//1. 獲取List集合的Stream流List<String> list = new ArrayList<String>();Collections.addAll(list, "卡莎", "卡車", "泰坦", "璐璐", "卡拉", "卡卡卡", "伊澤");//獲取Stream流Stream<String> stream = list.stream();//2. 獲取Set集合的Stream流Set<String> set = new HashSet<String>();Collections.addAll(set, "大宇", "朵朵", "歡歡", "麥琪");//獲取Stream流Stream<String> stream1 = set.stream();stream1.filter(s -> s.contains("歡")).forEach(System.out::println);//3. 獲取Map集合的Stream流Map<String, String> map = new HashMap<>();map.put("楊過", "小龍女");map.put("張無忌", "趙敏");map.put("郭靖", "黃蓉");map.put("令狐沖", "東方不敗");// 獲取Stream流 分開處理Set set2 = map.entrySet();Stream<String> keys = set2.stream();Collection<String> values = map.values();Stream<String> vas = values.stream();//統(tǒng)一處理Set<Map.Entry<String, String>> entry = map.entrySet();Stream<Map.Entry<String, String>> kvs = entry.stream();kvs.filter(k -> k.getKey().contains("張")).forEach(System.out::println);//4. 獲取數(shù)組的Stream流String[] str = {"路馬", "天天", "萊德", "落落"};//public static <T> Stream<T> stream(T[] array)Stream<String> stream2 = Arrays.stream(str);//public static<T> Stream<T> of(T... values)Stream<String> stream3 = Stream.of(str);
}
? ? ? ? 2. Stream流常見的中間方法
? ? ? ? ? ? ? ? (1) 中間方法是指調(diào)用完成后返回新的Stream流,可以繼續(xù)使用(支持鏈式編程)
常用方法 | 說明 |
Stream<T> filter(Predicate<? super T> predicate) | 用于對流中的數(shù)據(jù)進行過濾 |
Stream<T> sorted() | 對元素進行升序排序 |
Stream<T> sorted(Comparator<? super T> comparator) | 對元素按照指定規(guī)則排序 |
Stream<T> limit(long maxSize) | 獲取前幾個元素 |
Stream<T> skip(long n) | 跳過前幾個元素 |
Stream<T> distinct() | 去除流中重復(fù)的元素 |
<R> Stream<R> map(Function<? super T, ? extends R> mapper) | 對元素進行加工,并返回對應(yīng)的新流 |
static <T> Stream<T> concat(Stream a, Stream b) | 合并a和b兩個流 |
public class Student{private String name;private int age;private double score;public Student() {}public Student(String name, int age, double score) {this.name = name;this.age = age;this.score = score;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", score=" + score +'}';}@Overridepublic boolean equals(Object o) {if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Double.compare(score, student.score) == 0 && Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age, score);}
}
public static void main(String[] args) {List<Double> scores = new ArrayList<Double>();Collections.addAll(scores, 99.0, 96.0, 94.0,59.0, 66.0, 74.0);//成績大于等于60的并排序scores.stream().filter(s -> s >= 60.0).sorted().forEach(System.out::println);//66.0 74.0 94.0 96.0 99.0System.out.println("--------------------------------------------");List<Student> students = new ArrayList<>();Student s1 = new Student("卡莎", 18, 99.0);Student s2 = new Student("泰坦", 19, 93.0);Student s3 = new Student("伊澤", 16, 98.0);Student s4 = new Student("璐璐", 14, 96.0);Student s5 = new Student("璐璐", 14, 96.0);Collections.addAll(students, s1, s2, s3, s4, s5);//找出年齡大于等于16 且小于等于20 按照年齡降序//filter() sorted()students.stream().filter(s -> s.getAge() >= 16 && s.getAge() <= 20).sorted(((o1, o2) -> o2.getAge() - o1.getAge())).forEach(System.out::println);System.out.println("--------------------------------------------");//找出分數(shù)最高的前三名// sorted() limit()students.stream().sorted((o1, o2) -> Double.compare(o2.getScore(), o1.getScore())).limit(3).forEach(System.out::println);System.out.println("--------------------------------------------");//找出分數(shù)最低的 倒數(shù)3名//sorted() skip()students.stream().sorted((o1, o2) -> Double.compare(o2.getScore(), o1.getScore())).skip(students.size() - 3).forEach(System.out::println);System.out.println("--------------------------------------------");//成績大于等于95的 去除重復(fù)的名字// distinct() 自定義類型 如果希望內(nèi)容一樣認為重復(fù) 需重寫 equals()和 hashCode()//filter() map() distinct()students.stream().filter(s -> s.getScore() >= 95).map(s -> s.getName()).distinct().forEach(System.out::println);students.stream().filter(s -> s.getScore() >= 95).distinct().forEach(System.out::println);//static <T> Stream<T> concat(Stream a, Stream b) 合并a和b兩個流Stream<String> stream = Stream.of("1", "2", "3");Stream<String> stream2 = Stream.of("4", "5", "6", "7", "8", "9");Stream<String> stream3 = Stream.concat(stream, stream2);stream3.forEach(System.out::println);}
? ? ? ? 3. Stream流常見的終結(jié)方法
? ? ? ? ? ? ? ? (1)?終結(jié)方法指的是調(diào)用完成后,不會再返回新的Stream流了,不能再繼續(xù)使用Stream流了。
方法名稱 | 說明 |
void forEach(Consumer action) | 對此流運算后的元素進行遍歷 |
long count() | 統(tǒng)計此流運算后的元素個數(shù) |
Optional<T> max(Comparator<? super T> copmarator) | 獲取此流運算后的最大值元素 |
Optional<T> min(Comparator<? super T> copmarator) | 獲取此流運算后的最小值元素 |
方法名稱 | 說明 |
R collect(Collector collector) | 把流處理后的結(jié)果放到一個指定的集合中 |
Object[] toArray() | 把流處理后的結(jié)果放到一個指定的數(shù)組中 |
public static void main(String[] args) {List<Student> students = new ArrayList<>();Student s1 = new Student("卡莎", 18, 99.0);Student s2 = new Student("泰坦", 19, 93.0);Student s3 = new Student("伊澤", 16, 98.0);Student s4 = new Student("璐璐", 14, 96.0);Student s5 = new Student("璐璐", 14, 96.0);Collections.addAll(students, s1, s2, s3, s4, s5);// 計算分數(shù)超過95的有幾個 .count()long l = students.stream().filter(s -> s.getScore() > 95).count();System.out.println(l);//4//找出分數(shù)最高的 并輸出 .max()Student smax = students.stream().max((o1, o2) -> Double.compare(o1.getScore(), o2.getScore())).get();System.out.println(smax);//找出分數(shù)最低的 并輸出 .min()Student smin = students.stream().min((o1, o2) -> Double.compare(o1.getScore(), o2.getScore())).get();System.out.println(smin);//計算分數(shù)超過95的 并放到一個新集合中//流只能收集一次List<Student> list1 = students.stream().filter(s -> s.getScore() > 95).collect(Collectors.toList());System.out.println(list1);Set<Student> list2 = students.stream().filter(s -> s.getScore() > 95).collect(Collectors.toSet());System.out.println(list2);//找出分數(shù)超過95的 并把名字和分數(shù)放到一個map集合中//不會自動去重 需調(diào)用distinct()去重Map<String, Double> map = students.stream().filter(s -> s.getScore() > 95).distinct().collect(Collectors.toMap(m -> m.getName(), m -> m.getScore()));System.out.println(map);//找出分數(shù)超過95的 并把名字和分數(shù)放到一個數(shù)組Object[] arr = students.stream().filter(s -> s.getScore() > 95).toArray();Student[] arr1 = students.stream().filter(s -> s.getScore() > 95).toArray(len -> new Student[len]);System.out.println(Arrays.toString(arr));System.out.println(Arrays.toString(arr1));
}