博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TreeMap使用案例-多个映射(高级版)
阅读量:4228 次
发布时间:2019-05-26

本文共 1853 字,大约阅读时间需要 6 分钟。

先按单词长度分,然后按共性把,相同类别的存在一起,然后,把其中的一个作为key,其他参数就为value

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class WordMaps {
    public static Map<String, List<String>>
    computeAdjacentWords(List<String> words){
        Map<String, List<String>> adjWords=new TreeMap<>();
        Map<Integer,List<String>> wordsByLength=new TreeMap<>();
        for(String w:words)//按长度存值
            update(wordsByLength,w.length(),w);
        for(Map.Entry<Integer, List<String>> entry:wordsByLength.entrySet()){
            List<String> groupsWords=entry.getValue();//指定长度的所有值
            int groupNum=entry.getKey();//指定长度
            for(int i=0;i<groupNum;i++){//在索引0到groupNum之间遍历
                Map<String,List<String>> repToWord=new TreeMap<>();
                for(String str:groupsWords){//对指定长度的所有值进行遍历
                    String rep=str.substring(0, i)+str.substring(i+1);//把该值删除其中的一位
                    update(repToWord,rep,str);//把当前的值,存到删除一位的key中
                }
                for(List<String> wordClique:repToWord.values()){
                    if(wordClique.size()>=2){
                        for(String s1:wordClique){
                            for(String s2:wordClique){
                                if(s1!=s2){
                                    update(adjWords,s1,s2);
                                }
                            }
                        }
                    }
                }
            }
        }
        return adjWords;
    }
    //跟新map
    private static <KeyType> void update(Map<KeyType, List<String>> m,
                                KeyType key,String value){
        List<String> lst=m.get(key);
        if(lst==null){//如果不存在,就创建一个集合
            lst=new ArrayList<>();
            m.put(key, lst);
        }
        //如果存在,直接加到尾部
        lst.add(value);
    }
}
测试:

        List<String> list=null;

        String[] a=new String[]{"dine","line","mine","pine","vine","wide","wife","wipe","wire"};
        list=Arrays.asList(a);
        Map<String,List<String>> m=WordMaps.computeAdjacentWords(list);
        for(Map.Entry<String,List<String>> me : m.entrySet()) {
           System.out.println(me.getKey() + ": " + me.getValue());
        }

效果:

dine: [line, mine, pine, vine]

line: [dine, mine, pine, vine]
mine: [dine, line, pine, vine]
pine: [dine, line, mine, vine]
vine: [dine, line, mine, pine]
wide: [wife, wipe, wire]
wife: [wide, wipe, wire]
wipe: [wide, wife, wire]
wire: [wide, wife, wipe]

转载地址:http://evjqi.baihongyu.com/

你可能感兴趣的文章
Openfiler 配置 NFS 示例
查看>>
Oracle 11.2.0.1 RAC GRID 无法启动 : Oracle High Availability Services startup failed
查看>>
Oracle 18c 单实例安装手册 详细截图版
查看>>
Oracle Linux 6.1 + Oracle 11.2.0.1 RAC + RAW 安装文档
查看>>
Oracle 11g 新特性 -- Online Patching (Hot Patching 热补丁)说明
查看>>
Oracle 11g 新特性 -- ASM 增强 说明
查看>>
Oracle 11g 新特性 -- Database Replay (重演) 说明
查看>>
Oracle 11g 新特性 -- 自动诊断资料档案库(ADR) 说明
查看>>
CSDN博客之星 投票说明
查看>>
Oracle wallet 配置 说明
查看>>
Oracle smon_scn_time 表 说明
查看>>
VBox fdisk 不显示 添加的硬盘 解决方法
查看>>
Java多态性理解
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第一篇:互联网时代U盘化生存方式 【张振华.Jack】
查看>>
CentOS6.4配置Hadoop-2.6.0集群配置安装指南(经过实战演练)【张振华.Jack】
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第二篇:专注的力量 [张振华.Jack]
查看>>
BFS——求矩阵中“块”的个数
查看>>
BFS——走迷宫的最小步数
查看>>
并查集——好朋友
查看>>
关键路径
查看>>