在java中将映射字符串转换为唯一的整数ID?

在java中将映射字符串转换为唯一的整数ID?,java,treemap,Java,Treemap,将数据从字符串转换为唯一整数ID的最佳选项是什么 UserName, MovieType , year watched John , Comedy , 2000 John , Comedy , 2012 Alis , Comedy , 2005 Alis, Animation , 2003 到 我想先在集合中添加用户名和MovieType,以获得唯一的列表。然后为它们中的每一个创建一个地图。我现在的问题是,如何使用这两个映射读取原始数据(表1)并与之进行比较以创建新数据(表2)。假设我对表1

将数据从字符串转换为唯一整数ID的最佳选项是什么

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

我想先在集合中添加用户名和MovieType,以获得唯一的列表。然后为它们中的每一个创建一个地图。我现在的问题是,如何使用这两个映射读取原始数据(表1)并与之进行比较以创建新数据(表2)。假设我对表1使用了Map>


谢谢

您可以在这里看到工作示例:

导入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);
        }
    }

}
public类MainClass{
私有静态最终字符串[]dataArray={“约翰,喜剧,2000”,
“约翰,喜剧,2012”,
“艾利斯,喜剧,2005”,
“Alis,动画,2003年”};
公共静态void main(字符串[]args){
int-wordIndex=0;
Map wordIndexMap=new HashMap();//映射以存储单词和相关索引
Map reverseWordMap=new HashMap();//映射到word的存储索引
String[]convertedArray=新字符串[dataArray.length];
for(int index=0;index
我认为该信息不正确或可能是我不理解
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);
        }
    }

}