import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Demo06WordCountJava {
public static void main(String[] args) throws IOException {
//用Java代码是实现WordCount
//读文件
BufferedReader br = new BufferedReader(new FileReader("Scala/data/words.txt"));
String line;
//使用HashMap进行存储
HashMap<String, Integer> wordCntMap = new HashMap<>();
while ((line = br.readLine()) != null) {
//处理每一行数据
//按照分隔符切分
String[] words = line.split(",");
for (String word : words) {
/**
* 判断每个单词word 在不在wordCntMap的key中
* 如果在 则value+1
* 如果不在 第一次取到 则value置为1
*/
if(wordCntMap.containsKey(word)){
Integer value = wordCntMap.get(word);
wordCntMap.put(word,value+1);
}else{
wordCntMap.put(word,1);
}
}
}
//将单词数量做一个打印
for (Map.Entry<String, Integer> wordCnt : wordCntMap.entrySet()) {
System.out.println(wordCnt.getKey()+":"+wordCnt.getValue());
}
}
}
java:20
spark:10
hadoop:10
object Demo07ScalaApply {
def main(args: Array[String]): Unit = {
//创建一辆汽车
val car1: Car = new Car("本田", "黑色")
println(car1._bread)
val car2: Car = Car.apply("本田", "白色")
println(car2._bread)
val car3: Car = Car("马自达", "白色")
println(car3._bread)
}
}
class Car(brand:String,color:String){
val _bread:String=brand
val _color:String=color
}
//伴生对象
object Car{
def apply(bread:String,color:String): Car = new Car(bread, color)
}
本田
本田
马自达
object Demo08Func1 {
/**
* 函数的定义
* 函数可以定义在 类中、object中、方法中
*/
def main(args: Array[String]): Unit = {
def func1(): Unit ={
println("在函数中定义函数")
}
func1()
println(func2("100","200"))
println(func3("100","200"))
}
/**
*def 关键字 表示声明一个函数
* func2 方法名
* str1:String,str2:String 两个参数
* str1、str2参数名 String为参数类型 多个参数之间用逗号分隔
* Int 返回值类型
* (......} 方法体
* 返回值通过return返回
*/
def func2(str1:String,str2:String): String ={
return str1+str2
}
def func3(str1:String,str2:String): Int ={
return str1.toInt+str2.toInt
}
}
在函数中定义函数
100200
300