Java Stream sorted Method - Java 147

Java Stream sorted Method – Java 147

Java Stream sorted Method

提供 Stream sorted 方法對集合中的元素進行排序,使用標準庫中的類別,建立 java.util.List 來執行操作, Sorted Java Stream 支援預設和自定義比較器,比較兩個物件並傳回一個整數,本篇增加了範例,並透過單元測試來驗證產出結果。

Stream<T> sorted(Comparator<? super T> comparator);

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- stream
       |                   +- sort
       |                       +- StreamSortedTest.java   

單元測試

Stream Sorted Java 提供不同類型的資料進行排序,使用預設或自定義比較器,排序集合中的元素。

sort

Stream Sorted Java 建立一個 List ,增加三個元素,使用 Stream sorted 方法,依照預設比較器,由小到大,升序對集合進行排序。

	@Test
	public void sort() {
		List<String> list = Arrays.asList("Lichee", "Coconut", "Plum");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted().collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Coconut", "Lichee", "Plum");
	}
[Lichee, Coconut, Plum]
[Coconut, Lichee, Plum]

sortWithReverseOrder

Stream Sorted Java 建立一個 List ,增加三個元素,使用 Stream sorted 方法,依照預設反向比較器,由大到小,降序對集合進行排序。

	@Test
	public void sortWithReverseOrder() {
		List<String> list = Arrays.asList("Lichee", "Coconut", "Plum");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(Comparator.reverseOrder())
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Plum", "Lichee", "Coconut");
	}
[Lichee, Coconut, Plum]
[Plum, Lichee, Coconut]

sortWithComparator

Stream Sorted Java 建立一個 List ,增加三個元素,使用 Stream sorted 方法,依照自定義比較器,由小到大,升序對集合進行排序。

	@Test
	public void sortWithComparator() {
		List<String> list = Arrays.asList("Lichee", "Coconut", "Plum");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted((s1, s2) -> s1.length() - s2.length())
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Plum", "Lichee", "Coconut");
	}
[Lichee, Coconut, Plum]
[Plum, Lichee, Coconut]

sortWithComparing

Stream Sorted Example 建立一個 List ,增加三個元素,使用 Stream sorted 方法,依照自定義比較器,由小到大,升序對集合進行排序。

	@Test
	public void sortWithComparing() {
		List<String> list = Arrays.asList("Lichee", "Coconut", "Plum");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(Comparator.comparing(String::length))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Plum", "Lichee", "Coconut");
	}
[Lichee, Coconut, Plum]
[Plum, Lichee, Coconut]

sortWithMultipleConditions

Stream Sorted Example 建立一個 List ,增加三個元素,使用 Stream sorted 方法,依照多個自定義比較器,由小到大,升序對集合進行排序。

	protected static Comparator<String> nameComparator = new Comparator<String>() {
		@Override
		public int compare(String s1, String s2) {
			return s1.compareTo(s2);
		}
	};

	protected static Comparator<String> lengthComparator = (s1, s2) -> Integer.compare(s1.length(), s2.length());

	@Test
	public void sortWithMultipleConditions() {
		List<String> list = Arrays.asList("Coconut", "Plum", "Lichee");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(nameComparator.thenComparing(lengthComparator))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Coconut", "Lichee", "Plum");
	}
[Coconut, Plum, Lichee]
[Coconut, Lichee, Plum]

sortWithNull

建立一個 List ,增加三個元素及一個 null 元素,使用 Stream sorted 方法,依照自定義比較器, null 元素放在第一個,由小到大,升序對集合進行排序。

	@Test
	public void sortWithNull() {
		List<String> list = Arrays.asList("Coconut", "Plum", "Lichee", null);
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted((s1, s2) -> {
			if (s1 == null) {
				return s2 == null ? 0 : -1;
			} else if (s2 == null) {
				return 1;
			}
			return s1.compareTo(s2);
		}).collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(null, "Coconut", "Lichee", "Plum");
	}
[Coconut, Plum, Lichee, null]
[null, Coconut, Lichee, Plum]

sortWithNullsFirst

