Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:23:03+00:00 2026-05-28T13:23:03+00:00

What is the best option to convert the data from String to unique int

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:23:04+00:00Added an answer on May 28, 2026 at 1:23 pm

    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);
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given an xml string like this: <some><nested><xml>value</xml></nested></some> what's the best option (using ruby) to
I've been trying to find best way to convert decimal/string to currency depending on
What is the best option if I want to upgrade old C-code to newer
Which MVC-framework is the best option (performance/ease of development) for a web application, that
I'm looking for the best option to store my application settings. I decided to
I´d like to know what is the best option to develop an auction site
I am trying to find the best option to create a simple progress bar
I'm starting out with MVC but not sure it's the best option. I need
I'm starting a new project here (Windows Forms). What's the best option today for
Caution: This question is over nine years old! Your best option is to search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.