首页 > 其他分享 > Set

Set

时间:2023-02-14 00:33:21浏览次数:31  
标签:Set HashSet add set set1 new

1. Set

1.1, Set集合特性

1、Set 接口不保证将元素按照添加它们时的顺序存储;

2、Set 接口中不存在重复元素;

3、Set 接口支持使用多种方式存储,如哈希表、二叉搜索树等;

4、Set 接口支持批量操作,如添加 Collection 中的所有元素;

5、Set 接口中不支持存储 null 元素;

1.2 , Set的方法

1, boolean add(E e)

向Set中添加元素

Set<String> set = new HashSet<String>();
set.add("a");

2, boolean addAll(Collection<? extends E> c)

接收一个Collection类型的参数,将参数内的所有元素添加到当前Set集合中,如果满足添加条件(指根据Set集合当前状态和Collection类型参数中元素去重等),则返回true,反之返回false,该方法不适用于LinkedHashSet(无值会变动)

Set<String> set1 = new HashSet<String>();
set1.add("a");
set1.add("b");
set1.add("c");
    
List<String> list1 = new ArrayList<String>();
list1.add("d");
list1.add("e");
list1.add("f");
    
boolean result = set1.addAll(list1);
System.out.println(result); // 输出true
System.out.println(set1);//输出 [a, b, c, d, e, f]

3.clear()

清空Set

Set<String> set = new HashSet<String>();
set.add("a");
set.clear();

4. boolean contains(Object o)

检查Set中是否存在指定元素

Set<String> set = new HashSet<String>();
set.add("a");
if (set.contains("a")){
    System.out.println("Set中存在元素a");
} 

**5.boolean containsAll(Collection<?> c) **

如果此集合包含指定集合的所有元素,则返回 true

Set<String> countriesSet = new HashSet<>();
//Add element in HashSet
countriesSet.add("India");
countriesSet.add("Japan");
countriesSet.add("Australia");
countriesSet.add("USA");
countriesSet.add("China");
System.out.println("Set of countries : " + countriesSet);
//new Set
Set<String> newSet = new HashSet<>();
newSet.add("India");
newSet.add("Japan");
newSet.add("Australia");
System.out.println("New Set of countries : " + newSet);
//Check if both sets are same
if(countriesSet.containsAll(newSet))
    System.out.println("countriesSet contains all elements of newSet");
else
    System.out.println("countriesSet is not containing all elements of newSet");

6.boolean equals(Object o)

用于比较指定的对象和此集合是否相等,集合元素地址相同,或者集合中元素个数及元素值完全相同则俩个集合相等

7, boolean isEmpty()

如果此集合不包含任何元素,则返回 true

8.iterator()

返回此set中元素的迭代器

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

class SetIteratorDemo {
	public static void main(String[] args) {
		Set<String> set = new HashSet<>();
		set.add("Java");
		set.add("C");
		set.add("C++");
		// 通过iterator遍历set
		Iterator<String> it = set.iterator();
		while (it.hasNext()) {
			String str = it.next();
			System.out.println(str);
		}
	}
}
//输出结果
Java
C++
C

9, boolean remove(Object o)

从Set中移除指定元素

Set<String> set = new HashSet<String>();
set.add("a");
set.remove("a");

10,boolean removeAll(Collection<?> c)

从此集合中删除指定集合中包含的所有元素(可选操作)。

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

class SetDemo {
	public static void main(String[] args) {
		// 创建HashSet对象
		Set<String> set1 = new HashSet<String>();
		set1.add("John");
		set1.add("Ralph");
		set1.add("Smith");
		set1.add("Tom");
		set1.add("Harry");

		// 创建第二个HashSet对象
		Set<String> set2 = new HashSet<String>();
		set2.add("John");
		set2.add("Juliet");
		set2.add("Harry");
		set2.add("Tom");

		// 从set1中移除未包含在set2中的元素
		set1.removeAll(set2);
		System.out.println("set1集合中的元素:" + set1);
	}
}
//输出结果
set1集合中的元素:[Ralph, Smith]

11, boolean retainAll(Collection<?> c)交集

仅保留此集合中包含在指定集合中的元素(可选操作)。

相当于求俩个集合的交集

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

class SetRetainAllDemo {
	public static void main(String[] args) {
		Set<String> set1 = new HashSet<>();
		set1.add("a");
		set1.add("b");
		set1.add("c");

		Set<String> set2 = new HashSet<>();
		set2.add("d");
		set2.add("e");
		set2.add("a");
		set2.add("f");
		// 交集:retainAll(Collection c)
		set1.retainAll(set2);
		System.out.println("交集是:" + set1); // 输出 {a}
	}
}

12. int size();

返回此集合中的元素个数(长度)

13, toArray()

Set的toArray方法用于将Set转换为Object类型或者指定类型的数组的数组。该方法接收一个指定类型的数组作为参数,返回该Set转换后数组类型。

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

class SetToArray {
	public static void main(String[]args){
		Set<String> set = new HashSet<>();
		set.add("java");
		set.add("c");
		set.add("c++");
		set.add("Android");
		String[] strings = set.toArray(new String[set.size()]);
		for (String string:strings){
			System.out.println(string);
		}
	}
}

//输出结果:
c
Android
java
c++

标签:Set,HashSet,add,set,set1,new
From: https://www.cnblogs.com/zouzhibin/p/17118382.html

相关文章