Java集合-Set源码

简介

Set集合不包含重复的元素,相同的元素只保存一个,不包含相等的两个元素,Set至多只能包含一个NULL元素。Set的实现类都是基于Map来实现的,其中HashSet是通过HashMap来实现的,TreeSet是通过TreeMap实现的。

类图

这里写图片描述

遍历方式

public static void main(String[] args) {
    Set<String> set = new HashSet<>();
    set.add("Lions are the most mighty of all animals in Africa.");
    set.add("Those kids always have so much enthusiasm about studying.");
    set.add("Calligraphy is the art of beautiful handwriting.");
    // 迭代器
    Iterator<String> iterator = set.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
    System.out.println("-------------------------------------------");
    // 迭代器for循环
    for (iterator = set.iterator();iterator.hasNext();){
        System.out.println(iterator.next());
    }
    System.out.println("-------------------------------------------");
    // 加强型for循环
    for (String str : set) {
        System.out.println(str);
    }
}

源码

参照JDK1.8版本

Query Operations

//返回集合的大小
int size();
//集合为空,返回true,否则,返回false
boolean isEmpty();
//判断集合是否包含指定对象o
boolean contains(Object o);
//返回set集合的迭代器
Iterator<E> iterator();
//Returns an array containing all of the elements in this set.
Object[] toArray();
//Returns an array containing all of the elements in this set;
<T> T[] toArray(T[] a);

Modification Operations

//添加一个元素
boolean add(E e);
//删除一个元素
boolean remove(Object o);

Bulk Operations

//Returns true if this set contains all of the elements of the //specified collection. 
boolean containsAll(Collection<?> c);
//将指定集合的全部元素添加到当前集合中
boolean addAll(Collection<? extends E> c);
/**
* In other words, removes from this set all of its elements 
* that are not contained in the specified collection.  
**/
boolean retainAll(Collection<?> c);
/**
* Removes from this set all of its elements that are contained
* in the specified collection (optional operation).  
*/
boolean removeAll(Collection<?> c);
/**
* Removes all of the elements from this set (optional operation).
*/
void clear();

Comparison and hashing

//判断是否相等
boolean equals(Object o);
//Returns the hash code value for this set. 
int hashCode();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
粗糙集属性约简算法(Rough Set Attribute Reduction Algorithm)是一种常用的数据挖掘算法,用于在数据集中找到最重要的属性,从而减少数据维度和提高数据挖掘效率。以下是一个简单的粗糙集属性约简算法的Java源码示例: ```java import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class RoughSetAttributeReduction { public static Set<String> attributeReduction(List<Set<String>> data) { Set<String> attributes = new HashSet<>(data.get(0)); // 获取属性集合 Set<String> reduct = new HashSet<>(attributes); // 初始化约简集合 // 迭代计算 boolean change = true; while (change) { change = false; // 计算属性依赖关系 Set<Set<String>> dependency = calculateDependency(data, reduct); // 检查冗余属性,删除无关属性 Set<String> redundant = new HashSet<>(); for (Set<String> depend : dependency) { for (String attr : attributes) { if (!reduct.contains(attr) && !depend.contains(attr)) { redundant.add(attr); break; } } } if (!redundant.isEmpty()) { reduct.removeAll(redundant); change = true; } } return reduct; } // 计算属性依赖关系 private static Set<Set<String>> calculateDependency(List<Set<String>> data, Set<String> reduct) { Set<Set<String>> dependency = new HashSet<>(); for (Set<String> instance : data) { Set<String> depend = new HashSet<>(); for (String attr : instance) { if (reduct.contains(attr)) { depend.add(attr); } } dependency.add(depend); } return dependency; } public static void main(String[] args) { List<Set<String>> data = new ArrayList<>(); Set<String> instance1 = new HashSet<>(); instance1.add("attribute1"); instance1.add("attribute2"); instance1.add("attribute3"); data.add(instance1); Set<String> instance2 = new HashSet<>(); instance2.add("attribute1"); instance2.add("attribute4"); instance2.add("attribute5"); data.add(instance2); Set<String> instance3 = new HashSet<>(); instance3.add("attribute2"); instance3.add("attribute3"); instance3.add("attribute4"); data.add(instance3); Set<String> reduct = attributeReduction(data); System.out.println("Attribute Reduction: " + reduct); } } ``` 这段代码首先定义了一个`attributeReduction`方法来执行粗糙集属性约简算法。在算法中,通过迭代不断计算属性依赖关系,然后检查冗余属性并删除无关属性,直到不再发生变化。最后,返回约简后的属性集合。 在`main`方法中,我们定义了一个包含3个实例的数据集,并调用`attributeReduction`方法来获取属性约简结果。最终输出约简后的属性集合。 这只是一个简单的粗糙集属性约简算法的Java源码示例,实际应用中可能需要更多的优化和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值