set scala添加元素_如何从Scala中的Set获取第一个元素?

set scala添加元素

Scala集 (Scala Set)

A set in Scala is a collection of unique elements i.e. duplicate elements are not allowed in the Set.

Scala中的集合是唯一元素的集合,即,集合中不允许重复元素。

Example:

例:

    Set(10, 3, 213, 56, 9, 82)

访问集合的第一个元素 (Accessing First Element of Set)

In the Scala programming language, to access the first element of the Set, there are multiple methods defined that can accomplish the task easily.

在Scala编程语言中,要访问Set的第一个元素,定义了多种方法可以轻松完成任务。

1)take()方法 (1) take() method)

The take() method in Scala is used to return the Set of elements up to the specified length from the given Set. So, passing 1 as a parameter to the take() method will return a set with the first element only.

Scala中take()方法用于从给定Set中返回指定长度的元素Set。 因此,将1作为参数传递给take()方法将仅返回带有第一个元素的集合。

Program:

程序:

object MyClass {
    def main(args: Array[String]) {
        val bike = Set("Pulsar 150" , "Thunderbird 350", "Ninja 300", "Harley Davidson street 750")
        printf("all my bikes are : ")
        println(bike)
        println("My first bike was "+bike.take(1))
    }
}

Output

输出量

all my bikes are : Set(Pulsar 150, Thunderbird 350, Ninja 300, Harley Davidson street 750)
My first bike was Set(Pulsar 150)

Here the output is correct i.e. we wanted the first element and we got in too. But in a program, we need to change this set to an element so that it can be used in the code. So, there are other methods too that will do this job for us.

这里的输出是正确的,即我们想要第一个元素,我们也进入了。 但是在程序中,我们需要将此集合更改为一个元素,以便可以在代码中使用它。 因此,还有其他方法可以为我们完成这项工作。

2)头法 (2) head method)

The head method in the Scala is defined to return the first element of the set which call the method.

Scala中head方法定义为返回集合中调用该方法的第一个元素。

Syntax:

句法:

    Set_name.take;

The method does not accept any parameter, it returns the first value of the set.

该方法不接受任何参数,它返回集合的第一个值。

Program:

程序:

object MyClass {
    def main(args: Array[String]) {
        val bike = Set("Pulsar 150" , "Thunderbird 350", "Ninja 300", "Harley Davidson street 750")
        printf("all my bikes are : ")
        println(bike)
        println("My first bike was "+bike.head)
    }
}

Output

输出量

all my bikes are : Set(Pulsar 150, Thunderbird 350, Ninja 300, Harley Davidson street 750)
My first bike was Pulsar 150

3)headOption (3) headOption)

The headOption in Scala is also used to return the first element of the set that calls it.

Scala中headOption也用于返回集合中调用它的第一个元素。

Syntax:

句法:

    set_name.headOption

Program:

程序:

object MyClass {    
    def main(args: Array[String]) {
        val bike = Set("Pulsar 150" , "Thunderbird 350", "Ninja 300", "Harley Davidson street 750")
        printf("all my bikes are : ")
        println(bike)
        println("My first bike was "+bike.headOption)
    }
}

Output

输出量

all my bikes are : Set(Pulsar 150, Thunderbird 350, Ninja 300, Harley Davidson street 750)
My first bike was Some(Pulsar 150)


翻译自: https://www.includehelp.com/scala/how-to-get-the-first-element-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、付费专栏及课程。

余额充值