How to print out a hashset

25,389

You have to print the content of your array. Arrays.toString(char[] a) will do that for you :

System.out.println(" Iterating over HashSet in Java current object: " + Arrays.toString(itr.next()));
Share:
25,389
user3097544
Author by

user3097544

Updated on December 14, 2020

Comments

  • user3097544
    user3097544 over 3 years

    I just now started working with hashsets and I am trying to print it out but all it prints out is its location i tried Iterator and .toString() here is my code :

    char [] a = RandomWord.toCharArray();
            HashSet<char[]>  set = new HashSet<char[]>();
            set.add(a);
              Iterator<char[]> itr = set.iterator();
                while(itr.hasNext()){
                    System.out.println(" Iterating over HashSet in Java current object: " + itr.next().toString());
                }
        }