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

发布于 2024-12-28 10:50:57 字数 449 浏览 1 评论 0原文

将数据从 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 个地图读取原始数据(表 1)并进行比较以创建新数据(表 2)。假设我使用 Map>表 1。

谢谢

What is the best option to convert the data from String to unique int IDs .For example

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

TO

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

I was thinking to add UserName and MovieType to Sets to get a unique lists first. Then create a Map for each of them. My question now , how I can use the 2 Maps to read and compare with original data (table 1) to create new data (table 2). Assume I used Map> for table 1.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名。 2025-01-04 10:50:57

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

import 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);
        }
    }

}

You can see the working example here:

import java.util.HashMap;
import 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);
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文