0
ArrayList<Integer> factors = new ArrayList<Integer>()

factors = [1, 183, 3, 61];

Collections.sort(factors); is => [1, 3, 61, 183]

How can I turn [1, 3, 61, 183] to this => "1 3 61 183"

1
  • is representation the only difference? when? how are you trying to access the list? Commented Dec 4, 2018 at 15:23

4 Answers 4

1

Instead of using Collections.sort(factors) (or factors.sort(null)), you can use a Stream to sort and then collect it to String using Collectors.joining.

String result = factors.stream()                           // iterate all the list
                       .sorted()                           // make it sorted
                       .map(Number::toString)              // convert Number to String
                       .collect(Collectors.joining(" "));  // collect them to String

The most confusing parts for you might be map and collect, let me explain more in detail:

  • map(Number::toString) is the same as map(n -> n.toString()) which calls toString method to each of the element iterated through. It results from Stream<String> in `Stream.
  • collect(..) takes all the Stream as is and using a Collector creates an output from the streamed elements. It might be a Map, List or any object T - it depends on the Collector.
  • Collectors.joining(" ") is a collector, that requires Stream<String> and concatenate elements together with the delimiter, which is one empty space in our case " ".
Sign up to request clarification or add additional context in comments.

4 Comments

Consider marking this or any other answer which you consider as the most helpful as accepted - it says the community that the issue has been resolved.
That's not sorted though.
@Nikolas if you have time, can you please walk me through your code? It works but I do not fully understand it. You can also send me links so I can read up on this. Thank you.
@fabtree: See the edit - there is a short explanation in my answer included :)
0

The following code sample should accomplish this. We are declaring a new array to hold the string values (Setting it equal to the same size as the integer array), and filling out with the string values of the integers.

String myStringArray[] = new String[factors.length];

for (int i = 0; i < factors.length; i++){
    myStringArray[i] = String.valueOf(intArray[i]);
    System.out.println(Arrays.toString(myStringArray));
}//end for

Comments

0

You can use call toString of ArrayList and remove brackets like this,

import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> factors = Arrays.asList(1, 183, 3, 61);
        String arrayString = factors.toString();
        System.out.println(arrayString.substring(1, arrayString.length() - 1).replace(",", ""));
    }
}

4 Comments

Cannot resolve method 'toString(java.util.ArrayList<java.lang.Integer>) this is the error I get and Arrays.toString(factors) factors is underlined with red in intelliJ
@fabtree thats because you were using an ArrayList and first I thought you were using a int[] type array. I have updated my code. Please check it :)
You forgot to remove commas separating the elements.
@Nikolas My bad... Thanks for showing. I have updated the code
0

You may convert any object to string directly using toString() method of that object. here you can manipulate string functions as per your requirement.

replace brackets with double quotes as below

String s = factors.toString();
s=s.replace("[", "\"");
s=s.replace("]", "\"");

or you may only remove square brackets using substring mathod of string and make it useful. as below

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

remove comma using below method

s=s.replace(",","");

try it , may it will help you.

1 Comment

You forgot to remove commas separating the elements.

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.