Scala ing and metadata

Source: Internet
Author: User
Ing
Import Scala. collection. mutableobject helloworld {def main (ARGs: array [String]) {Val scores = map ("Alice"-> 10, "Bob"-> 33) // construct an immutable map [String, int] Val scores2 = Scala. collection. mutable. map ("Alice"-> 10, "Bob"-> 33) // construct a variable Val ing Val scores3 = new mutable. hashmap [String, int] // construct an empty ing, select a ing implementation and give the type parameter Val scores4 = map ("Alice", 10), ("Bob ", 33) // another method for defining mappings Val bobscore = scores4 ("Bob") // similar to scores4.get ("Bob") in Java "); val bobscore2 = scores4.getorelse ("Bob", 0) // If the ing contains the key "Bob", the corresponding value is returned; otherwise, 0 // ing is returned. A call like get (key) returns an option object, either some (the value corresponding to the key) or NONE // update the value scores ("Bob") in the ing ") = 90 // The scores ("Fred") value corresponding to the update key "Bob ") = 88 // Add a new key/value pair to scores + = ("Bob"-> 20, "Fred"-> 30) // Add multiple links in batches. Scores-= "Alice" // remove the values corresponding to the Alice key. // If the ing is immutable, change the value, you can use the following method to VaR newscores = scores + ("Bob"-> 1, "Fred"-> 7) // return the updated new ing newscores = scores-"Alice" // return the new ing after removing Alice }}
Iterative ing for (K, v) <- ing) processes K and V. If you need to access the key or value, you can use the keyset and values methods, values returns an iterable to reverse a ing ----- That is, the location of the exchange key and value ----- You can use: for (K, v) <- ing) yield (v, k) tuples are a set of key-value pairs. Dual is the simplest form of tuple-tuples are the aggregation of different types of values. If you have tuples, Val t = (1, 3.14, "Fred"), you can use Val second = T. _ 2 to access the second element, that is, 3.14. Note that the tuples of the tuples start from 1 rather than 0. (This is one of the disgusting parts of scala. It is full of variables, not regular enough, and not uniform enough. Inaction adds a lot of memory burden to developers and worries that Scala will not become as popular as Java, the JVM function language, which is better than Scala in the future, may be used to return more than one value for a function, "New York ". partition (_. isupper) // output dual, which is a character that meets a certain condition and does not meet a certain condition.
Object helloworld {def main (ARGs: array [String]) {Val symbols = array ("<", "-", ">") Val counts = array (2, 10, 2) Val pairs = symbols.zip (counts) // use the zip method to bind multiple values so that they can be processed together for (S, N) <-pairs) {console. print (S * n) // output <---------- >}println () // The tomap method can be used to convert a dual set into a Val ing Val keys = array ("1 ", "2", "3", "4", "5") Val values = array ("strike", "Inverted", "kiss", "day ", "school") Val map = keys.zip (values ). tomap. toarray. sorted // convert the ing to an array and sort it. Then, you can output the sorted result for (K, v) <-map) {println ("key: "+ K +", value: "+ V )}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.