0

I'm trying to remove the duplicates and sort the String array but I'm unable to do it. I'm not getting what I'm doing wrong. Please check the code.

String [] s = {"a", "y", "x","a","d", "y","m"};


Set st = new HashSet();
st.add(s);

Array.sort(st);

When I do this, I'm getting this error.

The method sort(int[]) in the type Arrays is not applicable for the arguments (Set).

As per my sense we should first remove duplicates by using the Set interface and then we have to sort them. But I'm unable to print even the st reference variable also([[Ljava.lang.String;@1d9dc39]) getting this error.

Please help me.

4
  • 10
    TreeSet is what you need . Commented Mar 21, 2014 at 18:24
  • @JasonSperske but im getting the following exception for treeset.Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(TreeMap.java:1188) at java.util.TreeMap.put(TreeMap.java:531) at java.util.TreeSet.add(TreeSet.java:255) at com.ani.sample.Mango.main(Mango.java:70) Commented Mar 21, 2014 at 18:31
  • With st.add(s); you are adding the array to the set. You have to add the elements of the array to the set. You can do this with set.addAll(Arrays.asList(s)) or a for loop. These problems could be avoided if you used generics.... Commented Mar 21, 2014 at 18:33
  • I added a quick implementation, but honestly there are a lot of great answers here Commented Mar 21, 2014 at 18:51

8 Answers 8

2

You cannot sort a set with the Array.sort().. As gravitas mentioned this is because a set has no order. It's just a mass of objects.

You have to add the elements in the set to an array or something else that has an order, and then sort them

Or you could use an arraylist, and use Collections.sort()

Something like

ArrayList<String> list = new ArrayList<>();
list.addAll(set);

Collections.sort(list);

This woiuld sort your list according to the natural order of the objects contained in the list.

Sign up to request clarification or add additional context in comments.

2 Comments

This makes sense because there is no such thing as order in a set. The concept of order exists in an array, however.
@gravitas: I'm getting the exception:Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(TreeMap.java:1188) at java.util.TreeMap.put(TreeMap.java:531) at java.util.TreeSet.add(TreeSet.java:255) at com.ani.sample.Mango.main(Mango.java:71)
1
   String [] s = {"a", "y", "x","a","d", "y","m"};
     Set st = new TreeSet();
     st.addAll(Arrays.asList(s));

1 Comment

prints:[a, d, m, x, y], you can convert it back to array with st.toArray() if you want.
1
public static String sortAndRemoveDuplicates(String s) {
    return List.of(s.split("")).stream()
        .sorted()
        .distinct()
        .collect(Collectors.joining());
}

Comments

0
List<String> sortedUnique = 
    new ArrayList<String>(new TreeSet<String>(Arrays.asList(s)));

Comments

0
  String [] s = {"a", "y", "x","a","d", "y","m"};
     List<String> list = Arrays.asList(s);
     Set<String> set = new HashSet<>(list);
     list = new ArrayList<>(set);
     Collections.sort(list);
     //Iterate the list

Comments

0

Hi You can use TreeSet

String [] s = {"a", "y", "x","a","d", "y","m"};

Set<String> newSet= new TreeSet<String>(Arrays.asList(s));

Comments

0

Here is a quick TreeSet example with output (I didn't know which answer to add this to, but feel free to copy it, as a lot of people here are telling the OP to do the same basic thing) (here it is cleaned up a bit).

import java.util.Arrays;
import java.util.TreeSet;

public class SO {
  public static void main(String[] args) {
    String[] s = {"a", "y", "x","a","d", "y","m"};
    System.out.println(Arrays.toString(s));
    //[a, y, x, a, d, y, m]
    System.out.println(Arrays.toString(clean(s)));
    //[a, d, m, x, y]
  }

  public static String[] clean(String[] input) {
    TreeSet<String> unique = new TreeSet<>(Arrays.asList(input));
    return unique.toArray(new String[unique.size()]);
  }
}

Comments

0

Hi solution for your problem is as below code

String [] s = {"a", "y", "x","a","d", "y","m"};

    List<String> list = Arrays.asList(s);

    TreeSet<String> sortedSet = new  TreeSet<String>(list);


    System.out.println(sortedSet);

here first convert the array of string into list, then put that list to treeset which will remove the duplicates and sorts it.

2nd way is add each string from the array into set(to remove duplicates) then convert it into list then use collections.sort(list);

code for the same is as below

HashSet<String> set = new HashSet<String>();

    for (String str : s) {
        set.add(str);

    }
    List<String> strArr = new ArrayList<String>(set);
    Collections.sort(strArr);

let me know if have any doubts.

Comments

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.