第十八讲 二叉树、JDK8函数式接口、File类、输入流与输出流、字节流与字符流、文件复制的实现

来源:互联网 发布:周振兴 mysql 编辑:程序博客网 时间:2024/05/21 02:48

导读

二叉树:是一种优秀的算法。

JDK8新特性:函数式接口与Lambda表达式。

File类:文件目录或者文件对象。

输入流与输出流:底层数据传输技术。

字节流与字符流:不同的传递媒介,有不同的特性。

文件复制:采用IO技术的底层传输实现重新写出文件。


①、二叉树

     概念分析:用Java实现二叉树的前序,中序,后序,层序遍历, S型层序遍历

算法简述:

前三个算法在于输出当前节点的位置,

1、前序: 在递归左右儿子之前,输出当前节点

代码如下:

void PreOrderPrint(){  
    System.out.print(value.toString() + " ");  
    if(left != null)  
        left.PreOrderPrint();  
    if(right != null)  
        right.PreOrderPrint();  


2、中序:在递归左右儿子中间,输出

代码如下:

void InOrderPrint(){  
    if(left != null)  
        left.InOrderPrint();  
    System.out.print(value.toString() + " ");  
    if(right != null)  
        right.InOrderPrint();  
}

  

3、后序:在递归左右儿子之后输出

代码如下:

void PostOrderPrint(){  
    if(left != null)  
        left.PostOrderPrint();  
    if(right != null)  
        right.PostOrderPrint();  
    System.out.print(value.toString() + " ");  
}


4、层序遍历,这里的实现方式类似于两个簸箕(queue1 和 queue2)之间互相倒,知道谁都没有后继节点位置,即两个簸箕都为空,此处是两个队列都为空——

代码如下:

void LevelOrderPrint(){  
    if(this == null)  
        throw new IllegalArgumentException("null node !");  
    Queue<Node<E>> queue1 = new LinkedList<Node<E>>();  
    Queue<Node<E>> queue2 = new LinkedList<Node<E>>();  
    queue1.add(this);  
    while(!queue1.isEmpty() || !queue2.isEmpty()){  
        if(queue2.isEmpty()){  
            while(!queue1.isEmpty()){  
                Node<E> currentNode = queue1.poll();  
                System.out.print(currentNode.value.toString() + " ");  
                if(currentNode.left != null){  
                    queue2.add(currentNode.left);  
                }  
                if(currentNode.right != null){  
                    queue2.add(currentNode.right);  
                }  
            }  
        }  
        else{  
            while(!queue2.isEmpty()){  
                Node<E> currentNode = queue2.poll();  
                System.out.print(currentNode.value.toString() + " ");  
                if(currentNode.left != null){  
                    queue1.add(currentNode.left);  
                }  
                if(currentNode.right != null){  
                    queue1.add(currentNode.right);  
                }  
            }  
        }  
        System.out.println();  
    }        

 5、S型层序遍历,就是把上面使用的queue换为stack,注意左右子节点添加顺序,就可以了——

代码如下:

//Print By Level S-style  
public void S_LevelOrderPrint(){  
    Stack<Node<E>> stack1 = new Stack<Node<E>>();  
    Stack<Node<E>> stack2 = new Stack<Node<E>>();  
    stack1.add(this);  
    while(!stack1.isEmpty() || !stack2.isEmpty()){  
        if(stack1.isEmpty()){  
            while(!stack2.isEmpty()){  
                Node<E> currentNode = stack2.pop();  
                System.out.print(currentNode.value + " ");  
                if(currentNode.left != null)  
                    stack1.push(currentNode.left);  
                if(currentNode.right != null)  
                    stack1.push(currentNode.right);  
            }  
        }else{  
            while(!stack1.isEmpty()){  
                Node<E> currentNode = stack1.pop();  
                System.out.print(currentNode.value + " ");  
                if(currentNode.right != null)  
                    stack2.add(currentNode.right);  
                if(currentNode.left != null)  
                    stack2.add(currentNode.left);  
            }  
        }  
        System.out.println();  
    }  


上面的各种分析方式都罗列出来了,下面我们来看一下完整的二叉树代码:

package offer;  
  
import java.util.LinkedList;  
import java.util.Queue;  
import java.util.Stack;  
  
  
class Node<E extends Comparable<E>>{  
    Node<E> left;  
    Node<E> right;  
    E value;  
    Node(){  
        left = null;  
        right = null;  
        value = null;  
    }  
    Node(E value){  
        this.value = value;  
        left = null;  
        right = null;  
    }  
      
    void PreOrderPrint(){  
        System.out.print(value.toString() + " ");  
        if(left != null)  
            left.PreOrderPrint();  
        if(right != null)  
            right.PreOrderPrint();  
    }  
      
    void InOrderPrint(){  
        if(left != null)  
            left.InOrderPrint();  
        System.out.print(value.toString() + " ");  
        if(right != null)  
            right.InOrderPrint();  
    }  
      
    void PostOrderPrint(){  
        if(left != null)  
            left.PostOrderPrint();  
        if(right != null)  
            right.PostOrderPrint();  
        System.out.print(value.toString() + " ");  
    }  
      
    //Print By Level  
    void LevelOrderPrint(){  
        if(this == null)  
            throw new IllegalArgumentException("null node !");  
        Queue<Node<E>> queue1 = new LinkedList<Node<E>>();  
        Queue<Node<E>> queue2 = new LinkedList<Node<E>>();  
        queue1.add(this);  
        while(!queue1.isEmpty() || !queue2.isEmpty()){  
            if(queue2.isEmpty()){  
                while(!queue1.isEmpty()){  
                    Node<E> currentNode = queue1.poll();  
                    System.out.print(currentNode.value.toString() + " ");  
                    if(currentNode.left != null){  
                        queue2.add(currentNode.left);  
                    }  
                    if(currentNode.right != null){  
                        queue2.add(currentNode.right);  
                    }  
                }  
            }  
            else{  
                while(!queue2.isEmpty()){  
                    Node<E> currentNode = queue2.poll();  
                    System.out.print(currentNode.value.toString() + " ");  
                    if(currentNode.left != null){  
                        queue1.add(currentNode.left);  
                    }  
                    if(currentNode.right != null){  
                        queue1.add(currentNode.right);  
                    }  
                }  
            }  
            System.out.println();  
        }  
    }  
      
    //Print By Level S-style  
    public void S_LevelOrderPrint(){  
        Stack<Node<E>> stack1 = new Stack<Node<E>>();  
        Stack<Node<E>> stack2 = new Stack<Node<E>>();  
        stack1.add(this);  
        while(!stack1.isEmpty() || !stack2.isEmpty()){  
            if(stack1.isEmpty()){  
                while(!stack2.isEmpty()){  
                    Node<E> currentNode = stack2.pop();  
                    System.out.print(currentNode.value + " ");  
                    if(currentNode.left != null)  
                        stack1.push(currentNode.left);  
                    if(currentNode.right != null)  
                        stack1.push(currentNode.right);  
                }  
            }else{  
                while(!stack1.isEmpty()){  
                    Node<E> currentNode = stack1.pop();  
                    System.out.print(currentNode.value + " ");  
                    if(currentNode.right != null)  
                        stack2.add(currentNode.right);  
                    if(currentNode.left != null)  
                        stack2.add(currentNode.left);  
                }  
            }  
            System.out.println();  
        }  
    }  
}  
  
class BinarySearchTree<E extends Comparable<E>>{  
    private Node<E> root;  
      
    public Node<E> getRoot(){  
        return root;  
    }  
      
    BinarySearchTree(){  
        root = null;  
    }  
      
    public void InsertNode(E value){  
        if(root == null){  
            root = new Node<E>(value);  
            return;  
        }  
        Node<E> currentNode = root;  
        while(true){  
            if(value.compareTo(currentNode.value) > 0){  
                if(currentNode.right == null){  
                    currentNode.right = new Node<E>(value);  
                    break;  
                }  
                currentNode = currentNode.right;  
            }else{  
                if(currentNode.left == null){  
                    currentNode.left = new Node<E>(value);  
                    break;  
                }  
                currentNode = currentNode.left;  
            }  
        }  
    }  
      
}  
  
public class LevelPrintOfBinaryTree<E extends Comparable<E>> {  
    public static void main(String[] args) {  
        BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();  
        {  
            tree.InsertNode(4);  
            tree.InsertNode(2);  
            tree.InsertNode(1);  
            tree.InsertNode(3);  
            tree.InsertNode(6);  
            tree.InsertNode(5);  
            tree.InsertNode(7);  
            tree.InsertNode(8);  
        }  
        System.out.print("PreOrderPrint: ");  
        tree.getRoot().PreOrderPrint();  
        System.out.print("\nInOrderPrint: ");  
        tree.getRoot().InOrderPrint();  
        System.out.print("\nPostOrderPrint: ");  
        tree.getRoot().PostOrderPrint();  
        System.out.println("\nLevelOrderPrint: ");  
        tree.getRoot().LevelOrderPrint();  
        System.out.println("\nS_LevelOrderPrint: ");  
        tree.getRoot().S_LevelOrderPrint();  
    }  


JDK8新特性之:函数式接口与Lambda表达式——

这里来讲解一下Java8 新特性中的函数式接口, 以及和Lambda 表达式的关系。看到过很多不少介绍Java8特性的文章,都会介绍到函数式接口和lambda表达式,但是都是分别介绍,没有将两者的关系说明清楚,在这里,把自己的理解整理如下:

详细分析如下——

一、函数式接口:

  函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:SAM类型的接口(Single Abstract Method)。定义了这种类型的接口,使得以其为参数的方法,可以在调用时,使用一个lambda表达式作为参数。从另一个方面说,一旦我们调用某方法,可以传入lambda表达式作为参数,则这个方法的参数类型,必定是一个函数式的接口,这个类型必定会使用@FunctionalInterface进行修饰。

  从SAM原则上讲,这个接口中,只能有一个函数需要被实现,但是也可以有如下例外:

    1. 默认方法与静态方法并不影响函数式接口的契约,可以任意使用,即

      函数式接口中可以有静态方法,一个或者多个静态方法不会影响SAM接口成为函数式接口,并且静态方法可以提供方法实现

      可以由 default 修饰的默认方法方法,这个关键字是Java8中新增的,为的目的就是使得某一些接口,原则上只有一个方法被实现,但是由于历史原因,不得不加入一些方法来兼容整个JDK中的API,所以就需要使用default关键字来定义这样的方法

    2. 可以有 Object 中覆盖的方法,也就是 equals,toString,hashcode等方法。

  JDK中以前所有的函数式接口都已经使用 @FunctionalInterface 定义,可以通过查看JDK源码来确认,以下附JDK 8之前已有的函数式接口:

            java.lang.Runnable

            java.util.concurrent.Callable

            java.security.PrivilegedAction

            java.util.Comparator

            java.io.FileFilter

            java.nio.file.PathMatcher 

            java.lang.reflect.InvocationHandler

            java.beans.PropertyChangeListener

            java.awt.event.ActionListener  

            javax.swing.event.ChangeListener


代码如下——

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

@FunctionalInterface
interface Converter<F, T> {
    T convert(F from);
}

/** 测试一下 **/

Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
Integer converted = converter.convert("123");

注:方法和构造函数引用在Java8中可以通过 :: 操作符调用


自行设计的方法中, 如果可以接收 lambda 表达式, 则可以使用 Function 作为参数, 如下为一些已经实现的函数式接口:

代码如下——

    // Function<T, R> -T作为输入,返回的R作为输出
    Function<String,String> function = (x) -> {System.out.print(x+": ");return "Function";};
    System.out.println(function.apply("hello world"));
    //Predicate<T> -T作为输入,返回的boolean值作为输出
    Predicate<String> pre = (x) ->{System.out.print(x);return false;};
    System.out.println(": "+pre.test("hello World"));
    //Consumer<T> - T作为输入,执行某种动作但没有返回值
    Consumer<String> con = (x) -> {System.out.println(x);};
    con.accept("hello world");
    //Supplier<T> - 没有任何输入,返回T
    Supplier<String> supp = () -> {return "Supplier";};
    System.out.println(supp.get());
    //BinaryOperator<T> -两个T作为输入,返回一个T作为输出,对于“reduce”操作很有用
    BinaryOperator<String> bina = (x,y) ->{System.out.print(x+" "+y);return "BinaryOperator";};
    System.out.println("  "+bina.apply("hello ","world"));


二、Lambda表达式(这里只是简单提一下)

书写方法:  e -> System.out.println( e )

    1. 三部分构成


        参数列表

        符号 ->

        函数体 : 有多个语句,可以用{} 包括, 如果需要返回值且只有一个语句,可以省略 return

    2. 访问控制:

        可以访问类的成员变量和局部变量(非final会自动隐含转为final) ——


三、File类核心常用方式

     代码如下:

java中File类的常用所有方法及其应用


创建:
createNewFile()在指定位置创建一个空文件,成功就返回true,如果已存在就不创建,然后返回false。
mkdir()  在指定位置创建一个单级文件夹。
mkdirs()  在指定位置创建一个多级文件夹。
renameTo(File dest)如果目标文件与源文件是在同一个路径下,那么renameTo的作用是重命名, 如果目标文件与源文件不是在同一个路径下,那么renameTo的作用就是剪切,而且还不能操作文件夹。 


删除:
delete()  删除文件或者一个空文件夹,不能删除非空文件夹,马上删除文件,返回一个布尔值。
deleteOnExit()jvm退出时删除文件或者文件夹,用于删除临时文件,无返回值。
 判断:
exists()  文件或文件夹是否存在。
isFile()  是否是一个文件,如果不存在,则始终为false。
isDirectory()  是否是一个目录,如果不存在,则始终为false。
isHidden()  是否是一个隐藏的文件或是否是隐藏的目录。
isAbsolute()  测试此抽象路径名是否为绝对路径名。
 获取:
getName()  获取文件或文件夹的名称,不包含上级路径。
getAbsolutePath()获取文件的绝对路径,与文件是否存在没关系
length()  获取文件的大小(字节数),如果文件不存在则返回0L,如果是文件夹也返回0L。
getParent()  返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回null。
lastModified()获取最后一次被修改的时间。


文件夹相关:
static File[] listRoots()列出所有的根目录(Window中就是所有系统的盘符)
list()  返回目录下的文件或者目录名,包含隐藏文件。对于文件这样操作会返回null。
listFiles()  返回目录下的文件或者目录对象(File类实例),包含隐藏文件。对于文件这样操作会返回null。
list(FilenameFilter filter)返回指定当前目录中符合过滤条件的子文件或子目录。对于文件这样操作会返回null。
listFiles(FilenameFilter filter)返回指定当前目录中符合过滤条件的子文件或子目录。对于文件这样操作会返回null。


复制代码
package com.file;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * @author 王金龙
 * @date 创建时间: 2017-2-9 上午11:32:40
 * @version 1.0
 */
public class FileMethod {
    public static void main(String[] args) throws IOException {
        //创建方法
/*

@SuppressWarnings("unused")
        File file = new File("F:\\a.txt");
        //System.out.println("创建成功了吗?"+file.createNewFile());
        //System.out.println("单级文件夹创建成功了吗?"+file.mkdir());
        //System.out.println("多级文件夹创建成功了吗?"+file.mkdirs());
        //File dest = new File("F:\\电影\\c.txt");
        //System.out.println("重命名成功了吗?"+file.renameTo(dest));
*/
/*     

//删除方法
        File file = new File("F:\\电影");
        System.out.println("删除成功了吗?"+file.delete());
        file.deleteOnExit();
*/
        //判断方法
/*        

File file = new File("F:\\a.txt");
        System.out.println("文件或者文件夹存在吗?"+file.exists());
        System.out.println("是一个文件吗?"+file.isFile());
        System.out.println("是一个文件夹吗?"+file.isDirectory());
        System.out.println("是隐藏文件吗?"+file.isHidden());
        System.out.println("此路径是绝对路径名?"+file.isAbsolute());
*/
        
        //获取方法
/*
         File file = new File("f:\\a.txt");
        System.out.println("文件或者文件夹得名称是:"+file.getName());
        System.out.println("绝对路径是:"+file.getPath());
        System.out.println("绝对路径是:"+file.getAbsolutePath());
        System.out.println("文件大小是(以字节为单位):"+file.length());
        System.out.println("父路径是"+file.getParent());
        //使用日期类与日期格式化类进行获取规定的时间
        long  lastmodified= file.lastModified();
        Date data = new Date(lastmodified);
        SimpleDateFormat simpledataformat = new SimpleDateFormat("YY年MM月DD日 HH:mm:ss");
        System.out.println("最后一次修改的时间是:"+simpledataformat.format(data));
*/

        //文件或者文件夹的方法
        File[] file = File.listRoots();
        System.out.println("所有的盘符是:");
        for(File item : file){
            System.out.println("\t"+item);
        }
        File filename =new File("F:\\Java workspace\\Java");
        String[] name = filename.list();
        System.out.println("指定文件夹下的文件或者文件夹有:");
        for(String item : name){
            System.out.println("\t"+item);
        }
        File[] f = filename.listFiles();
        System.out.println("获得该路径下的文件或文件夹是:");
        for(File item : f){
            System.out.println("\t"+item.getName());
            }
        }
}


输入流与输出流——

在这里我推荐一篇博客,写得非常详细,希望大家细心研读,其作者心得很好。

【网址:http://blog.csdn.net/liuxiaogangqq/article/details/25892667/】



文件的复制操作——

/**
     * 复制文件
     * @param fromFile
     * @param toFile
     * <br/>
     * 2016年12月19日  下午3:31:50
     * @throws IOException 
     */
    public void copyFile(File fromFile,File toFile) throws IOException{
        FileInputStream ins = new FileInputStream(fromFile);
        FileOutputStream out = new FileOutputStream(toFile);
        byte[] b = new byte[1024];
        int n=0;
        while((n=ins.read(b))!=-1){
            out.write(b, 0, n);
        }
        
        ins.close();
        out.close();
    }


阅读全文
0 0
原创粉丝点击