1

I have a Set of data. For example:

car accord car civic suv landcruzer suv landrover muv innova

I want store it in a scanner or hash map and retrieve the values based on the input.

If "car" is the input I want to pass URL+/accord and URL+/civic as its output

If "muv" is the input, I want to pass URL+/innova as its output

String URL = "www.abc.com";
String Vehicletype = "";
@DataProvider(name = "irLanguage")
    public Object[][] lang() {

        @SuppressWarnings("resource")
        Scanner s = new Scanner(
                "Car         /accord/\n" +
                "Car         /civic/\n" +
                "suv         /landcruzer/\n" +
                "suv         /rangerover/\n" +
                "muv         /innova/\n");
        Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
        while (s.hasNext()) {

            String key = s.next();
            if (!map.containsKey(key))
                map.put(key, new LinkedList<String>());

            map.get(key).add(s.next());

        }
        urlArray = map.get(vehicletype);
        String[][] shades = new String[urlArray.size()][2];
        for (int i = 0; i < urlArray.size(); i++) {
            shades[i][0] = urlArray.get(i).toString();
            shades[i][1] = URL + urlArray.get(i).toString();
            lang = shades[i][0];
            System.out.println(shades[i][0]);

        }

        return shades;
    }

Here, the code is working fine. That is , if the input vehicle type is car then the output url is www.abc.com/accord/ and www.abc.com/civic/ and if the vehicle type is muv, it only returns www.abc.com/innova/ . This setup works fine for me. But, I wonder if there is any simpler method to do this. Can anybody with good knowledge in java can help?

1
  • Removed the Selenium tag since this has nothing to do with Selenium. Commented May 17, 2019 at 13:28

1 Answer 1

1

You have the right idea, I would build a HashMap that contains one Key (e.g. "car") and all the desired Values for that Key (e.g. "accord", "civic")

HashMap<String, ArrayList<String>> vehicles = new HashMap<String, ArrayList<String>>();
ArrayList<String> makes = new ArrayList<String>();
makes.add("accord");
makes.add("civic");
vehicles.put("car", makes);
makes.clear();

makes.add("landcruzer");
makes.add("rangerover");
vehicles.put("suv", makes);
makes.clear();

makes.add("innova");
vehicles.put("muv", makes);
makes.clear();

Now that you've got the vehicles HashMap built, you can fetch a Key and get all Values and build your URLs.

makes = vehicles.get("car");
for (String make : makes)
{
    System.out.println("www.abc.com/" + make);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.