【问题标题】:Convert map string to unique int IDs in java?在java中将映射字符串转换为唯一的int ID?
【发布时间】:2012-01-22 05:06:04
【问题描述】:

将数据从 String 转换为唯一 int ID 的最佳选择是什么。例如

UserName, MovieType , year watched
John , Comedy , 2000
John , Comedy , 2012
Alis , Comedy , 2005 
Alis, Animation , 2003

UserName, MovieType , year watched
1, 4, 2000
1, 4, 2012
2, 4, 2005
2,3,2003

我正在考虑将 UserName 和 MovieType 添加到 Sets 以首先获得唯一列表。然后为他们每个人创建一个地图。我现在的问题是,如何使用 2 Maps 读取原始数据并与原始数据(表 1)进行比较以创建新数据(表 2)。假设我在表 1 中使用了 Map>。

谢谢

【问题讨论】:

  • 我认为信息不正确或者可能是我不理解

标签: java treemap


【解决方案1】:

您可以在此处查看工作示例:

导入 java.util.HashMap; 导入 java.util.Map;

public class MainClass {

    private static final String [] dataArray = {  "John , Comedy , 2000",
                        "John , Comedy , 2012",
                        "Alis , Comedy , 2005 ",
                        "Alis, Animation , 2003"};

    public static void main(String[] args) {
        int wordIndex = 0;
        Map<String, Integer> wordIndexMap = new HashMap<String, Integer>();    //Map to store Word and related index
        Map<Integer, String> reverseWordMap = new HashMap<Integer, String>();  //Map to store index to word
        String [] convertedArray = new String [dataArray.length];

        for(int index = 0; index < dataArray.length; index++) {
            String [] tokens = dataArray[index].trim().split("\\s*,\\s*");
            StringBuilder convertedString = new StringBuilder();
            boolean dataEncountered = false;
            for(int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) {
                if(dataEncountered) {                      //Add ', ' only when something is added to the convertedString
                    convertedString.append(", ");
                }

                if(tokens[tokenIndex].matches("\\d+")) {      //To match the years like 2000, 2010 etc. This condition can be altered according to the requirement
                    convertedString.append(tokens[tokenIndex]);
                } else {
                    if(wordIndexMap.get(tokens[tokenIndex]) == null) {
                        wordIndexMap.put(tokens[tokenIndex], ++wordIndex);
                        reverseWordMap.put(wordIndex, tokens[tokenIndex]);
                    }
                    convertedString.append(wordIndexMap.get(tokens[tokenIndex]));
                }
                dataEncountered = true;
            }
            convertedArray[index] = convertedString.toString();
        }

        String [] reverseConvertedArray = new String[convertedArray.length];

        for(String data: convertedArray) {   //Print the converted array
            System.out.println(data);
        }

        for(int index = 0; index < convertedArray.length; index++) {
            String [] data = convertedArray[index].trim().split("\\s*,\\s*");
            StringBuilder convertedString = new StringBuilder();
            boolean dataEncountered = false;
            for(int arrayIndex = 0; arrayIndex < data.length; arrayIndex++) {
                if(dataEncountered) {
                    convertedString.append(", ");
                }

                if(arrayIndex + 1 == data.length) {         //To ignore the last item of the String
                    convertedString.append(data[arrayIndex]);
                } else {
                    convertedString.append(reverseWordMap.get(Integer.parseInt(data[arrayIndex])));
                }
                dataEncountered = true;
            }
            reverseConvertedArray[index] = convertedString.toString();
        }

        for(String data: reverseConvertedArray) {       //Print the reverse converted array
            System.out.println(data);
        }
    }

}

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 2020-08-19
    • 2023-03-17
    • 1970-01-01
    • 2014-03-10
    • 2021-12-25
    • 2015-03-21
    • 2013-11-03
    相关资源
    最近更新 更多