Java Set 总结

总结一下 Set 在使用时需要的注意事项:

 

Set (interface): 为了实现唯一性,需要实现 equals 方法

 

HashSet : 实现 hashCode 方法

TreeSet: 实现 Comparable 接口,实现compareTo方法

LinkedHashSet: 实现 hashCode方法

 

结果:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[5, 6, 2, 4, 7, 7, 6, 7, 9, 1, 1, 5, 6, 0, 8, 2, 4, 5, 0, 1, 4, 8, 3, 2, 8, 3, 9, 9, 3, 0]
[4, 0, 1, 1, 2, 8, 8, 2, 5, 9, 2, 6, 0, 7, 9, 7, 3, 4, 9, 0, 6, 6, 8, 5, 4, 7, 3, 3, 1, 5]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
SetType cannot be cast to java.lang.Comparable
HashType cannot be cast to java.lang.Comparable

分析:

前三行,唯一

中四行,失败,不唯一。 原因是 hashCode

后两行, 失败,异常。原因,未实现 Comparable 接口

 

 

import java.lang.reflect.InvocationTargetException;
import java.util.*;

// 简单对象 实现 equals
class SetType {
	int i;
	public SetType(int n) {	i=n;	}
	public boolean equals( Object o )
	{
		if(o!=null && o instanceof SetType)
		{
			return i==((SetType)o).i;
		}
		return false;
	}
	public String toString()
	{	return Integer.toString(i);	}
}

// Hash 对象 ,实现了 hashCode
class HashType extends SetType
{
	public HashType(int n)
	{	super(n);	}
	public int hashCode()
	{
			return i;
	}
}

// Tree对象,实现 compareTo 对象
class TreeType extends SetType implements Comparable<TreeType>
{
	public TreeType(int n){	 super(n);	}	
	@Override
	public int compareTo(TreeType o) {
		return i>o.i?-1:( i==o.i?0:1 );
	}
}

public class TypeForSets
{
	// 填充
	static<T>  Set<T> fill (Set<T> set, Class<T> type) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
	{
		for(int i=0;i<10;i++)
			set.add( type.getConstructor(int.class).newInstance(i) );
		return set;
	}
	// 测试
	static <T> void test(Set<T> set, Class<T> type) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
	{
		fill(set,type);
		fill(set,type);
		fill(set,type);
		System.out.println(set);
	}
	
	public static void main(String [] args) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
	{
		// 这些事符合规范的Set使用
	    test(new HashSet<HashType>(), HashType.class);
	    test(new LinkedHashSet<HashType>(), HashType.class);
	    test(new TreeSet<TreeType>(), TreeType.class);
		
	 // Things that don't work:
	    // SetType,TreeType对象 未实现hashCode
	    test(new HashSet<SetType>(), SetType.class);
	    test(new HashSet<TreeType>(), TreeType.class);
	    test(new LinkedHashSet<SetType>(), SetType.class);
	    test(new LinkedHashSet<TreeType>(), TreeType.class);
	    
	    // SetType,HashType 未实现 Comparable 接口
	    try {
	      test(new TreeSet<SetType>(), SetType.class);
	    } catch(Exception e) {
	      System.out.println(e.getMessage());
	    }
	    try {
	      test(new TreeSet<HashType>(), HashType.class);
	    } catch(Exception e) {
	      System.out.println(e.getMessage());
	    }

	}
}

 

