标签:
1. 设置一个映射,其中包含你想要的一些装备,以及它们的价格。然后构建另一个映射,采用同一组键,但在价格上打9折。import
scala.collection.JavaConversions.asScalaBuffer
import
scala.collection.mutable.Buffer
import
java.awt.datatransfer.
_
import
java.util.TimeZone
object
App{
def
main(args
:
Array[String]){
val
a
=
Map(
"Mac Mini"
->
4988
,
"MacBook Air"
->
6288
,
"MacBook Pro"
->
8488
)
println(a.mkString(
"["
,
", "
,
"]"
))
println(
"10% OFF"
)
val
b
=
for
( (k,v) <- a )
yield
(k, v *
0.9
)
println(b.mkString(
"["
,
", "
,
"]"
))
}
}
/*result
[Mac Mini -> 4988, MacBook Air -> 6288, MacBook Pro -> 8488]
10% OFF
[Mac Mini -> 4489.2, MacBook Air -> 5659.2, MacBook Pro -> 7639.2]
*/
object
App{
def
main(args
:
Array[String]){
val
map
=
countWord(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
countWord(filename
:
String)
=
{
var
words
=
Map[String, Int]()
val
in
=
new
java.util.Scanner(
new
java.io.File(
"myfile.txt"
))
while
(in.hasNext()){
var
key
=
in.next()
words +
=
(key -> (words.getOrElse(key,
0
) +
1
))
}
words
}
}
/*result
*/
object
App{
def
main(args
:
Array[String]){
val
map
=
countWord(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
countWord(filename
:
String)
=
{
var
words
=
scala.collection.immutable.SortedMap[String, Int]()
val
in
=
new
java.util.Scanner(
new
java.io.File(
"myfile.txt"
))
while
(in.hasNext()){
var
key
=
in.next()
words +
=
(key -> (words.getOrElse(key,
0
) +
1
))
}
words
}
}
/*result
*/
import scala.collection.JavaConversions.mapAsScalaMap object App{ def main(args : Array[String]){ val map = countWord( "myfile.txt" ) println(map.mkString( "[" , ", " , "]" )) } def countWord(filename : String) = { var words = new java.util.TreeMap[String, Int]() val in = new java.util.Scanner( new java.io.File( "myfile.txt" )) while (in.hasNext()){ var key = in.next() words + = (key -> (words.getOrElse(key, 0 ) + 1 )) } words } } /*result */
|
import
scala.collection.JavaConversions.mapAsScalaMap
object
App{
def
main(args
:
Array[String]){
val
map
=
weekday(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
weekday(filename
:
String)
=
{
var
weekdays
=
new
java.util.LinkedHashMap[String,Int]()
weekdays(
"Monday"
)
=
java.util.Calendar.MONDAY
weekdays(
"Tuesday"
)
=
java.util.Calendar.TUESDAY
weekdays(
"Wednesday"
)
=
java.util.Calendar.WEDNESDAY
weekdays(
"Thursday"
)
=
java.util.Calendar.THURSDAY
weekdays(
"Friday"
)
=
java.util.Calendar.FRIDAY
weekdays(
"Saturday"
)
=
java.util.Calendar.SATURDAY
weekdays(
"Sunday"
)
=
java.util.Calendar.SUNDAY
weekdays
}
}
/*result
[Monday -> 2, Tuesday -> 3, Wednesday -> 4, Thursday -> 5, Friday -> 6, Saturday -> 7, Sunday -> 1]
*/
import
scala.collection.JavaConversions.propertiesAsScalaMap
object
App{
def
main(args
:
Array[String]){
printSystemProperties()
}
def
printSystemProperties()
=
{
val
props
:
scala.collection.Map[String, String]
=
System.getProperties()
var
maxlen
=
0
for
((k,
_
) <- props)
if
(maxlen < k.length) maxlen
=
k.length
maxlen +
=
1
for
((k,v) <- props){
printf(
"%-"
+ maxlen.toString +
"s | %s\n"
, k, v)
}
}
}
/*result
java.vm.specification.version | 1.8
sun.java.command | scala.tools.nsc.MainGenericRunner e4-7.scala
java.home | H:\ProgramFiles\Java\jdk1.8.0_25\jre
...
*/
object
App{
def
main(args
:
Array[String]){
val
a
=
Array(
1
,
2
,
3
,
4
,
5
,
6
,
7
)
val
b
=
minmax(a)
println(a.mkString(
"["
,
", "
,
"]"
))
println(b)
}
def
minmax(values
:
Array[Int])
=
{
(values.min,values.max)
}
}
/*result
[1, 2, 3, 4, 5, 6, 7]
(1,7)
*/
object
App{
def
main(args
:
Array[String]){
val
a
=
Array(
1
,
2
,
3
,
4
,
5
,
6
,
7
)
val
b
=
lteqgt(a,
4
)
println(a.mkString(
"["
,
", "
,
"]"
))
println(b)
}
def
lteqgt(values
:
Array[Int], v
:
Int)
=
{
val
lt
=
values.count(
_
< v)
val
eq
=
values.count(
_
==
v)
val
gt
=
values.count(
_
> v)
(lt, eq, gt)
}
}
/*result
[1, 2, 3, 4, 5, 6, 7]
(3,1,3)
*/
object App{ def main(args : Array[String]){ val a = zip( "Hello" , "World" ) println(a) //println(a.mkString("[",", ","]")) } def zip(s 1 : String, s 2 : String) = { val a = s 1 .zip(s 2 ) a } } /*result Vector((H,W), (e,o), (l,r), (l,l), (o,d)) */
|
标签:
原文地址:http://www.cnblogs.com/chenjo/p/4436588.html