-1

I have a scenario in which if the user select any option from drop-down then that value is going to server as ArrayList. And if the user does not select anything then the default value is going to server as String. In java I have to convert the value to string(if i get ArrayList I have to convert that into string if i get string I have to store as it is. What is the best way to do this with minimal code?

I am trying below code :

String encoding =  myMap.get("encoding").toString();

encoding = encoding.replaceAll("\\[\\]", ""); \\removing brackets
5
  • Since you mention a server, use a simple and common syntax, JSON Commented Jun 7, 2017 at 5:09
  • What is the structure of the option dropdown data Commented Jun 7, 2017 at 5:15
  • what i am doing is right? Commented Jun 7, 2017 at 5:16
  • encoding.replaceAll("\[\]", "") will replace brackets only if they are adjacent i.e. "[]". Try encoding.replaceAll("\[", "").replaceAll("\]", ""). But if your options contain "[" and/or "]" the results will not be as expected. Commented Jun 7, 2017 at 5:20
  • i am getting data as "abc" (string) sometime sometime as xyz(arraylist) only one element in arraylist Commented Jun 7, 2017 at 5:24

3 Answers 3

0

I think you need to check if object converted to string is or is not array,because they are converted to string differently:

public String listToString(List<Object> list) {
        StringBuilder sb = new StringBuilder();

        for (Object object : list) {
            if (object != null) {
                if (object.getClass().isArray()) {
                    sb.append(Arrays.toString((Object[]) object));
                } else {
                    sb.append(object.toString());
                }
            }
        }
        return sb.toString();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of the regex you're currently using, use [\\[\\]].

List<Integer> list = Arrays.asList(1,2,3,4,5);
String s = list.toString().replaceAll("[\\[\\]]", "");
System.out.println(s);  

Or you can use substring, like

String s = list.toString().substring(1, list.toString().length()-1);

5 Comments

Is there any best way to handle it. Instead of converting arraylist to string and then removing the braces.
@ramya you could also use substring, which might be faster, but most of the time, it won't make any difference.
Arraylist will have only one element always
@ramya then you could just do list.get(0)
i am getting in map and the value of map is arraylist String encoding = myMap.get("encoding").toString();
0

You should never ever rely on things like list.toString().replaceAll("[\\[\\]]", "") because they are just an specific implementation of the toString method for the list and can be changed any moment making your code to crash...

Using join since java8

List<Object> olist = Arrays.asList(1, 2, 3, 4, 5);
List<String> sList = olist.stream().map(String::valueOf).collect(Collectors.toList());
String results = String.join(",", sList);
System.out.println(results);

1 Comment

i have a map and the value of map may be either arralylist which has only one element or a string. What i am doing now is getting the value from the map(it may be arraylist with one element or a string) and converting it into string using Tostring then removing the bracket String encoding = myMap.get("encoding").toString();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.