caroly 2 年前
/**
 * 后序:递归改压栈
 * 1、压入头节点,tree 指向栈顶
 * 2、左节点没处理完处理左节点
 * 3、右节点没处理完处理右节点
 * 4、处理中间节点
 * head 指向上次打印节点的位置
 */
public static void pos(TreeNode head){
    System.out.println("pos-order: ");
    if(head!= null){
        Stack<TreeNode> stack= new Stack<>();
        stack.push(head);
        TreeNode tree= null;
        while (!stack.isEmpty()){
            tree= stack.peek();
            if(tree.left!= null&& head!= tree.left&& head!= tree.right){
                stack.push(tree.left);
            }else if(tree.right!= null&& head!= tree.right){
                stack.push(tree.right);
            }else {
                System.out.println(stack.pop().val+ " ");
                head= tree;
            }
        }
    }
    System.out.println();
}

//class TreeNode {
//    int val;
//    TreeNode left;
//    TreeNode right;
//
//    TreeNode() {
//    }
//
//    TreeNode(int val) {
//        this.val = val;
//    }
//
//    TreeNode(int val, TreeNode left, TreeNode right) {
//        this.val = val;
//        this.left = left;
//        this.right = right;
//    }
//}
caroly 2 年前

添加 jar 包到本地 Maven 仓库

mvn install:install-file -Dfile=spring-social-github-1.0.0.M4.jar -DgroupId=org.springframework.social -DartifactId=spring-social-github -Dversion=1.0.0.M4 -Dpackaging=jar

-Dfile:jar 包的位置
-DgroupId:groupId
-DartifactId:artifactId
-Dversion:version

caroly 3 年前

获取 topic 主题的列表:

./kafka-topics.sh --zookeeper caroly02:2181,caroly03:2181,caroly04:2181 --list

生产者:

./kafka-console-producer.sh --broker-list caroly02:9092,caroly03:9092,caroly04:9092 --topic 主题

消费者

./kafka-console-consumer.sh --zookeeper caroly02:2181,caroly03:2181,caroly04:2181 --topic 主题
caroly 3 年前

Redis 集群最大节点个数是 16384 个。
Redis 单个 key 最大 512M。
Redis 集群间 异步复制。

caroly 3 年前

使用 Thread 类的 join() 方法来确保所有程序创建的线程在 main() 方法退出前结束。

caroly 3 年前

Integer 与 int 相比,会占用更多的内存。

原因:Integer 是一个对象,需要存储对象的元数据。
     int 是一个原始类型的数据,所以占用的空间更少。

caroly 3 年前
System.out.println(3* 0.1== 0.3);    // false

有些浮点数不能完全精确的表示出来。

caroly 3 年前

Java 中 for 循环 i++ 和 ++i 的区别:
首先是 for 循环的执行过程:

for(A; B; C) {
    D;
}

即:

  • 进入循环执行 A;// 只是进入的时候执行
  • 执行 B;// 条件为真执行 D,为假跳出 for 循环
  • 执行 D;
  • 执行 C;
  • 回到第二步开始执行

为此,在 for 循环中 i++ 和 ++i 的执行结果是相同的,但是在 Java 中 i++ 语句是需要一个临时变量去存储返回自增前的值,而 ++i 不需要。这样就导致使用 i++ 时系统需要先申请一段内存空间,然后将值塞入进去,最后不用了才去释放。

caroly 3 年前

通过反射调用对象的方法:

import java.lang.reflect.Method;
class MethodInvokeTest {
    public static void main(String[] args) throws Exception {
        String str = "hello";
        Method m = str.getClass().getMethod("toUpperCase");
        System.out.println(m.invoke(str));

//        System.out.println(str.toUpperCase());
    }
}
caroly 3 年前

编写一个 Scala 应用程序来实现词频统计:

import org.apache.spark.{SparkConf, SparkContext}
object SparkWC {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName("wordcount")
    conf.setMaster("local")
    val sc = new SparkContext(conf)

    sc.textFile("./data/words").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).foreach(println)
    sc.stop()
  }
}

注:某些变量在匿名函数内部只用了一次可以用 "_" 将其表示