📜  Java中流和集合的区别

📅  最后修改于: 2021-09-15 01:39:28             🧑  作者: Mango

Collection是一种内存数据结构,它保存数据结构当前具有的所有值。在我们将它添加到集合之前,必须计算集合中的每个元素。可以对集合进行搜索、排序、插入、操作、删除等操作。它提供了许多接口,如(Set、List、Queue、Deque)和类(ArrayList、Vector、LinkedList、PriorityQueue、HashSet)。

Java 中的流和集合之间的差异

一个流 是一种固定的数据结构,其中元素是按需计算的。 Stream API 用于处理对象集合。流是支持各种方法的对象序列,这些方法可以通过流水线来产生所需的结果。

Java流的特点是:

  • 它从集合、数组或 I/O 通道获取输入,而不是数据结构。
  • Streams 仅根据流水线方法提供结果,并且不更改原始数据结构。

    STREAMS        

  COLLECTIONS          

It doesn’t store data, it operates on the source data structure i.e collection. It stores/holds all the data that the data structure currently has in a particular data structure like Set, List or Map,
They use functional interfaces like lambda which makes it a good fit for programming language. They don’t use functional interfaces.
Java Streams are consumable i.e; to traverse the stream, it needs to be created every time. They are non-consumable i.e; can be traversable multiple times without creating it again.
Java streams support both sequential and parallel processing. It supports parallel processing and parallel processing can be very helpful in achieving high performance.
All the Java stream API interfaces and classes are in java.util.stream package. Specific classes for primitive types such as IntStream, LongStream, and DoubleStream are used in collections since primitive data types such as int, long in the collections using auto-boxing and these operations could take a lot of time.
Streams are not modifiable i.e one can’t add or remove elements from streams. These are modifiable i.e one can easily add to or remove elements from collections.
Streams are iterated internally by just mentioning the operations. Collections are iterated externally using loops.

集合示例

Java
// Java program to demonstrate
// Collection example
  
import java.util.*;
import java.io.*;
  
class CollectionsExample {
    public static void main(String[] args)
    {
        // create an instance of list
        List CompanyList = new ArrayList<>();
  
        // add elements
        CompanyList.add("Google");
        CompanyList.add("Apple");
        CompanyList.add("Microsoft");
  
        // create a comparator
        Comparator com
            = (String o1, String o2) -> o1.compareTo(o2);
  
        // sort the list
        Collections.sort(CompanyList, com);
  
        // print the elements
        for (String name : CompanyList) {
            System.out.println(name);
        }
    }
}


Java
// Java program to demonstrate streams
  
import java.io.*;
import java.util.*;
  
class StreamsExample {
    public static void main(String[] args)
    {
        // create a list
        List CompanyList = new ArrayList<>();
  
        // add elements
        CompanyList.add("Google");
        CompanyList.add("Apple");
        CompanyList.add("Microsoft");
  
        // sort the list
        CompanyList.stream().sorted().forEach(
            System.out::println);
    }
}


输出
Apple
Google
Microsoft

流示例

Java

// Java program to demonstrate streams
  
import java.io.*;
import java.util.*;
  
class StreamsExample {
    public static void main(String[] args)
    {
        // create a list
        List CompanyList = new ArrayList<>();
  
        // add elements
        CompanyList.add("Google");
        CompanyList.add("Apple");
        CompanyList.add("Microsoft");
  
        // sort the list
        CompanyList.stream().sorted().forEach(
            System.out::println);
    }
}
输出
Apple
Google
Microsoft