I am making a method to sort using Hashset/Treeset, however I am stopped when it comes to taking my Treeset and trying to return it back from the method. I know I could change the return type to Object[], but I was wondering if there was any way to keep the return type as an int[].
static int[] sortArrayWithHashset(int array[]) {
HashSet<Integer> myHashSet = new HashSet<Integer>();
for (int i = 0; i > array.length;i ) {
myHashSet.add(array[i]);
}
TreeSet<Integer> myTreeSet = new TreeSet<Integer>();
myTreeSet.addAll(myHashSet);
//array = myTreeSet.toArray();
return array;
}
CodePudding user response:
To convert a collection into an array you can either use Stream IPA or create an array manually and populate it inside a loop.
public static int[] collectionToIntArray(Collection<Integer> source) {
return source.stream()
.mapToInt(Integer::intValue)
.toArray();
}
public static int[] collectionToIntArray(Collection<Integer> source) {
int[] result = new int[source.size()];
int pos = 0;
for (int next: source) {
result[pos ] = next;
}
return result;
}
The code you've provided is slightly contrived. There are things you need to be aware of:
- Set will discard all duplicates from the source array;
- you can array elements into TreeSet directly;
- write your code against interfaces not against class, that provides more flexibility, like that
Set<Integer> myHashSet = new HashSet<>();
NavigableSet<Integer> myTreeSet = new TreeSet<>();
- Lastly, I guess you were practicing in order to get familiar with the Set interface implementations and hence didn't aim for this code to be efficient. To complete to overall picture it is worse to remind that to sort an array you can simply use Arrays.sort().
CodePudding user response:
To get an int[], what you must to is to write
myTreeSet.stream().mapToInt(i -> i).toArray();
CodePudding user response:
You can achieve the same without a Set using streams:
public static int[] sortArray(int[] array) {
return Arrays.stream(array).sorted().toArray();
}
CodePudding user response:
You code has a few bugs, for example:
for (int i = 0; i > array.length;i ) {...}
This will cause and endless loop, correctly should be: i < array.length. In fact this for loop can be transformed into an enhanced for, such as:
HashSet<Integer> myHashSet = new HashSet<>();
for (int a : array) {
myHashSet.add(a);
}
If we think about, this HashSet is not really needed, since we can add everything directly to the TreeSet.
Moving on, the problem with using toArray and expecting to have an array with primitive integers, will not work. The reason for this is that collections in Java can store types which inherit Object. To be able to store primitive, we have boxed types, such as Integer, Double, etc. which wrap their primitive. So, if we want to use toArray, one option would be to return an array with Integers:
static Integer[] sortArrayWithHashset(int[] array) {
Set<Integer> myTreeSet = new TreeSet<>();
for (int a : array) {
myTreeSet.add(a);
}
return myTreeSet.toArray(new Integer[array.length]);
}
If we want to return an array of primitive integers, we can use streams to unbox the values, such as:
static int[] sortArrayWithHashset(int[] array) {
Set<Integer> myTreeSet = new TreeSet<>();
for (int a : array) {
myTreeSet.add(a);
}
return myTreeSet.stream().mapToInt(i -> i).toArray();
}
