java 实现递归实现tree(2)

import com.google.common.collect.Lists;
import org.springframework.cglib.beans.BeanCopier;

import java.util.*;
import java.util.stream.Collectors;
public class tset {
	 private static Map<String, List<String>> map = new HashMap<>();

	    static {
	        map.put("2", Lists.newArrayList("A", "B", "C"));
	        map.put("3", Lists.newArrayList("D", "E", "F"));
	        map.put("4", Lists.newArrayList("G", "H", "I"));
	        map.put("5", Lists.newArrayList("J", "K", "L"));
	        map.put("6", Lists.newArrayList("M", "N", "O"));
	        map.put("7", Lists.newArrayList("P", "Q", "R", "S"));
	        map.put("8", Lists.newArrayList("T", "U", "V"));
	        map.put("9", Lists.newArrayList("W", "X", "Y", "Z"));
	    }

	    public static void main(String[] args) {
	        Scanner scanner = new Scanner(System.in);
	        Map<String, List<Node>> nodeMap;
	        while (true) {
	            nodeMap = new HashMap<>();
	            String num = scanner.next();
	            char[] chars = num.toCharArray();
	            LinkedList<Node> list = new LinkedList<>();
	            if (chars.length == 1) {
	                list.addAll(map.get(String.valueOf(chars[0])).stream().map(Node::new).collect(Collectors.toList()));
	            } else {
	                for (int i = 0; i < chars.length - 1; i++) {
	                    char aChar = chars[i];
	                    List<Node> collect0 = nodeMap.computeIfAbsent(String.valueOf(aChar), a -> map.get(String.valueOf(aChar)).stream().map(Node::new).collect(Collectors.toList()));
	                    if (i == 0) {
	                        list.addAll(collect0);
	                    }
	                    List<Node> collect = map.get(String.valueOf(chars[i + 1])).stream().map(Node::new).collect(Collectors.toList());
	                    nodeMap.put(String.valueOf(chars[i + 1]), collect);
	                    for (Node node : collect0) {
	                        node.children = collect;
	                    }
	                }
	            }
	            String name;
	            String preName;
	            List<String> result = new ArrayList<>();
	            while (!list.isEmpty()) {
	                Node node = list.removeFirst();
	                name = node.name;
	                preName = node.preName != null ? node.preName + name : name;
	                if (node.children != null) {
	                    for (Node node1 : node.children) {
	                        Node newNode = new Node();
	                        BeanCopier beanCopier = BeanCopier.create(node1.getClass(), newNode.getClass(), false);
	                        beanCopier.copy(node1, newNode, null);
	                        newNode.preName = preName;
	                        if (newNode.children != null) {
	                            list.add(newNode);
	                        } else {
	                            result.add(preName + newNode.name);
	                        }
	                    }
	                } else {
	                    result.add(name);
	                }
	            }
	            System.out.println(result);
	        }

	    }

	    public static class Node {
	        String name;
	        String preName;
	        List<Node> children;

	        Node() {
	        }

	        Node(String name) {
	            this.name = name;
	        }

	        public String getName() {
	            return name;
	        }

	        public void setName(String name) {
	            this.name = name;
	        }

	        public String getPreName() {
	            return preName;
	        }

	        public void setPreName(String preName) {
	            this.preName = preName;
	        }

	        public List<Node> getChildren() {
	            return children;
	        }

	        public void setChildren(List<Node> children) {
	            this.children = children;
	        }
	    }

}

  

posted on 2019-06-10 14:55  啊哈哈哈哈-  阅读(483)  评论(0编辑  收藏  举报