Java 8 reverse list Mới nhất Mới nhất

Kinh Nghiệm Hướng dẫn Java 8 reverse list Mới nhất Mới Nhất

Pro đang tìm kiếm từ khóa Java 8 reverse list Mới nhất được Cập Nhật vào lúc : 2022-02-03 18:22:00 . Với phương châm chia sẻ Thủ Thuật Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi Read nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Mình lý giải và hướng dẫn lại nha.

Quý khách đang tìm kiếm từ khóa Java 8 reverse list được Cập Nhật vào lúc : 2022-02-03 18:22:02 . Với phương châm chia sẻ Bí quyết về trong nội dung nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi tìm hiểu thêm nội dung nội dung bài viết vẫn ko hiểu thì hoàn toàn hoàn toàn có thể lại phản hồi ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.

In this episode we will show how to reverse the element position while maintaining order in a java 8 stream.

Detailed Video Notes
Reverse order and print
Proving the sort
Reverse order convert to ArrayList
Iterable to Arraylist using guava
Reverse order numeric stream

Detailed Video Notes

Java 8 introduced stream which is a sequence of elements supporting sequential and parallel aggregate operations. In addition it helped introduce a functional programming style to java. It is a possible that a stream has unordered elements so this tutorial is not to be confused with sorting a stream in reverse order. Let’s find out how to flip the elements in the stream while maintaining order.

Setup

[0:21]

Generating a java project from a maven archetype quick start we will update the project to use java 8 and add the dependencies of junit and guava.

junit

junit

4.11

test

com.google.guava

guava

18.0

Reverse order and print

[0:29]

For our first java example let us create a Stream of Strings then collect the elements into a LinkedList. Since a LinkedList is a double-linked data structure we are able to navigate in either direction, forward or reverse. Calling DescendingIterator allows us to iterate over the elements in reverse sequential order. We can output the elements in the Stream by passing the System.out::println consumer to the forEachRemaining method.

Lets run our test and examine the output of the elements in reversed order.

@Test

public void reverse_stream_print()

Stream.of(“One”, “Two”, “Three”, “Four”)

.collect(Collectors.toCollection(LinkedList::new))

.descendingIterator().forEachRemaining(System.out::println);

Output

Four

Three

Two

One

Proving the sort

[0:57]

Just to prove that the order is different when you sort a stream, lets write a quick test method and sort the elements in reversed order. Again we will build a stream calling sorted, an intermediate stream operation, passing in Collections.reverseOrder() will sort the stream in reverse alphabetical order. Using the foreach we can print the elements of the stream.

Lets run our test and examine the output of the elements in reversed order.

@Test

public void sorting_reverse()

Stream.of(“One”, “Two”, “Three”, “Four”)

.sorted(Collections.reverseOrder())

.forEach(System.out::println);

Output

Two

Three

One

Four

Reverse order convert to ArrayList

[1:20]

Next you might be thinking, how do I convert an Iterator to a collection? This is a two step process first we must reverse the order and then transform the Iterator into a list. Using the same snippet above and assigning reversedStream to the Iterator returned we can create a new stream by using StreamSupport and Spliterators. Finally we can transform stream tolist using Collectors utility.

Lets run our test and examine the output of converting Iterator to list using stream functions.

@Test

public void reverse_stream_collect_to_list()

Iterator reversedStream = Stream

.of(“One”, “Two”, “Three”, “Four”)

.collect(Collectors.toCollection(LinkedList::new))

.descendingIterator();

List listReversedOrder = StreamSupport.stream(

Spliterators.spliteratorUnknownSize(reversedStream,

Spliterator.ORDERED), false).collect(

Collectors. toList());

System.out.println(listReversedOrder);

Output

[Four, Three, Two, One]

Iterable to Arraylist using guava

[1:56]

If the previous snippet is to painful and you already have guava on you classpath, you could convert Iterable to arraylist using guava’s list utility.

@Test

public void iterator_to_list_guava()

Iterator reversedStream = Stream

.of(“One”, “Two”, “Three”, “Four”)

.collect(Collectors.toCollection(LinkedList::new))

.descendingIterator();

