Table of Contents
ToggleLinkedHashSet in Java with Examples
以雙向鏈結的方式,維護元素的新增、修改、刪除等操作,會依新增順序保持排序,自動調整其容量,是一個非同步的操作,其集合中的元素,不可重複, LinkedHashSet in Java 介紹常見的 forEach 、 iterator 、 spliterator 、 trySplit 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- set
| +- linkedhashset
| +- LinkedHashSetWithExamplesTest.java
單元測試
LinkedHashSet Java 提供循環訪問、轉成陣列等操作 Set 中的元素。
forEach
建立 Fruit 物件,覆寫 equals 、 hashCode 提供給集合比較使用,建立一個 LinkedHashSet ,內有三個元素,迴圈取得元素。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Fruit)) {
return false;
}
if (this == object) {
return true;
}
Fruit other = (Fruit) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
@Test
public void forEach() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
set.forEach(System.out::println);
}
{"name":"Longan","quantity":1.0,"type":1}
{"name":"Tomato","quantity":2.0,"type":1}
{"name":"Pear","quantity":3.0,"type":1}
forEachRemaining
LinkedHashSet Java 建立一個 LinkedHashSet ,內有三個元素,迴圈取得剩餘元素。
@Test
public void forEachRemaining() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Iterator<Fruit> it = set.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
{"name":"Longan","quantity":1.0,"type":1}
{"name":"Tomato","quantity":2.0,"type":1}
----------
{"name":"Pear","quantity":3.0,"type":1}
iterator
LinkedHashSet Java 建立一個 LinkedHashSet ,內有三個元素,迴圈取得元素。
@Test
public void iterator() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Iterator<Fruit> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
{"name":"Longan","quantity":1.0,"type":1}
{"name":"Tomato","quantity":2.0,"type":1}
{"name":"Pear","quantity":3.0,"type":1}
spliterator
LinkedHashSet Spliterator in Java 建立一個 LinkedHashSet ,內有三個元素,使用 spliterator 取得元素,等同於 iterator.hasNext 和 iterator.next。
@Test
public void spliterator() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Spliterator<Fruit> sit = set.spliterator();
sit.tryAdvance(e -> System.out.println(e));
System.out.println("----------");
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
sit = set.spliterator();
while (sit.tryAdvance(e -> System.out.println(e))) {
}
}
{"name":"Longan","quantity":1.0,"type":1}
----------
{"name":"Tomato","quantity":2.0,"type":1}
{"name":"Pear","quantity":3.0,"type":1}
----------
{"name":"Longan","quantity":1.0,"type":1}
{"name":"Tomato","quantity":2.0,"type":1}
{"name":"Pear","quantity":3.0,"type":1}
trySplit
LinkedHashSet TrySplit in Java 建立一個 LinkedHashSet ,內有三個元素,使用 spliterator 取得元素,trySplit 將目前的拆分器分為兩個新的拆分器並行處理。
@Test
public void trySplit() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Spliterator<Fruit> sit = set.spliterator();
Spliterator<Fruit> sit2 = sit.trySplit();
System.out.println(sit.getExactSizeIfKnown());
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
System.out.println(sit2.getExactSizeIfKnown());
sit2.forEachRemaining(e -> System.out.println(e));
}
0
----------
3
{"name":"Longan","quantity":1.0,"type":1}
{"name":"Tomato","quantity":2.0,"type":1}
{"name":"Pear","quantity":3.0,"type":1}
toArray
LinkedHashSet in Java 建立一個 LinkedHashSet ,內有三個元素,轉換成 String 陣列。
@Test
public void toArray() {
int expectedSize = 3;
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Fruit[] array = set.toArray(new Fruit[0]);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[{"name":"Longan","quantity":1.0,"type":1}, {"name":"Tomato","quantity":2.0,"type":1}, {"name":"Pear","quantity":3.0,"type":1}]
streamToArray
LinkedHashSet in Java 建立一個 LinkedHashSet ,內有三個元素,使用 Stream ,轉換成 String 陣列。
@Test
public void streamToArray() {
int expectedSize = 3;
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Fruit[] array = set.stream().toArray(Fruit[]::new);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[{"name":"Longan","quantity":1.0,"type":1}, {"name":"Tomato","quantity":2.0,"type":1}, {"name":"Pear","quantity":3.0,"type":1}]
LinkedHashSetWithExamplesTest.java
LinkedHashSet in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.set.linkedhashset;
import static org.junit.Assert.*;
import java.util.LinkedHashSet;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.Spliterator;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class LinkedHashSetWithExamplesTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Fruit)) {
return false;
}
if (this == object) {
return true;
}
Fruit other = (Fruit) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
@Test
public void forEach() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
set.forEach(System.out::println);
}
@Test
public void forEachRemaining() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Iterator<Fruit> it = set.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
@Test
public void iterator() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Iterator<Fruit> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void spliterator() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Spliterator<Fruit> sit = set.spliterator();
sit.tryAdvance(e -> System.out.println(e));
System.out.println("----------");
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
sit = set.spliterator();
while (sit.tryAdvance(e -> System.out.println(e))) {
}
}
@Test
public void trySplit() {
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Spliterator<Fruit> sit = set.spliterator();
Spliterator<Fruit> sit2 = sit.trySplit();
System.out.println(sit.getExactSizeIfKnown());
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
System.out.println(sit2.getExactSizeIfKnown());
sit2.forEachRemaining(e -> System.out.println(e));
}
@Test
public void toArray() {
int expectedSize = 3;
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Fruit[] array = set.toArray(new Fruit[0]);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
@Test
public void streamToArray() {
int expectedSize = 3;
Set<Fruit> set = new LinkedHashSet<>();
set.add(new Fruit("Longan", 1, 1));
set.add(new Fruit("Tomato", 2, 1));
set.add(new Fruit("Pear", 3, 1));
Fruit[] array = set.stream().toArray(Fruit[]::new);
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
}
心得分享
LinkedHashSet Java 除了傳統的 iterator 循環訪問,也有 forEash 的 lambda 表示式,可以讓程式碼更為簡潔, LinkedHashSet in Java 提供了幾種 LinkedHashSet 常見方法的操作範例,像是:拆分器、陣列轉換等各種單元測試。