set scala添加元素_如何从Scala中的Set中删除元素?

set scala添加元素

Scala集 (Scala Set)

In Scala, a Set is a collection of elements of the same type. All elements of the set are unique i.e. no elements are allowed. Sets can be mutable as well as immutable.

在Scala中,集合是相同类型元素的集合。 集合中的所有元素都是唯一的,即不允许任何元素。 集可以是可变的,也可以是不变的。

Example:

例:

    Set(1, 4, 5, 7, 12, 87, 213)

In Scala, you can remove elements from mutable as well as immutable sets. This operation is handled differently for both mutable as well as immutable sets.

在Scala中,您可以从可变集和不可变集中删除元素。 对于可变集和不可变集,此操作的处理方式有所不同。

1)从可变集中删除元素 (1) Deleting elements from the Mutable Set)

For deleting elements of a mutable set, we will use -= ,--=, retain, clear, remove.

为了删除可变集合的元素,我们将使用-= , -= , keep , clear和remove 。

-=Deletes single element from set.
--=Deletes multiple element from set.
retainDeletes multiple element based on a certain condition.
clearDeletes all elements of the set.
removeRemoves the specified element and return boolean value of operation done.
-= 从集合中删除单个元素。
-= 从集合中删除多个元素。
保留 根据特定条件删除多个元素。
明确 删除集合中的所有元素。
去掉 删除指定的元素并返回布尔值完成的操作。

Example 1: use of -= and --= methods

示例1:使用-=和-=方法

object MyClass {
    def main(args: Array[String]) {
        val set = scala.collection.mutable.Set(2, 56, 577,12 , 46, 19, 90 , 32, 75, 81)
        println("The set is "+set)
        set -= 2;
        println("After deletion of one element, the set is "+set)
        set --= List(577, 12, 19);
        println("After deletion of multiple elements, the set is "+set)
    }
}

Output

输出量

The set is HashSet(32, 81, 577, 2, 19, 56, 90, 75, 12, 46)
After deletion of one element, the set is HashSet(32, 81, 577, 19, 56, 90, 75, 12, 46)
After deletion of multiple elements, the set is HashSet(32, 81, 56, 90, 75, 46)

Example 2:

范例2:

object MyClass {
    def main(args: Array[String]) {
        val set = scala.collection.mutable.Set(2, 56, 577,12 , 46, 19, 90 , 32, 75, 81);
        println("The set is "+set)
        set.retain(_  >20);
        println("After deletion using retain, the set is "+set)
        set.remove(577)
        println("After deletion using remove, the set is "+set)
        set.clear()
        println("After deletion using clear, the set is "+set)
    }
}

Output

输出量

The set is HashSet(32, 81, 577, 2, 19, 56, 90, 75, 12, 46)
After deletion using retain, the set is HashSet(32, 81, 577, 56, 90, 75, 46)
After deletion using remove, the set is HashSet(32, 81, 56, 90, 75, 46)
After deletion using clear, the set is HashSet()

2)从不可变集中删除元素 (2) Deleting elements from the Immutable Sets )

Element in an immutable set cannot be changed. So, for performing deletion operation on these types of the set we need to create a new copy for every operation. -- and - operations are valid.

不变集中的元素无法更改。 因此,为了对这些类型的集合执行删除操作,我们需要为每个操作创建一个新副本。 -和-操作有效。

Example:

例:

object MyClass {
    def main(args: Array[String]) {
        val set = scala.collection.mutable.Set(2, 56, 577,12 , 46, 19, 90 , 32, 75, 81);
        println("The set is "+set)
        var set1 = set - 2;
        println("After deletion of one element, the set is "+set1)
        var set2 = set1 -- List(577, 12, 19);
        println("After deletion of multiple elements, the set is "+set2)
        var set3 = set2 - (81, 46)
        println("After deletion of multiple elements, the set is "+set2)
        var set4 = set3 -- Array(56, 90);
        println("After deletion of multiple elements, the set is "+set2)
    }
}

Output

输出量

The set is HashSet(32, 81, 577, 2, 19, 56, 90, 75, 12, 46)
After deletion of one element, the set is HashSet(32, 81, 577, 19, 56, 90, 75, 12, 46)
After deletion of multiple elements, the set is HashSet(32, 81, 56, 90, 75, 46)
After deletion of multiple elements, the set is HashSet(32, 81, 56, 90, 75, 46)
After deletion of multiple elements, the set is HashSet(32, 81, 56, 90, 75, 46)


翻译自: https://www.includehelp.com/scala/how-to-delete-elements-from-the-set-in-scala.aspx

set scala添加元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值