计及风电并网运行的微电网及集群电动汽车综合需求侧响应的优化调度策略研究(Matlab代码实现)内容概要:本文研究了计及风电并网运行的微电网及集群电动汽车综合需求侧响应的优化调度策略,并提供了基于Matlab的代码实现。研究聚焦于在高渗透率可再生能源接入背景下,如何协调微电网内部分布式电源、储能系统与大规模电动汽车充电负荷之间的互动关系,通过引入需求侧响应机制,建立多目标优化调度模型,实现系统运行成本最小化、可再生能源消纳最大化以及电网负荷曲线的削峰填谷。文中详细阐述了风电出力不确定性处理、电动汽车集群充放电行为建模、电价型与激励型需求响应机制设计以及优化求解算法的应用。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事新能源、微电网、电动汽车等领域技术研发的工程师。; 使用场景及目标:①用于复现相关硕士论文研究成果,深入理解含高比例风电的微电网优化调度建模方法;②为开展电动汽车参与电网互动(V2G)、需求侧响应等课题提供仿真平台和技术参考;③适用于电力系统优化、能源互联网、综合能源系统等相关领域的教学与科研项目开发。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注模型构建逻辑与算法实现细节,同时可参考文档中提及的其他相关案例(如储能优化、负荷预测等),以拓宽研究视野并促进交叉创新。
### Java 中 `Set` 数据结构的使用方法及示例 #### 什么是 `Set` `Set` 是 Java 集合框架中的一个重要接口,它继承自 `Collection` 接口。其主要特点是不允许存储重复元素[^1]。 --- #### 创建 `Set` 的方式 可以通过以下几种常见的方式创建 `Set` 实例: - **`HashSet`**: 基于哈希表实现,不保证元素顺序。 - **`LinkedHashSet`**: 继承自 `HashSet`,通过链表维护插入顺序。 - **`TreeSet`**: 提供有序集合功能,基于红黑树实现。 以下是创建 `Set` 的基本语法: ```java import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Apple"); set.add("Banana"); set.add("Orange"); System.out.println(set); } } ``` 上述代码展示了如何创建一个简单的 `HashSet` 并向其中添加元素。 --- #### 集合操作 ##### 1. 添加元素到 `Set` 使用 `add()` 方法将单个元素加入集合中。如果尝试添加已存在的元素,则不会改变集合状态,并返回 `false` 表明未成功添加新元素。 ```java set.add("Mango"); // 成功添加 boolean isAdded = set.add("Apple"); // 返回 false,因为 "Apple" 已存在 System.out.println(isAdded); // 输出 false ``` ##### 2. 删除元素 利用 `remove()` 方法删除指定元素;若不存在该元素则无任何影响。 ```java set.remove("Banana"); System.out.println(set.contains("Banana")); // 输出 false ``` ##### 3. 清空集合 调用 `clear()` 方法可清空整个集合内的所有数据项。 ```java set.clear(); System.out.println(set.isEmpty()); // true ``` --- #### 高级集合运算 ##### 1. 并集 (`Union`) 两个集合 A 和 B 合并后的结果即为它们的并集。可通过 `addAll()` 方法完成此操作[^5]。 ```java Set<Integer> setA = new HashSet<>(Arrays.asList(1, 2, 3)); Set<Integer> setB = new HashSet<>(Arrays.asList(3, 4, 5)); setA.addAll(setB); System.out.println(setA); // 输出 [1, 2, 3, 4, 5] ``` ##### 2. 交集 (`Intersection`) 获取两集合中共有的部分作为新的子集。这能借助 `retainAll()` 函数达成目标[^4]。 ```java Set<Integer> setC = new HashSet<>(Arrays.asList(1, 2, 3)); Set<Integer> setD = new HashSet<>(Arrays.asList(3, 4, 5)); setC.retainAll(setD); System.out.println(setC); // 输出 [3] ``` ##### 3. 差集 (`Difference`) 找出属于某一方却不隶属于另一方的数据成员构成差集。这里采用的是 `removeAll()` 技术手段来处理差异情况。 ```java Set<Integer> setE = new HashSet<>(Arrays.asList(1, 2, 3)); Set<Integer> setF = new HashSet<>(Arrays.asList(3, 4, 5)); setE.removeAll(setF); System.out.println(setE); // 输出 [1, 2] ``` --- #### 迭代访问 `Set` 元素 由于 `Set` 不支持索引访问,因此通常会配合增强型 `for-each` 循环或者迭代器来进行遍历。 ```java for (String fruit : set) { System.out.println(fruit); } Iterator<String> iterator = set.iterator(); while (iterator.hasNext()) { String item = iterator.next(); System.out.println(item); } ``` 注意,在遍历时修改集合可能会引发并发修改异常(Concurrent Modification Exception),除非使用专门设计的支持此类场景的方法或工具类[^2]。 --- #### 总结 以上介绍了有关 Java 编程语言里 `Set` 类型的基础概念及其典型应用场景实例说明。掌握这些知识点有助于开发者更高效地管理和操控去重需求下的对象群体管理任务。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值