List reversedList = Lists.newArrayList(reversedStream);

System.out.println(reversedList);

Output

[Four, Three, Two, One]

Reverse order numeric stream

[2:5]

If you are working with numbers the following technique could be applied to IntStream, DoubleStream or LongStream. Note: this technique was not tested and verified for performance.

Creating a static method that accepts an IntStream we will create an array from a stream and then use the same techniques we find in reversing elements of an array in java.

Let’s run our example to see the output.

public static IntStream reverseOrderStream(IntStream intStream)

int[] tempArray = intStream.toArray();

return IntStream.range(1, tempArray.length + 1).boxed()

.mapToInt(i -> tempArray[tempArray.length – i]);

@Test

public void intstream_reverse_order()

reverseOrderStream(IntStream.of(1, 4, 5, 6, 7)).forEach(

System.out::println);

Output

7

6

5

4

1

I hope this showed the difference in reversing the order of elements of a stream vs sorting in reverse order while providing a few techniques to convert an Iterable to a collection.

Thanks for joining in today’s level up, have a great day!

Reverse elements of stream java 8 posted by Justin Musgrove on 23 February 2015

Tagged: java, java-tutorial, and java8

Share on: Meta Google+

All the code on this page is available on github: View the source

Reply

0

0

Chia sẻ

Share Link Cập nhật Java 8 reverse list miễn phí

Bạn vừa đọc nội dung nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Review Java 8 reverse list tiên tiến và phát triển và tăng trưởng nhất Chia SẻLink Download Java 8 reverse list Free.

Giải đáp vướng mắc về Java 8 reverse list

Nếu sau khi đọc nội dung nội dung bài viết Java 8 reverse list vẫn chưa hiểu thì hoàn toàn hoàn toàn có thể lại phản hồi ở cuối bài để Mình lý giải và hướng dẫn lại nha

#Java #reverse #list

Clip Java 8 reverse list Mới nhất ?

Bạn vừa đọc tài liệu Với Một số hướng dẫn một cách rõ ràng hơn về Video Java 8 reverse list Mới nhất tiên tiến và phát triển nhất

Share Link Download Java 8 reverse list Mới nhất miễn phí

Bạn đang tìm một số trong những Share Link Down Java 8 reverse list Mới nhất miễn phí.

Giải đáp vướng mắc về Java 8 reverse list Mới nhất

Nếu sau khi đọc nội dung bài viết Java 8 reverse list Mới nhất vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Admin lý giải và hướng dẫn lại nha
#Java #reverse #list #Mới #nhất

Phone Number

Share
Published by
Phone Number

Recent Posts

Tra Cứu MST KHƯƠNG VĂN THUẤN Mã Số Thuế của Công TY DN

Tra Cứu Mã Số Thuế MST KHƯƠNG VĂN THUẤN Của Ai, Công Ty Doanh Nghiệp…

3 years ago

[Hỏi – Đáp] Cuộc gọi từ Số điện thoại 0983996665 hoặc 098 3996665 là của ai là của ai ?

Các bạn cho mình hỏi với tự nhiên trong ĐT mình gần đây có Sim…

3 years ago

Nhận định về cái đẹp trong cuộc sống Chi tiết Chi tiết

Thủ Thuật về Nhận định về nét trẻ trung trong môi trường tự nhiên vạn…

3 years ago

Hướng Dẫn dooshku là gì – Nghĩa của từ dooshku -Thủ Thuật Mới 2022

Thủ Thuật về dooshku là gì - Nghĩa của từ dooshku -Thủ Thuật Mới 2022…

3 years ago

Tìm 4 số hạng liên tiếp của một cấp số cộng có tổng bằng 20 và tích bằng 384 2022 Mới nhất

Kinh Nghiệm Hướng dẫn Tìm 4 số hạng liên tục của một cấp số cộng…

3 years ago

Mẹo Em hãy cho biết nếu đèn huỳnh quang không có lớp bột huỳnh quang thì đèn có sáng không vì sao Mới nhất

Mẹo Hướng dẫn Em hãy cho biết thêm thêm nếu đèn huỳnh quang không còn…

3 years ago