建立一個 List ,增加三個元素及一個 null 元素,使用 Stream sorted 方法,依照 nullsFirst 比較器, null 元素放在第一個,由小到大,升序對集合進行排序。

	@Test
	public void sortWithNullsFirst() {
		List<String> list = Arrays.asList("Coconut", "Plum", "Lichee", null);
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(Comparator.nullsFirst(Comparator.comparing(s -> s)))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(null, "Coconut", "Lichee", "Plum");
	}
[Coconut, Plum, Lichee, null]
[null, Coconut, Lichee, Plum]

sortWithNullsLast

建立一個 List ,增加三個元素及一個 null 元素,使用 Stream sorted 方法,依照 nullsLast 比較器, null 元素放在最後個,由小到大,升序對集合進行排序。

	@Test
	public void sortWithNullsLast() {
		List<String> list = Arrays.asList("Coconut", "Plum", "Lichee", null);
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(Comparator.nullsLast(Comparator.comparing(s -> s)))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Coconut", "Lichee", "Plum", null);
	}
[Coconut, Plum, Lichee, null]
[Coconut, Lichee, Plum, null]

StreamSortedTest.java

Stream Sorted Example 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.stream.sort;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Test;

public class StreamSortedTest {

	@Test
	public void sort() {
		List<String> list = Arrays.asList("Lichee", "Coconut", "Plum");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted().collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Coconut", "Lichee", "Plum");
	}

	@Test
	public void sortWithReverseOrder() {
		List<String> list = Arrays.asList("Lichee", "Coconut", "Plum");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(Comparator.reverseOrder())
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Plum", "Lichee", "Coconut");
	}

	@Test
	public void sortWithComparator() {
		List<String> list = Arrays.asList("Lichee", "Coconut", "Plum");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted((s1, s2) -> s1.length() - s2.length())
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Plum", "Lichee", "Coconut");
	}

	@Test
	public void sortWithComparing() {
		List<String> list = Arrays.asList("Lichee", "Coconut", "Plum");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(Comparator.comparing(String::length))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Plum", "Lichee", "Coconut");
	}

	protected static Comparator<String> nameComparator = new Comparator<String>() {
		@Override
		public int compare(String s1, String s2) {
			return s1.compareTo(s2);
		}
	};

	protected static Comparator<String> lengthComparator = (s1, s2) -> Integer.compare(s1.length(), s2.length());

	@Test
	public void sortWithMultipleConditions() {
		List<String> list = Arrays.asList("Coconut", "Plum", "Lichee");
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(nameComparator.thenComparing(lengthComparator))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Coconut", "Lichee", "Plum");
	}

	@Test
	public void sortWithNull() {
		List<String> list = Arrays.asList("Coconut", "Plum", "Lichee", null);
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted((s1, s2) -> {
			if (s1 == null) {
				return s2 == null ? 0 : -1;
			} else if (s2 == null) {
				return 1;
			}
			return s1.compareTo(s2);
		}).collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(null, "Coconut", "Lichee", "Plum");
	}

	@Test
	public void sortWithNullsFirst() {
		List<String> list = Arrays.asList("Coconut", "Plum", "Lichee", null);
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(Comparator.nullsFirst(Comparator.comparing(s -> s)))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(null, "Coconut", "Lichee", "Plum");
	}

	@Test
	public void sortWithNullsLast() {
		List<String> list = Arrays.asList("Coconut", "Plum", "Lichee", null);
		System.out.println(list);

		ArrayList<String> result = list.stream().sorted(Comparator.nullsLast(Comparator.comparing(s -> s)))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly("Coconut", "Lichee", "Plum", null);
	}
}

心得分享

Java Stream Sorted 實務上經常會使用到資料排序,例如:使用者想要查看按照價格升序排列的所有產品,將能夠首先看到最便宜的產品,進而做出相應的購買決定,提供了幾種 Sort 常見方法的操作範例,在應用上相當廣泛,熟悉 Stream Sorted Example 這些方法的操作,可以快速簡易地實現排序功能。

發佈留言