Stream sorted in Java with Examples - Java 147

Stream sorted in Java with Examples – Java 147

Stream sorted in Java with Examples

提供了建立自定義比較器,比較器是實作 Comparator 接口的物件,調用 Stream sorted 方法,對集合進行排序, Java Stream Sorted with Examples 自定義比較器,比較兩個物件並傳回一個整數,本篇增加了範例,並透過單元測試來驗證產出結果。

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

檔案目錄

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

單元測試

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

sort

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

	@Test
	public void sort() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted().collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}
[9223372036854775807, -22, 33]
[-22, 33, 9223372036854775807]

sortWithReverseOrder

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

	@Test
	public void sortWithReverseOrder() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.reverseOrder())
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(Long.MAX_VALUE, 33L, -22L);
	}
[9223372036854775807, -22, 33]
[9223372036854775807, 33, -22]

sortWithComparator

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

	@Test
	public void sortWithComparator() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted((i1, i2) -> Long.compare(i1, i2))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}
[9223372036854775807, -22, 33]
[-22, 33, 9223372036854775807]

sortWithComparing

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

	@Test
	public void sortWithComparing() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.comparing(Long::longValue))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}
[9223372036854775807, -22, 33]
[-22, 33, 9223372036854775807]

sortWithComparingLong

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

	@Test
	public void sortWithComparingLong() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.comparingLong(l -> l))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}
[9223372036854775807, -22, 33]
[-22, 33, 9223372036854775807]

sortWithMultipleConditions

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

	protected static Comparator<Long> valueComparator = new Comparator<Long>() {
		@Override
		public int compare(Long i1, Long i2) {
			return Long.compare(i1, i2);
		}
	};

	protected static Comparator<Long> lengthComparator = (i1, i2) -> Long.compare(String.valueOf(i1).length(),
			String.valueOf(i2).length());

	@Test
	public void sortWithMultipleConditions() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(valueComparator.thenComparing(lengthComparator))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}
[9223372036854775807, -22, 33]
[-22, 33, 9223372036854775807]

sortWithNull

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

	@Test
	public void sortWithNull() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L, null);
		System.out.println(list);

		ArrayList<Long> 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, -22L, 33L, Long.MAX_VALUE);
	}
[9223372036854775807, -22, 33, null]
[null, -22, 33, 9223372036854775807]

sortWithNullsFirst

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

	@Test
	public void sortWithNullsFirst() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L, null);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.nullsFirst(Comparator.comparing(s -> s)))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(null, -22L, 33L, Long.MAX_VALUE);
	}
[9223372036854775807, -22, 33, null]
[null, -22, 33, 9223372036854775807]

sortWithNullsLast

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

	@Test
	public void sortWithNullsLast() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L, null);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.nullsLast(Comparator.comparing(s -> s)))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE, null);
	}
[9223372036854775807, -22, 33, null]
[-22, 33, 9223372036854775807, null]

StreamSortWithExamplesTest.java

Stream Sorted Java 新增單元測試,驗證 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.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Test;

import com.google.common.collect.Lists;

public class StreamSortedWithExamplesTest {

	@Test
	public void sort() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted().collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}

	@Test
	public void sortWithReverseOrder() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.reverseOrder())
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(Long.MAX_VALUE, 33L, -22L);
	}

	@Test
	public void sortWithComparator() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted((i1, i2) -> Long.compare(i1, i2))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}

	@Test
	public void sortWithComparing() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.comparing(Long::longValue))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}

	@Test
	public void sortWithComparingLong() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.comparingLong(l -> l))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}

	protected static Comparator<Long> valueComparator = new Comparator<Long>() {
		@Override
		public int compare(Long i1, Long i2) {
			return Long.compare(i1, i2);
		}
	};

	protected static Comparator<Long> lengthComparator = (i1, i2) -> Long.compare(String.valueOf(i1).length(),
			String.valueOf(i2).length());

	@Test
	public void sortWithMultipleConditions() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(valueComparator.thenComparing(lengthComparator))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE);
	}

	@Test
	public void sortWithNull() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L, null);
		System.out.println(list);

		ArrayList<Long> 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, -22L, 33L, Long.MAX_VALUE);
	}

	@Test
	public void sortWithNullsFirst() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L, null);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.nullsFirst(Comparator.comparing(s -> s)))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(null, -22L, 33L, Long.MAX_VALUE);
	}

	@Test
	public void sortWithNullsLast() {
		List<Long> list = Lists.newArrayList(Long.MAX_VALUE, -22L, 33L, null);
		System.out.println(list);

		ArrayList<Long> result = list.stream().sorted(Comparator.nullsLast(Comparator.comparing(s -> s)))
				.collect(Collectors.toCollection(ArrayList::new));
		System.out.println(result);
		assertThat(result).containsExactly(-22L, 33L, Long.MAX_VALUE, null);
	}
}

心得分享

Java Stream Sorted Example 提供了幾種 Sort 常見方法的操作範例,對不同類型的資料進行排序,像是字串、數字和物件,對資料進行排序可能很有用,例如:產品可以根據用戶設定的要求,像是:價格、評級、品牌,以多種方式排序,熟悉 Stream Sorted Java 這些方法的操作,讓排序依照不同的需求,快速方便地實現功能。

發佈留言