Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using built-in methods and objects, it would likely be faster than most custom implementations.
Notice that HashSet
:
makes no guarantees as to the insertion order of the set; in particular it does not guarantee that the order will remain constant over time.
If you need to keep insertion order, use LinkedHashSet
as suggested in @mrblewog's comment. If you want to have the internal Set
sorted, use SortedSet
or TreeSet
.
I also changed int[]
to Integer[]
so that it can be used with generics (generics do not support primitive types).
Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using built-in methods and objects, it would likely be faster than most custom implementations.
Notice that HashSet
:
makes no guarantees as to the insertion order of the set; in particular it does not guarantee that the order will remain constant over time.
If you need to keep insertion order, use LinkedHashSet
as suggested in @mrblewog's comment. If you want to have the internal Set
sorted, use SortedSet
or TreeSet
.
I also changed int[]
to Integer[]
so that it can be used with generics (generics do not support primitive types).
Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using built-in methods and objects, it would likely be faster than most custom implementations.
Notice that HashSet
:
makes no guarantees as to the insertion order of the set; in particular it does not guarantee that the order will remain constant over time.
If you need to keep insertion order, use LinkedHashSet
as suggested in @mrblewog's comment. If you want to have the internal Set
sorted, use SortedSet
or TreeSet
.
I also changed int[]
to Integer[]
so that it can be used with generics (generics do not support primitive types).
Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using built-in methods and objects, it would likely be faster than most custom implementations.
Notice that it returns aHashSet
:
makes no guarantees as to the insertion order of the set; in particular it does not guarantee that the order will remain constant over time.
If you need to keep insertion order, use LinkedHashSet
as suggested in @mrblewog's comment. If you want to have the internal Set
sorted array, rather than an unsorted oneuse SortedSet
or TreeSet
.
I also changed int[]
to Integer[]
so that it can be used with generics (generics do not support primitive types).
Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using built-in methods and objects, it would likely be faster than most custom implementations.
Notice that it returns a sorted array, rather than an unsorted one. I also changed int[]
to Integer[]
so that it can be used with generics (generics do not support primitive types).
Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using built-in methods and objects, it would likely be faster than most custom implementations.
Notice that HashSet
:
makes no guarantees as to the insertion order of the set; in particular it does not guarantee that the order will remain constant over time.
If you need to keep insertion order, use LinkedHashSet
as suggested in @mrblewog's comment. If you want to have the internal Set
sorted, use SortedSet
or TreeSet
.
I also changed int[]
to Integer[]
so that it can be used with generics (generics do not support primitive types).
Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using buildbuilt-in methods and objects, it would likely be faster than most custom implementations.
Notice that it returns a sorted array, rather than an unsorted one. I also changed int[]
to Integer[]
so that Iit can be used with generics (generics do not support primitive types).
Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using build-in methods and objects, it would likely be faster than most custom implementations.
Notice that it returns a sorted array, rather than an unsorted one. I also changed int[]
to Integer[]
so that I can be used with generics (generics do not support primitive types).
Wouldn't it be easier to just use:
public static class IntArrayProcessor {
private Integer[] arr;
public IntArrayProcessor(Integer[] arr) {
this.arr = arr;
}
public Integer[] unique() {
final Set<Integer> uniqueSet = new HashSet<Integer>(Arrays.asList(this.arr));
return uniqueSet.toArray(new Integer[uniqueSet.size()]);
}
}
I changed the method name from getSet()
to the more descriptive unique()
.
This simply generates a hash set, which contains only the unique values of the array. The hash set (uniqueSet
) is then converted from Set
to Integer[]
, and returned.
Given that this is using built-in methods and objects, it would likely be faster than most custom implementations.
Notice that it returns a sorted array, rather than an unsorted one. I also changed int[]
to Integer[]
so that it can be used with generics (generics do not support primitive types).