3

I originally have an arraylist of strings but I want to save it as an arraylist of those strings.toCharArray() instead. Is it possible to make an arraylist that stores char arrays? Here is how I tried to implement it.

String[] words = new String[]{"peter","month","tweet", "pete", "twee", "pet", "et"};
    HashMap<Integer,ArrayList<Character[]>> ordered = new HashMap<>();

    int length = 0;
    int max = 0; //max Length of words left

    for(String word: words){

        if(ordered.containsKey(length) == false){ //if int length key doesnt exist yet
             ordered.put(length, new ArrayList<Character[]>()); //put key in hashmap with value of arraylist with the one value
             ordered.get(length).add(word.toCharArray());
        }
    }

1 Answer 1

3

Note that toCharArray() returns an array of primitives (char[]), and not an array of the boxing class (Character[] as you currently have). Additionally, you're only adding the given array to the map if the length of the array isn't in the map, which probably isn't the behavior you wanted (i.e., you should move the line ordered.get(length).add(word.toCharArray()); outside the if statement).

Also, note that Java 8's streams can do a lot of the heavy lifting for you:

String[] words = new String[]{"peter","month","tweet", "pete", "twee", "pet", "et"};
Map<Integer, List<char[]>> ordered =
    Arrays.stream(word)
          .map(String::toCharArray)
          .collect(Collectors.groupingBy(x -> x.length));

EDIT:
As per the question in the comment, this is also entirely possible in Java 7 without streams:

String[] words = new String[]{"peter","month","tweet", "pete", "twee", "pet", "et"};
Map<Integer, List<char[]>> ordered = new HashMap<>();

for (String word: words) {
    int length = words.length();

    // if int length key doesnt exist in the map already
    List<char[]> list = orderd.get(length);
    if (list == null) {
        list = new ArrayList<>();
        orderd.put(length, list);
    }
    list.add(word);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ohhh thank you, can you speak more on the char[] and the boxing class Character[]. I thought i had to have the <Character> and not <char> in the generic for the map. I'm going to look into Java 8 streams later but is there a way to do this in java 7?
@PeterLuo Arrays are reference types in Java, so char[] can be used as a type argument. Since char is a primitive type, though, it cannot (yet). See below for an answer to your Java 7 question.
@PeterLuo Logan's comment basically explains the char vs Character point. WRT implementing this in Java 7 - it's a bit clunkier, but entirely possible. See my edited answer for details.

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.