当前位置: 首页 > news >正文

Scala day4(tuple, set and map)

Foreword

Hi!! my dear friends, are you lazy at today?? Oh! I am also lazy sometimes, but you will know keep study that’s a right way at last. Now!! let’s start new travel about Scala.

Text

The all Codes in file day3.scala, like the below program:

import scala.collection.mutable
import scala.collection.immutable.HashSet

object day3 {
  def main(args:Array[String]): Unit = {
    System.out.println("Scala is make me happy!!");
    Tuple_operation();
  }

  def Tuple_operation(): Unit = {
//    val empty_tup = ();
//    System.out.println(empty_tup);
    val pair = (81,"number","Y");
    System.out.println(pair);
//    pair(0) = 5;
    System.out.println(pair(0));
    System.out.println(pair(2));
    System.out.println(pair(1));
//    System.out.println(pair[1]);
    System.out.println(pair._1);
    System.out.println(pair._2);
    System.out.println(pair._3);

    var capital = Set("Beijing","Tokyo","London");
    capital += "Paris";
    System.out.println(capital);
    System.out.println(capital.contains("Washington"));

    val exam = Set("123","456");
    System.out.println(exam);

    val Musicset = mutable.Set("One day","Lemon Tree");
    Musicset += "Tomorrow will be better";
    System.out.println(Musicset);

    Musicset.+=("Waiting for love");
    System.out.println(Musicset);
    Musicset.+=("MoonLight");
    System.out.println(Musicset);
    Musicset += "Oblivion";
    System.out.println(Musicset);

    var video = mutable.Set("KaSha","Hero");
    System.out.println(video);
//    Musicset = video;

    var Name = HashSet("Tom","Sam");
    System.out.println("HashSet:");
    Name += "Amy";
    System.out.println(Name);
    Name += "Pan"
    System.out.println(Name);

    val letters = HashSet("ABC","DEF","RTF");
    System.out.println(letters);

    val MAP = mutable.Map[String,String]();
    MAP += ("music" -> "后来");
    MAP += ("movie" -> "第一滴血")
    MAP += ("game" -> "war3");
    System.out.println(MAP);
    System.out.println(MAP("game"));
    System.out.println(MAP("music"));
    System.out.println(MAP("movie"));

    val Mapper = Map(1 -> "A",2 -> "B",3 -> "C");
    System.out.println(Mapper);
    System.out.println(Mapper(1));
    System.out.println(Mapper(2));
    System.out.println(Mapper(3));
      
  }
}

The first example, it’s about the tuple defined and get the tuple values. It like the below Codes:

	val pair = (81,"number","Y");
    System.out.println(pair);

    System.out.println(pair(0));
    System.out.println(pair(2));
    System.out.println(pair(1));

    System.out.println(pair._1);
    System.out.println(pair._2);
    System.out.println(pair._3);

Output Result:
请添加图片描述
From the above Codes, we know the process of tuple type defined. Through subscript couldn’t gain value at the previous relase. In 3.6.4 relase, the reason is like the below pictures:
请添加图片描述
请添加图片描述
It’s use apply() function, it’s like List type, so it could use subscript to gain the tuple value.

Next, the example is set type declared, codes like the below program:

    var capital = Set("Beijing","Tokyo","London");
    capital += "Paris";
    System.out.println(capital);
    System.out.println(capital.contains("Washington"));

    val exam = Set("123","456");
    System.out.println(exam);

Output Result:
请添加图片描述
From the Output Result, we know var type could let set type mutable, contains() function, it could judge data in the set. Val type couldn’t change the set element.

Then, we look the mutable set, it like the below codes:

 	val Musicset = mutable.Set("One day","Lemon Tree");
    Musicset += "Tomorrow will be better";
    System.out.println(Musicset);

    Musicset.+=("Waiting for love");
    System.out.println(Musicset);
    Musicset.+=("MoonLight");
    System.out.println(Musicset);
    Musicset += "Oblivion";
    System.out.println(Musicset);

    var video = mutable.Set("KaSha","Hero");
    System.out.println(video);
//    Musicset = video;

Output Result:
请添加图片描述
From the above program, we know val type couldn’t limit the mutable set, the reason is mutable set is change itself, isn’t change the Musicset Object. if we uncomment, then new Object video will assign value to Musicset Object, but the val type couldn’t permit the operation and pop error. And set is disordered.

After, we watch the example about immutable set, like the below codes:

 	var Name = HashSet("Tom","Sam");
    System.out.println("HashSet:");
    Name += "Amy";
    System.out.println(Name);
    Name += "Pan"
    System.out.println(Name);

    val letters = HashSet("ABC","DEF","RTF");
    System.out.println(letters);

Output Result:
请添加图片描述
From the result, we know var type could effect the immutable set, it also could add element to set.

At last, we look this codes:

	val MAP = mutable.Map[String,String]();
    MAP += ("music" -> "后来");
    MAP += ("movie" -> "第一滴血")
    MAP += ("game" -> "war3");
    System.out.println(MAP);
    System.out.println(MAP("game"));
    System.out.println(MAP("music"));
    System.out.println(MAP("movie"));

    val Mapper = Map(1 -> "A",2 -> "B",3 -> "C");
    System.out.println(Mapper);
    System.out.println(Mapper(1));
    System.out.println(Mapper(2));
    System.out.println(Mapper(3));

Output Result:
请添加图片描述
From the program result, we know map type has two ways to create it. One is obviously point itself all types and haven’t put datas, anthor is directly put datas, it could identify these datas corresponding itselves types.

In the end

I gratitude for your watch my articles, if you still learn the Scala, you are excellent, I admire you, I hope you could point some mistakes in my passages. The reason is I’m not a smart man. Please you remember: anything, you must insist do it at last that you will become winner in this game. Your Pan.【One human low-quality male】

相关文章:

  • echarts 图表
  • 【阶次跟踪】变转速阶次包络谱
  • 海洋垃圾分割数据集labelme格式3110张8类别
  • 从案例看建设工程领域居间合同的法律效力
  • 洛谷P8749 [蓝桥杯 2021 省 B] 杨辉三角形
  • 关于无线网络安全的基础知识,涵盖常见威胁、防护措施和实用建议
  • 简单记录一下Android四大组件
  • MySQL部分总结
  • 07.unity 游戏开发-粒子系统
  • 结构化需求分析:功能、数据与行为的全景建模
  • Tauri 2.3.1+Leptos 0.7.8开发桌面应用--Sqlite数据库的写入、展示和选择删除
  • 安卓设备使用网线连接电脑lan口
  • 从存储到智能:AI NAS的技术路径与未来演进
  • Android游戏辅助工具开发详解
  • 使用OpenSceneGraph (osg)实现一个星系漫游
  • asm汇编源代码之-字库转换程序
  • Mybatis-缓存详解
  • 因泰立科技激光雷达:点亮煤矿厂自动装车的智慧之光
  • 音视频学习(三十四):H264中的宏块
  • thymeleaf配套Security6
  • 吉林市马拉松5月18日开赛,奖牌、参赛服公布
  • 警惕“全网最低价”等宣传,市监总局和中消协发布直播消费提示
  • 世界人形机器人运动会将在北京“双奥场馆”举行
  • 巴基斯坦:印度向巴3处地点发射导弹
  • 今年五一档电影票房已破7亿
  • 工程机械行业景气度持续回升,三大龙头一季度营收、净利双增