In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the commonly used List list operations in Scala, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Create a list
Scala > val days = List ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
Days: List [String] = List (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
Create an empty list
Scala > val l = Nil
L: scala.collection.immutable.Nil.type = List ()
Scala > val l = List ()
L: List [Nothing] = List ()
Create a list with a string
Scala > val l = "Hello":: "Hi":: "Hah":: "WOW":: "WOOW":: Nil
L: List [String] = List (Hello, Hi, Hah, WOW, WOOW)
Create a new list with ":" overlay
Scala > val wow = l: List ("WOOOW", "WOOOOW")
Wow: List [String] = List (Hello, Hi, Hah, WOW, WOOW, WOOOW, WOOOOW)
Get list values through index
Scala > l (3)
Res0: String = WOW
Gets the number of elements with a value length of 3
Scala > l.count (s = > s.length = = 3)
Res1: Int = 2
Returns a new list of the first two elements of l
Scala > l.drop (2)
Res2: List [String] = List (Hah, WOW, WOOW)
Scala > l
Res3: List [String] = List (Hello, Hi, Hah, WOW, WOOW)
Returns a new list of the two elements after l is removed
Scala > l.dropRight (2)
Res5: List [String] = List (Hello, Hi, Hah)
Scala > l
Res6: List [String] = List (Hello, Hi, Hah, WOW, WOOW)
Determine whether l has an element.
Scala > l.exists (s = > s = = "Hah")
Res7: Boolean = true
Filter out elements of length 3
Scala > l.filter (s = > s.length = = 3)
Res8: List [String] = List (Hah, WOW)
Determine whether all elements start with "H"
Scala > l.forall (s = > s.startsWith ("H"))
Res10: Boolean = false
Determine whether all elements end in "H"
Scala > l.forall (s = > s.endsWith ("W"))
Res11: Boolean = false
Print each element
Scala > l.foreach (s = > print (s +'))
Hello Hi Hah WOW WOOW
Take out the first element
Scala > l.head
Res17: String = Hello
Take out the last element
Scala > l.last
Res20: String = WOOW
Remove the last element and generate a new list
Scala > l.init
Res18: List [String] = List (Hello, Hi, Hah, WOW)
Remove the first element and generate a new list
Scala > l.tail
Res49: List [String] = List (Hi, Hah, WOW, WOOW)
Determine whether the list is empty
Scala > l.isEmpty
Res19: Boolean = false
Get the list length
Scala > l.length
Res21: Int = 5
Modify each element, then reverse each element to form a new list
Scala > l.map (s = > {val S1 = s + "- 01"; s1.reverse})
Res29: List [String] = List (10-olleH, 10-iH, 10-haH, 10-WOW, 10-WOOW)
Generate a string separated by commas
Scala > l.mkString (",")
Res30: String = Hello, Hi, Hah, WOW, WOOW
Generate a new list in reverse order
Scala > l.reverse
Res41: List [String] = List (WOOW, WOW, Hah, Hi, Hello)
Sort by alphabetical increment
Scala > l.sortWith (_ .compareto (_) < 0)
Res48: List [String] = List (Hah, Hello, Hi, WOOW, WOW)
These are the common List list operations in Scala, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.