此合集仅仅当作字典查阅,字数很少,内容很多
为了能够读懂公司里的代码,为了能够早日进大厂,为了能够提高编程水平成为一流的程序员,为了能够实现梦想,函数式编程的学习必不可缺!!!
目录
函数式编程:之后简称FP
FP优点
1.在处理大数据量式效率更高,但小数据量并不是
2.并行,并发编程,线性安全,原子性
3.代码可读性更高
4.消灭嵌套地狱
Lambda表达式
Lambda表达式的直观感受
new Thread(new Runnable() { @Override public void run() { System.out.println("我是一个线程"); } }).start(); //TODO 优化代码过后 new Thread(()->{ System.out.println("我是优化后的线程"); }).start();Lambda表达式结构
( XXX -> { XXX })Lambda表达式省略规则
1.参数类型可以省略
2.方法体只有一句代码时大括号、return和唯一一句代码的分号可以省略
3.方法只有一个参数时小括号可以省略
4.以上规则用中记,记中用
创建流
单列集合创建流(Collection集合均为单列集合)
ArrayList
ArrayList<Integer> integerArrayList = new ArrayList<>(); integerArrayList.add(1); integerArrayList.stream().forEach(System.out::println);
LinkedListLinkedList<Integer> linkedList = new LinkedList<>(); linkedList.add(1); linkedList.stream().forEach(System.out::println);
VectorVector<Integer> vector = new Vector<>(); vector.add(1); vector.stream().forEach(System.out::println);HashSet
HashSet<Integer> hashSet = new HashSet<>(); hashSet.add(1); hashSet.stream().forEach(System.out::println);
双列集合创建流(Map均为双列集合)
HashMapHashMap<Object, Object> objectObjectHashMap = new HashMap<>(); objectObjectHashMap.entrySet() .stream() .forEach((entry) -> {});TreeMap
TreeMap<Object, Object> objectObjectTreeMap = new TreeMap<>(); objectObjectTreeMap.entrySet() .stream() .forEach((entry) -> {});LinkedHashMap
LinkedHashMap<Object, Object> linkedHashMap = new LinkedHashMap<>(); linkedHashMap.entrySet() .stream() .forEach((entry) -> {});
数组创建流两种方式
int[] arr = {1,2,3,4,5}; IntStream stream = Arrays.stream(arr); Stream<int[]> arr1 = Stream.of(arr);
T算子
map
一对一输出
List<Author> sampleAuthors = AuthorFactory.createSampleAuthors(); sampleAuthors.stream() .map(author -> author) .forEach(System.out::println);
distinct
消除重复的流数据,底层是Object的equals方法(重写hashCode和equals)
重写部分代码
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Author)) return false; Author author = (Author) o; return age == author.age && name.equals(author.name); } @Override public int hashCode() { return Objects.hash(age, name); }业务代码
List<Author> sampleAuthors = AuthorFactory.createSampleAuthors(); sampleAuthors.stream() .map(author -> author) .distinct() .forEach(System.out::println);
sorted
作用:调整顺序大小,按照你设定的规则按照从“大(小)”到“小(大)”输出
不带参
需要实现Comparator接口并重写compareTo方法,排名先后顺序自己试List<Author> sampleAuthors = AuthorFactory.createSampleAuthors(); sampleAuthors.stream() .sorted() .forEach(System.out::println);带参
new一个匿名内部类重写方法或者用Lambda表达式
sampleAuthors.stream() .sorted(new Comparator<Author>() { @Override public int compare(Author o1, Author o2) { return 0; } }) .forEach(System.out::println);sampleAuthors.stream() .sorted((o1, o2) -> 0) .forEach(System.out::println);
flatmap
作用:能够实现一对多,能够将一条流分散成多条流
作用在spark合集中已经提到过,这里自己写了一个作者类,然后执行flatmap转换算子以实现"一个流指向多个流的操作"
分流效果图

main函数部分
//创建书籍列表以及各个作者
List<String> list = Arrays.asList("Java", "Python", "C++", "JavaScript", "C#", "PHP");
ArrayList booklist1 = new ArrayList<>(list);
ArrayList booklist2 = new ArrayList<>(list);
ArrayList booklist3 = new ArrayList<>(list);
ArrayList booklist4 = new ArrayList<>(list);
Author author1 = new Author(10, "张三", new Author.BookList(booklist1));
Author author2 = new Author(10, "李四", new Author.BookList(booklist2));
Author author3 = new Author(10, "王五", new Author.BookList(booklist3));
Author author4 = new Author(10, "麻六", new Author.BookList(booklist4));
//创建流
List<Author> authorList = Arrays.asList(author1, author2, author3, author4);
authorList.stream()
.flatMap(Author::getBooks)
.forEach(System.out::println);
Author类部分
class Author implements Comparable<Author> {
private int age;
private String name;
private BookList booklist;
public Author(int age, String name, BookList booklist) {
this.age = age;
this.name = name;
this.booklist = booklist;
}
static class BookList {
private List<String> name;
public BookList(List<String> name) {
this.name = name;
}
public List<String> getName() {
return name;
}
}
public Stream<String> getBooks() {
return booklist.getName().stream();
}
@Override
public String toString() {
return "Author{" +
"age=" + age +
", name='" + name + '\'' + ", booklist=" + booklist +
'}';
}
@Override
public int compareTo(Author o) {
return o.age - this.age;//除了考试和面试会提到之外,记忆这个东西没有意义
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (!(obj instanceof Author)) return false;
Author a = (Author) obj;
return a.age == this.age && a.name.equals(this.name);
}
@Override
public int hashCode() {
return Objects.hash(age, name);
}
}
A算子
foreach
作用:输出、输出、还是TM的输出!!!
其中传入Consumer消费者接口实现其中的accept方法
List<Author> sampleAuthors = AuthorFactory.createSampleAuthors(); sampleAuthors.stream() .forEach(new Consumer<Author>() { @Override public void accept(Author author) { System.out.println(author); } });Lambda表达式形式(方法引用)
List<Author> sampleAuthors = AuthorFactory.createSampleAuthors(); sampleAuthors.stream() .forEach(System.out::println);
count
获得当前流中元素的个数
List<Author> sampleAuthors = AuthorFactory.createSampleAuthors(); long count = sampleAuthors.stream() .count(); System.out.println(count);
min/max
取得流中的最大/最小值
List<Author> sampleAuthors = AuthorFactory.createSampleAuthors(); sampleAuthors.stream() .max((o1, o2) -> o1.getAge() - o2.getAge()) .ifPresent(System.out::println);List<Author> sampleAuthors = AuthorFactory.createSampleAuthors(); sampleAuthors.stream() .min((o1, o2) -> o1.getAge() - o2.getAge()) .ifPresent(System.out::println);其中ifPresent方法用来防止空值,避免空指针异常。




412

被折叠的 条评论
为什么被折叠?



