Java TreeSet Methods - Java 147

Java TreeSet Methods – Java 147

Java TreeSet Methods

是一個有序集合,存儲唯一元素,不會有重複元素,不保留元素的插入順序,而是將元素排序,可提供自訂 Comparator 依照元素排序,介紹常見的 add 、 get 、 clear 、 size 等方法,將資料存儲在集合中, TreeSet Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- set
       |                   +- treeset
       |                       +- TreeSetMethodsTest.java   

單元測試

TreeSet Methods Java 提供新增、取得、修改、刪除等操作 Set 中的元素。

add

TreeSet Methods Java 建立一個 TreeSet ,增加三個元素。

	@Test
	public void add() {
		int expectedSize = 3;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
[Longan, Pear, Tomato]

remove

TreeSet Methods in Java 建立一個 TreeSet ,內有三個元素,刪除指定元素。

	@Test
	public void remove() {
		int expectedSize = 2;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		set.remove("Longan");
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
{[Pear, Tomato]

removeAll

TreeSet Methods in Java 建立一個 TreeSet ,內有三個元素,刪除來自另一個 Set 中的元素。

	@Test
	public void removeAll() {
		int expectedSize = 1;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");

		Set<String> set2 = new TreeSet<String>();
		set2.add("Longan");
		set2.add("Tomato");
		set2.add("Mango");
		set.removeAll(set2);
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
[Pear]

clear

TreeSet Methods in Java 建立一個 TreeSet ,內有三個元素,刪除所有元素。

	@Test
	public void clear() {
		int expectedSize = 0;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		set.clear();
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
	
{}

size

TreeSet Methods in Java 建立一個 TreeSet ,內有三個元素,取得長度。

	@Test
	public void size() {
		int expectedSize = 3;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set.size());
		assertEquals(expectedSize, set.size());
	}
{{"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}
{{"name":"Grape","quantity":1.0,"type":1}=10, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}

remove

TreeMap Methods in Java 建立一個 TreeMap ,內有三個元素,刪除指定 Key 元素。

	@Test
	public void remove() {
		int expectedSize = 2;
		Map<Fruit, Integer> map = new TreeMap<>();
		Fruit key = new Fruit("Grape", 1, 1);
		map.put(key, 1);
		map.put(new Fruit("Lemon", 3, 1), 3);
		map.put(new Fruit("Kiwifruit", 2, 1), 2);
		map.remove(key);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
3

addAll

TreeSet Methods in Java 建立兩個 TreeSet ,內各有三個元素,合併成為一個 Set 。

	@Test
	public void addAll() {
		int expectedSize = 6;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");

		Set<String> set2 = new TreeSet<String>();
		set2.add("Grape");
		set2.add("Lemon");
		set2.add("Mango");

		set.addAll(set2);
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}
[Grape, Lemon, Longan, Mango, Pear, Tomato]

isEmpty

建立一個 TreeSet ,檢查是否為空 Set 。

	@Test
	public void isEmpty() {
		Set<String> set = new TreeSet<String>();
		System.out.println(set.isEmpty());
		assertTrue(set.isEmpty());
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set.isEmpty());
		assertFalse(set.isEmpty());
	}
true
false

TreeSetMethodsTest.java

TreeSet Methods in Java 新增單元測試,驗證 TreeSet Functions in Java 是否符合預期。

package org.ruoxue.java_147.set.treeset;

import static org.junit.Assert.*;

import java.util.TreeSet;
import java.util.Set;

import org.junit.Test;

public class TreeSetMethodsTest {

	@Test
	public void add() {
		int expectedSize = 3;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void remove() {
		int expectedSize = 2;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		set.remove("Longan");
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void removeAll() {
		int expectedSize = 1;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");

		Set<String> set2 = new TreeSet<String>();
		set2.add("Longan");
		set2.add("Tomato");
		set2.add("Mango");
		set.removeAll(set2);
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void clear() {
		int expectedSize = 0;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		set.clear();
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void size() {
		int expectedSize = 3;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set.size());
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void addAll() {
		int expectedSize = 6;
		Set<String> set = new TreeSet<String>();
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");

		Set<String> set2 = new TreeSet<String>();
		set2.add("Grape");
		set2.add("Lemon");
		set2.add("Mango");

		set.addAll(set2);
		System.out.println(set);
		assertEquals(expectedSize, set.size());
	}

	@Test
	public void isEmpty() {
		Set<String> set = new TreeSet<String>();
		System.out.println(set.isEmpty());
		assertTrue(set.isEmpty());
		set.add("Longan");
		set.add("Tomato");
		set.add("Pear");
		System.out.println(set.isEmpty());
		assertFalse(set.isEmpty());
	}
}

心得分享

TreeSet Functions in Java 實作集合框架的 NavigableSet 接口,提供依照元素排序,熟悉 TreeSet Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。

發佈留言