scala scala Quick Start series [Introduction]

        In the scala column has written two blog posts, it will introduce in detail how to install scala on the windows and integration with IDEA.

        I believe we might have put the environment are configured, but the concept may scala is still unknown. So going to a small bacteriascala Quick StartSeries, for everyone to learn to use. As a quick start series first blog, this blog is to bring everyone scala basic introduction


Brief introduction

        scala is running in JVMa multi-paradigm programming language, as well as support for object-oriented and function-oriented programming.

        Early on, when the scala just appeared, and did not pay attention to how, as Spark, and Kafkathe rise of such large data scala-based framework, scala gradually into view Big Data developers. The main advantage is its scalaExpressive

Next, we want to learn:

  • Why use scala?
  • By comparing the two cases scala language and Java language

Why scala

  • Development of large data applications (Spark program, Flink program)
  • Strong communication skills, is worth a single line of code Java multi-line development speed.
  • Java compatible, you can access a huge library of Java classes such as: operating mysql, redis, freemarker, activemq and so on.

scala contrast Java

        The following two cases, respectively, using the experience and scala java code amount achieved.

Case number one

Defines three entity class (class of users, orders, merchandise)

Java code

/*** 用户实体类 */ 
 public class User {

 private String name; 
 
 private List<Order> orders; 
 
 public String getName() { 
 return name;
 }
 
 public void setName(String name) { 
 this.name = name; 
 }

 public List<Order> getOrders() { 
 return orders; 
 }
 
 public void setOrders(List<Order> orders) { 
 this.orders = orders; 
 }

 }

/*** 订单实体类 */ 
 public class Order {
 
 private int id; 
 
 private List<Product> products; 
 
 public int getId() {
  return id; 
 }
 
 public void setId(int id) { 
 this.id = id; 
 }
 
 public List<Product> getProducts() {
  return products; 
 }
 
 public void setProducts(List<Product> products) { 
this.products = products; 
} 

}
/*** 商品实体类 */ 
public class Product { 

private int id; 
private String category;


 public int getId() {
  return id; 
  }

public void setId(int id) {
 this.id = id; 
 }

public String getCategory() { 
 return category;

 }

public void setCategory(String category) { 
this.category = category;

} 
}

scala Code

case class User(var name:String, var orders:List[Order]) // 用户实体类 
case class Order(var id:Int, var products:List[Product]) // 订单实体类 
case class Product(var id:Int, var category:String) // 商品实体类

Case II

There is a string (digital) list, we'd like to list all the strings to integers.

Java code

// 创建一个Integer类型的列表
 List<Integer> ints = new ArrayList<Integer>(); 
 for (String s : list) { 
 ints.add(Integer.parseInt(s)); 
 }

scala Code

val ints = list.map(s => s.toInt)

        Remember mentioned in the introduction, the main advantage is its scalaExpressive. Yes, compared to the terms java, scala can use fewer lines of code "elegant" to express what we think. So, scala enough to be one of the languages ​​we have covered the programmer.

        Getting started as a series of scala first blog, I do not want a whole lot of bells and whistles - there are more doubts can be resolved, it can also private letter asked me. As scala Quick Start series, and strive hard quasi-fast, overcome all doubts and difficulties.

        Benpian to end here, I hope you can be a lot of support. Or can benefit big thumbs technical data of interest to look at longer follow ~~ regularly updated, there are a number of practical benefits dry, so stay tuned !!!
Here Insert Picture Description

Published 146 original articles · won praise 1340 · Views 350,000 +

Guess you like

Origin blog.csdn.net/weixin_44318830/article/details/103947811