1

I have a value of type Object. It could be of any type boolean, string, integer, long, etc. I want to convert that object into a string. I do use toString method and works just as supposed to. However, sometimes the object could be String[] or Integer[] etc. (basically an array). The problem is when converting them to string ,I get a coded, not readable string. I used Arrays.toString but it accepts only arrays and my value is of type Object and I've used String.join(" ", value) but this needs a type casting for the value to string and not all types can be cast into strings.

Is there a possible way to convert them to string no matter what the type is?

Note: I know that I can specify for each value an if statement and check the type but I don't think it's good way.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rex
  • 147
  • 3
  • 11
  • It would be helpful when you share your code with us. Have you tried to call on each object in a loop the toString method? – Aalexander Feb 14 '21 at 08:26
  • You can check your object if it is an array, eg. see here: https://stackoverflow.com/questions/2725533/how-to-see-if-an-object-is-an-array-without-using-reflection And then call toString on each element and join them. – Aston Feb 14 '21 at 08:38
  • Using an `if` statement is the only way to do this. – Mark Rotteveel Feb 14 '21 at 08:59

3 Answers3

4

Write a utility method like below:

public static String convertToString(Object input){
    if (input instanceof Object[]) {
        // deepToString used to handle nested arrays.
        return Arrays.deepToString((Object[]) input);
    } else {
        return input.toString();
    }   
}

Please note that the first if condition would be evaluated to false if the input is a primitive array like int[], boolean[], etc. But it would work for Integer[] etc. If you want the method to work for primitive arrays, then you need to add conditions for each type separately like:

else if (input instanceof int[]){
    // primitive arrays cannot be nested.
    // hence Arrays.deepToString is not required.
    return Arrays.toString((Object[]) input);
}
Gautham M
  • 4,816
  • 3
  • 15
  • 37
0

You can use map() on your array after converting it to a list as:

List<Integer> list = Arrays.asList(3, 6, 9, 12, 15);
list.stream().map(element -> element.toString()).collect(Collectors.toList());
String result = String.join(",", list);

The good thing about using map() is that it comes with the flexibility of processing and filtering your data.

Beshambher Chaukhwan
  • 1,418
  • 2
  • 9
  • 13
  • The second line does exactly what the first line already does, so it is obsolete. Especially since you dont assign the result of the second line to anything, it goes directly into the trash. – Zabuzard Feb 14 '21 at 09:20
  • You dont need to create a list of an array in order to stream it, you can just do `Arrays.stream(array)`. – Zabuzard Feb 14 '21 at 09:20
  • You dont need to use `String.join` when you have a stream, nor do you need to go to list first. You can directly use `.collect(Collectors.joining(", "))` on the stream. – Zabuzard Feb 14 '21 at 09:21
  • Yep there are multiple ways of doing things – Beshambher Chaukhwan Feb 14 '21 at 09:48
  • Thats not my point. Point is, the code, as it is written now, doesnt make too much sense. It is overly complex and the second line is just absolutely useless. You could remove it entirely and the behavior would not change a bit. – Zabuzard Feb 14 '21 at 10:31
  • I just wanted to explain my downvote to you, hoping that it might help you to improve your answer, if you want to. Thats all. No need to get mad or anything. – Zabuzard Feb 14 '21 at 12:02
  • No no I was just saying your answers would help the person who asked the question that's all. I got your point but in my opinion I would avoid converting the array into one single string. I don't know what this person is trying to achieve but data rendering and show casing should be left at the front end side where JavaScript would handle the stuffs. – Beshambher Chaukhwan Feb 14 '21 at 13:41
-1

Try this to convert Object type of array to array of string

Object[] src = { 5.6, "Rupesh", 5, "India" };   
String[] dest = new String[src.length];
for(int i = 0; i < src.length; i++){
    dest[i] = src[i].toString();
}
System.out.println(Arrays.toString(dest));
Gautham M
  • 4,816
  • 3
  • 15
  • 37