设为首页 加入收藏

TOP

Scala For Java的一些参考(一)
2017-10-10 12:13:02 】 浏览:2692
Tags:Scala For Java 一些 参考
     

变量

String yourPast = "Good Java Programmer";

val yourPast : String = "Good Java Programmer"

val yourPast = "Good Java Programmer"

var yourFuture = "Good Java Programmer"

自动类型推断;可变、不可变变量

class Money(amount:Int) amount不是成员变量

class Money(val amount:Int)

val notMuch = new Money(2)

notMuch.amount

class Money(var amount:Int)

val notMuch = new Money(2)

notMuch.amount=3

case classes

public class Money {

private Integer amount;

private String currency;

public Money(Integer amount, String currency) {

this.amount = amount;

this.currency = currency;

}

public Integer getAmount() {

return amount;

}

public void setAmount(Integer amount) {

this.amount = amount;

}

public String getCurrency() {

return currency;

}

public void setCurrency(String currency) {

this.currency = currency;

}

@Override

public int hashCode() {

int hash = 5;

hash = 29 * hash + (this.amount != null ? this.amount.

hashCode() : 0);

hash = 29 * hash + (this.currency != null ? this.currency.

hashCode() : 0);

return hash;

}

@Override

public boolean equals(Object obj) {

if (obj == null) {

return false;

}

if (getClass() != obj.getClass()) {

return false;

}

final Money other = (Money) obj;

return true;

}

@Override

public String toString() {

return "Money{" + "amount=" + amount + ", currency=" +

currency + '}';

}

public Money add(Money other) {

return new Money(this.amount +

other.amount, this.currency);

}

}

case class Money(amount:Int=1, currency:String="USD")  two immutable fields the fields declared in Scala classes are public

case class Money(private val amount: Int, private val currency: String)

to make them private instead, or used  var instead of  val to make the fields mutable.

val defaultAmount = Money()

val fifteenDollars = Money(15,"USD")

val fifteenDollars = Money(15)

val someEuros = Money(currency="EUR")

case class Money(val amount:Int=1, val currency:String="USD"){

def +(other: Money) : Money = Money(amount + other.amount)

}

Money(12) + Money(34)

collections

Scala collections are, by default, immutable

val numbers = List(1,2,3,4,5,6)

val reversedList = numbers.reverse

val onlyAFew = numbers drop 2 take 3

cons operator

val numbers = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: Nil

val simpleList = Nil.::(6)

val twoElementsList = List(6).::(5)

列表串接

val concatenatedList = simpleList ::: twoElementsList

val things = List(0,1,true) AnyVal

val things = List(0,1,true,"false") Any

复杂对象列表

val amounts = List(Money(10,"USD"),Money(2,"EUR"),Money(20,"GBP"),

Money(75,"EUR"),Money(100,"USD"),Money(50,"USD"))

Filter

val euros = amounts.filter(money => money.currency=="EUR")

val euros = amounts.filter(x => x.currency=="EUR")

val euros = amounts.filter(_.currency=="EUR")

partition

val allAmounts = amounts.partition(amt => amt.currency=="EUR")

Tuples

val euros = allAmounts._1

val everythingButEuros= allAmounts._2

val (euros,everythingButEuros) = amounts.partition(amt => amt.currency=="EUR")

Map

Map amounts = new HashMap<String,Integer>();

amounts.put("USD", 10);

amounts.put("EUR", 2);

val wallet = Map( "USD" -> 10, "EUR" -> 2 )

val someEuros = wallet("EUR")

Option类型

val mayBeSome

首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇函数式非凡的抽象能力 下一篇LINUX系统下Java和Scala的环境配置

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目