Stream快速入门

数据准备

```java public class Author { private Long id; private String name; private Integer age; private String intro; private List books; }

public class Book { private Long id; private String name; private String category; // "哲学,爱情,个人成长,个人传记" private Integer score; private String intro; } ```

```java private static List getAuthors() { //数据初始化 Author author = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null); Author author2 = new Author(2L,"亚拉索",15,"狂风也追逐不上他的思考速度",null); Author author3 = new Author(3L,"易",14,"是这个世界在限制他的思维",null); Author author4 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);

//书籍列表
    List<Book> books1 = new ArrayList<>();
    List<Book> books2 = new ArrayList<>();
    List<Book> books3 = new ArrayList<>();

    books1.add(new Book(1L,"刀的两侧是光明与黑暗","哲学,爱情",88,"用一把刀划分了爱恨"));
    books1.add(new Book(2L,"一个人不能死在同一把刀下","个人成长,爱情",99,"讲述如何从失败中明悟真理"));

    books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
    books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
    books2.add(new Book(4L,"吹或不吹","爱情,个人传记",56,"一个哲学家的恋爱观注定很难把他所在的时代理解"));

    books3.add(new Book(5L,"你的剑就是我的剑","爱情",56,"无法想象一个武者能对他的伴侣这么的宽容"));
    books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?"));
    books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?"));

    author.setBooks(books1);
    author2.setBooks(books2);
    author3.setBooks(books3);
    author4.setBooks(books3);

    List<Author> authorList = new ArrayList<>(Arrays.asList(author,author2,author3,author4));
    return authorList;
}

```

创建流
  • 单列集合:集合对象.stream()

  • 数组:Arrays.stream(数组)

  • 双列集合:转换成单列集合后再创建

    ```java Map map = new HashMap<>(); map.put("张三",17); map.put("向阳",16);

    Stream > stream = map.entrySet().stream(); ```

中间操作
map

java // 打印所有作家名字 List<Author> authors = getAuthors(); authors.stream().map(s -> s.getName()).forEach(s -> System.out.println(s));

sort()

java // 对年龄进行降序, 版本一 List<Author> authors = getAuthors(); authors.stream() .sorted() // 空参的sorted()方法,需要流中的元素是实现了Comparable .forEach(author -> System.out.println(author.getAge()));

java // 对年龄进行降序, 版本二 authors.stream() .sorted((o1, o2) -> o2.getAge()-o1.getAge()) .forEach(author -> System.out.println(author.getAge()));

flatMap

java // 打印出书的类别 List<Author> authors = getAuthors(); authors.stream() .flatMap(author -> author.getBooks().stream()) .distinct() .flatMap(book -> Arrays.stream(book.getCategory().split(","))) .distinct() .forEach(category-> System.out.println(category));

终结操作
forEach
count

java // 打印所有作家的书籍数量 long count = authors.stream() .flatMap(s -> s.getBooks().stream()) .distinct() .count(); System.out.println(count);

max、min

java Optional<Book> maxScoreBook = authors.stream() .flatMap(s -> s.getBooks().stream()) .max((book1, book2) -> book1.getScore() - book2.getScore());

collect

获取Map集合,map中key为作者名,value为List<Book>

```java // 简洁版 Map > authorList = authors.stream() .distinct() .collect(Collectors.toMap(Author::getName, Author::getBooks));

// 详细版 Map > list = authors.stream() .distinct() .collect(Collectors.toMap( new Function () { @Override public String apply(Author author) { return author.getName(); } }, new Function >() { @Override public List apply(Author author) { return author.getBooks(); } } )); ```

anyMatch

是否有一个符合条件的元素,有一个满足则为true,否则为false

java // 判断是否有年龄在29以上的作家 List<Author> authors = getAuthors(); boolean flag = authors.stream()

allMatch

所有元素是否都符合指定条件,都符合为true,否则为false

java // 判断是否所有的作家都是成年人 List<Author> authors = getAuthors(); boolean flag = authors.stream() .allMatch(author -> author.getAge() >= 18); System.out.println(flag);

noneMatch

所有元素是否都不符合指定条件,都不符合结果为true,否则为false

java // 判断作家是否都没有超过100岁的。 List<Author> authors = getAuthors(); boolean flag = authors.stream() .noneMatch(author -> author.getAge() > 100);

findFirst

java // 获取年龄最小的作家,并输出他的名字 List<Author> authors = getAuthors(); Optional<Author> first = authors.stream() .sorted((o1, o2) -> o1.getAge() - o2.getAge()) .findFirst(); first.ifPresent(author -> System.out.println(author.getName()));

reduce

将流中的元素按指定规则计算出一个结果,一个参数的内部计算过程(还有两个参数、三个参数的重载方法):

java boolean foundAny = false; T result = null; // result返回结果 for (T element : this stream) { if (!foundAny) { foundAny = true; result = element; // 将第一个元素赋值给result,进行初始化 } else result = accumulator.apply(result, element); // 自己定义的计算规则 } return foundAny ? Optional.of(result) : Optional.empty();

example:求所有作家中年龄的最大值

java Optional<Integer> res = authors.stream() .map(Author::getAge) // (result, element)整体作为一个参数 .reduce((result, element) -> element > result ? element : result); // result:返回结果,element:流中的每个元素,初始化时流中第一个元素赋值给result,后面元素按照指定规则计算后赋值给result

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CUDA(Compute Unified Device Architecture)是NVIDIA推出的并行计算平台和API模型,用于利用GPU的计算能力。以下是一个CUDA快速入门代码的示例: 代码示例: #include <cuda_runtime.h> #include <stdio.h> // CUDA核函数,执行在GPU上 __global__ void cudaHelloWorld() { printf("Hello World from GPU!\n"); } int main() { // 调用CUDA异步执行的配置 cudaStream_t stream; cudaStreamCreate(&stream); // 定义核函数的执行配置 dim3 block(1, 1); dim3 grid(1, 1); // 在GPU上调用核函数 cudaHelloWorld<<<grid, block, 0, stream>>>(); // 同步GPU,等待核函数执行完成 cudaStreamSynchronize(stream); // 销毁CUDA流 cudaStreamDestroy(stream); // 输出CPU上的信息 printf("Hello World from CPU!\n"); return 0; } 该示例代码中,我们在主函数中调用CUDA核函数cudaHelloWorld,并在GPU上并行执行。核函数使用__global__修饰符标记,表明它将在GPU上执行。在主函数中,我们首先使用cudaStreamCreate函数创建一个CUDA流,用于异步执行核函数。然后,我们定义了核函数的执行配置,即指定了需要启动的线程块数量和线程块中的线程数量。在调用核函数时,我们传递了执行配置、流对象和其他参数。接着,我们使用cudaStreamSynchronize函数来等待GPU上的核函数执行完成,以确保输出的正确顺序。最后,我们使用printf函数输出来自CPU和GPU的信息。 这个示例代码展示了如何使用CUDA快速入门,并在GPU上进行并行计算。通过学习和掌握CUDA编程,开发者可以充分利用GPU的并行计算能力,加速各种科学计算和计算密集型任务的执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值