HashSet

HashSet的基本用法

Posted by Ruizhi Ma on July 8, 2019

TestHashMap类

package cn.sxt.collection;

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

/*
 * 测试HashSet的基本用法
 * Set没有顺序,不可重复
 * List有顺序,可重复
 */

public class TestHashSet {

	public static void main(String[] args) {
		Set<String> set1 = new HashSet<String>();
		
		set1.add("sober");
		set1.add("nora");
		set1.add("sober");
		System.out.println(set1);
		set1.remove("sober");
		System.out.println(set1);
		
		Set<String> set2 = new HashSet<String>();
		set2.add("马睿智");
		set2.add("许诺");
		set2.addAll(set1);
		System.out.println(set2);
	}

}

运行结果:
[nora, sober]
[nora]
[许诺, nora, 马睿智]

注意: HashSet其实就是通过HashMap实现的,只不过HashSet的value存为了HashMap的key


0 comments
Anonymous
Error: Bad credentials.
Markdown is supported

Be the first person to leave a comment!