1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAVA java.util.HashSet/LinkedHashSet/TreeSet

Last updated at Posted at 2015-11-27

Setの基本特性

・それぞれの要素には、重複が許されない
・それぞれの要素には、基本的に順序関係がない
・set()やget()がない
・セットの要素には順序がない。
・拡張for文やイテレータを使ってセットの中身を1つづつ取り出す場合、どのような順序で要素が取り出せるかは一切確約されていない

■TestHashSet.java

import java.util.HashSet;
import java.util.Set;

public class TestHashSet {
	public static void main(String[] args) {
		Set<String> fish = new HashSet<String>();
		fish.add("black rockfishメバル");
		fish.add("yellowtailブリ");
		fish.add("globefishフグ");
		fish.add("codタラ");
		fish.add("porcupinefishハリセンボン");
		fish.add("yellowtailブリ");
		fish.add("yellowtailブリ");
		System.out.println("魚は" + fish.size() + "種類");
	}

}

■TestHashSet.java 実行結果
魚は5種類

HashSetは、1つづつ取り出す場合の順序は不明

■TestHashSet.java

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class TestHashSet {
	public static void main(String[] args) {
		Set<String> fish = new HashSet<String>();
		fish.add("black rockfishメバル");
		fish.add("yellowtailブリ");
		fish.add("globefishフグ");
		fish.add("codタラ");
		fish.add("porcupinefishハリセンボン");
		fish.add("yellowtailブリ");
		fish.add("yellowtailブリ");
		System.out.println("魚は" + fish.size() + "種類");

		Iterator<String> it = fish.iterator();
		while (it.hasNext()) {
			String e = it.next();
			System.out.println(e);
		}
		System.out.println("");
		System.out.println("拡張for文");
		for (String j : fish) {
			System.out.println(j);
		}
	}

}

■TestHashSet.java 実行結果
魚は5種類
black rockfishメバル
globefishフグ
porcupinefishハリセンボン
yellowtailブリ
codタラ

拡張for文
black rockfishメバル
globefishフグ
porcupinefishハリセンボン
yellowtailブリ
codタラ

LinkedHashSet

値を格納した順序に整列

■TestHashSet.java

import java.util.LinkedHashSet;
import java.util.Set;

public class TestLinkedHashSet {
	public static void main(String[] args) {
		Set<String> fish = new LinkedHashSet<String>();
		fish.add("black rockfishメバル");
		fish.add("yellowtailブリ");
		fish.add("globefishフグ");
		fish.add("codタラ");
		fish.add("porcupinefishハリセンボン");
		fish.add("yellowtailブリ");
		fish.add("yellowtailブリ");

		for (String m : fish) {
			System.out.println(m);
		}

	}

}

■TestHashSet.java 実行結果
black rockfishメバル
yellowtailブリ
globefishフグ
codタラ
porcupinefishハリセンボン

TreeSet

自然順序付けで整列
String型の複数の文字列をTreeSetに格納すると、「辞書順で取り出すことが出来る」

■TestTreeSet.java

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

public class TestTreeSet {
	public static void main(String[] args) {
		Set<String> fish = new TreeSet<String>();
		fish.add("black rockfishメバル");
		fish.add("yellowtailブリ");
		fish.add("globefishフグ");
		fish.add("porcupinefishハリセンボン");
		fish.add("yellowtailブリ");
		fish.add("black rockfishメバル");

		for (String s : fish) {
			System.out.println(s);
		}

	}

}

■TestTreeSet.java 実行結果
black rockfishメバル
globefishフグ
porcupinefishハリセンボン
yellowtailブリ

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?