首頁 > Java > java教程 > 主體

Java集合流

王林
發布: 2024-08-30 15:47:35
原創
544 人瀏覽過

Java Stream 是集合元件之一,它是用於在內部迭代其專案的API,也就是說,如果我們想要使用Java 集合迭代功能,例如Java 迭代器或Java 迭代器,它可以迭代自己的元素。 for-each 循環具有Java 可迭代接口,它必須實現元素迭代,流不會更改資料結構記憶體中的值,它將使用預設方法提供結果,我們可以為其分配預設空間(如管道結構)產生所需的輸出。

Java 集合流指南

首先,這可能是向集合介面引入流方法的最有爭議的地方。顯示項目清單後,將使用特定流開啟流資料。最簡單和最受歡迎的操作是 forEach() 循環,它遍歷流元素並在每個元素上呼叫給定的函數。這個方法也被廣泛使用,它已經被包含在Iterable、Map和其他類別中。流只不過是資料包裝器,它們不想儲存資料或影響底層資料來源。流支援許多有用且高效能的操作,這些操作用可以順序或並行完成的 lambda 進行簡潔描述。流不是資料結構,因為它不儲存任何資料。它也不會對底層資料來源進行任何更改。如果資料與我們的 Stream 是有序的,那麼我們的資料是按順序處理還是不按順序處理並不重要;該實作將保持 Stream 的遭遇順序。通常,如果使用的 Spliterator 具有 CONCURRENT 屬性,則流是線程安全的。流 API 為管道的每個步驟定義了許多契約,如果其中任何一個被破壞,則可能會發生意外的行為或異常。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

來自集合實例的流實例

流從集合、陣列和其他輸入/輸出通道取得輸入,這些是將輸入傳遞給函數的方式。在新的物件建立過程的幫助下,將建立物件並將其指派給特定的函數呼叫。此外,Streams 僅按照管道架構及其方法提供輸出結果,不會改變值和真實的資料結構過程。此外,集合主要是在其他循環和外部循環的幫助下進行迭代的,但是當我們使用串流時,需要在特定循環操作的幫助下進行內部迭代,大多數情況下它不需要外部循環。 Java 8中Stream API的終端方法之一是用於收集資料的collect()方法。它使我們能夠使用 Stream 實例中包含的資料元件進行可變折疊操作(例如,將元素重新打包到資料結構中並應用附加邏輯、連接它們等)。在 Collectors 類別中,您將找到所有預先定義的實作。為了獲得更好的可讀性,通常的做法是使用以下靜態導入。若要將所有 Stream 項目聚合到 List 物件中,請使用 toList 收集器。要知道的關鍵一點是,使用這種技術,我們不能假設任何特定的 List 實作。如果我們想更好地控制它,我們可以使用 toCollection 來代替。若要將所有 Stream 項目聚合到 Set 實例中,請使用 toSet 收集器。重要的是要認識到,使用這種方法,我們不能假設任何特定的 Set 實作。如果我們想更好地控制它,我們可以使用集合。 Set 中沒有重複的元素。如果我們的集合包含相同的元件,它們只會在最終集合中出現一次。若要將 Stream 項目聚合到 Map 實例中,請使用 toMap 收集器。為此,我們需要兩個函數:keyMapper 和 valueMapper。要從 Stream 元素中提取 Map 鍵,我們將使用 keyMapper,要檢索與特定鍵關聯的值,我們將使用 valueMapper。

Java 流創建

在java中建立流的不同方式,例如使用集合、從指定值建立流資料。它還使用流資料建立一個數組,並在 Stream 的幫助下計算空白空間。 empty() 方法不僅可以計算空白空間,我們還可以使用 Stream.empty() 方法建立 Stream 資料。構建器()方法。借助 iterate() 方法,我們可以計算無限的 Stream 數據,因為 Iterable 是接口,它沒有提供任何預設方法 StreamSupport.Stream() 方法,使用 Iterable 物件來取得 Stream 資料。

範例#1

import java.util.*;
import java.util.stream.*;
class first {
private static <T> void methd(Iterable<T> itr)
{
Stream<T> str
= StreamSupport
.stream(itr.spliterator(),
false);
Iterator<T> its = str.iterator();
while (its.hasNext()) {
System.out.print(its.next() + " ");
}
}
public static void main(String[] args)
{
Iterable<String> itr
= Arrays.<em>asList</em>("Marina", "Adyar", "Parrys or Broadway","Tambaram","Gundy");
methd(itr);
}
}
登入後複製

範例輸出:

Java集合流

In the above example we used Stream Instance creation and also we can use the StreamSupport class for accessing the stream() method. Like that we can use the Iterator<> interface for accessing the stream inputs and assign it to the variable. In the main method using Iterable<> interface we can access the values as Arrays and asList() method.

Example #2

import java.util.*;
import java.util.stream.*;
class Second {
private static <T> void methd(Iterator<T> itr)
{
Spliterator<T> splititr
= Spliterators
spliteratorUnknownSize(itr,
Spliterator.NONNULL);
Stream<T> str
= StreamSupport.<em>stream</em>(splititr, false);
Iterator<T> its = str.iterator();
while (its.hasNext()) {
System.<em>out</em>.print(its.next() + " ");
}
}
public static void main(String[] args)
Iterator<String> itre = Arrays
.asList("Thiruvanmaiyur", "Perungalathur", "TNagar","Central","Besant Nagar")
.iterator();
methd(itre);
}
}
登入後複製

Sample Output:

Java集合流

In the above example, we used an additionally Spliterator class for accessing the Stream inputs using Iterator interface we can access the Array List inputs using the asList() method.

Conclusion

While streams aren’t used by everyone and don’t necessarily indicate a superior approach, free certificate courses can help developers understand this newer way of programming, which incorporates functional-style programming and lambda expressions for Java. It’s up to developers to determine whether to use functional or imperative programming styles, and by taking advantage of free certificate courses, they can learn how to effectively combine both ideas to enhance their programs with enough effort.

以上是Java集合流的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!