integer array in a collection - java

I would like to have a collection that holds arrays of integers.
List<int[]> pairs = new ArrayList<>();
in order to add there an element I have to:
int[] newArray = {1, 2};
pairs.add(newArray);
Can someone explain my why the below does not work:
pairs.add({1,2});
is there any other way to add {1,2} to pairs without creating newArray object?

Most of the time, you'll need to do new int[] { 1, 2 }:
pairs.add( new int[] {1,2} );
The only place that you can avoid the new int[] is when you're declaring a variable of type int[], as you've done with int[] newArray = {1, 2};. It's just a limitation of the language design. In particular, you can read 10.6. Array Initializers in the specification, which states that:
An array initializer may be specified in a declaration (§8.3, §9.3,
§14.4), or as part of an array creation expression (§15.10), to create
an array and provide some initial values.
The important thing to take away from that is that { 1, 2 }, is an array initializer, and you can use it in a declaration (int[] newArray = {1, 2};), or in an array creation expression (new int[] { 1, 2 }); you can't use it on its own.

You got the syntax slightly wrong.
Try this: pairs.add(new int[]{1,2});

Related

Problem when working with arrays in Java - code does not go inside the "if" statement [duplicate]

How do I convert an array to a list in Java?
I used the Arrays.asList() but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the web use the 1.4.2 behaviour.
For example:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.asList(numbers)
on 1.4.2 returns a list containing the elements 1, 2, 3
on 1.5.0+ returns a list containing the array 'numbers'
In many cases it should be easy to detect, but sometimes it can slip unnoticed:
Assert.assertTrue(Arrays.asList(numbers).indexOf(4) == -1);
In your example, it is because you can't have a List of a primitive type. In other words, List<int> is not possible.
You can, however, have a List<Integer> using the Integer class that wraps the int primitive. Convert your array to a List with the Arrays.asList utility method.
Integer[] numbers = new Integer[] { 1, 2, 3 };
List<Integer> list = Arrays.asList(numbers);
See this code run live at IdeOne.com.
In Java 8, you can use streams:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.stream(numbers)
.boxed()
.collect(Collectors.toList());
We cannot have List<int> as int is a primitive type so we can only have List<Integer>.
Java 16
Java 16 introduces a new method on Stream API called toList(). This handy method returns an unmodifiable List containing the stream elements. So, trying to add a new element to the list will simply lead to UnsupportedOperationException.
int[] ints = new int[] {1,2,3,4,5};
Arrays.stream(ints).boxed().toList();
Java 8 (int array)
int[] ints = new int[] {1,2,3,4,5};
List<Integer> list11 =Arrays.stream(ints).boxed().collect(Collectors.toList());
Java 8 and below (Integer array)
Integer[] integers = new Integer[] {1,2,3,4,5};
List<Integer> list21 = Arrays.asList(integers); // returns a fixed-size list backed by the specified array.
List<Integer> list22 = new ArrayList<>(Arrays.asList(integers)); // good
List<Integer> list23 = Arrays.stream(integers).collect(Collectors.toList()); //Java 8 only
Need ArrayList and not List?
In case we want a specific implementation of List e.g. ArrayList then we can use toCollection as:
ArrayList<Integer> list24 = Arrays.stream(integers)
.collect(Collectors.toCollection(ArrayList::new));
Why list21 cannot be structurally modified?
When we use Arrays.asList the size of the returned list is fixed because the list returned is not java.util.ArrayList, but a private static class defined inside java.util.Arrays. So if we add or remove elements from the returned list, an UnsupportedOperationException will be thrown. So we should go with list22 when we want to modify the list. If we have Java8 then we can also go with list23.
To be clear list21 can be modified in sense that we can call list21.set(index,element) but this list may not be structurally modified i.e. cannot add or remove elements from the list. You can also check this answer of mine for more explanation.
If we want an immutable list then we can wrap it as:
List<Integer> list22 = Collections.unmodifiableList(Arrays.asList(integers));
Another point to note is that the method Collections.unmodifiableList returns an unmodifiable view of the specified list. An unmodifiable view collection is a collection that is unmodifiable and is also a view onto a backing collection. Note that changes to the backing collection might still be possible, and if they occur, they are visible through the unmodifiable view.
We can have a truly immutable list in Java 9 and 10.
Truly Immutable list
Java 9:
String[] objects = {"Apple", "Ball", "Cat"};
List<String> objectList = List.of(objects);
Java 10 (Truly Immutable list):
We can use List.of introduced in Java 9. Also other ways:
List.copyOf(Arrays.asList(integers))
Arrays.stream(integers).collect(Collectors.toUnmodifiableList());
Speaking about conversion way, it depends on why do you need your List.
If you need it just to read data. OK, here you go:
Integer[] values = { 1, 3, 7 };
List<Integer> list = Arrays.asList(values);
But then if you do something like this:
list.add(1);
you get java.lang.UnsupportedOperationException.
So for some cases you even need this:
Integer[] values = { 1, 3, 7 };
List<Integer> list = new ArrayList<Integer>(Arrays.asList(values));
First approach actually does not convert array but 'represents' it like a List. But array is under the hood with all its properties like fixed number of elements. Please note you need to specify type when constructing ArrayList.
The problem is that varargs got introduced in Java 5 and unfortunately, Arrays.asList() got overloaded with a vararg version too. So Arrays.asList(numbers) is understood by the Java 5 compiler as a vararg parameter of int arrays.
This problem is explained in more details in Effective Java 2nd Ed., Chapter 7, Item 42.
I recently had to convert an array to a List. Later on the program filtered the list attempting to remove the data. When you use the Arrays.asList(array) function, you create a fixed size collection: you can neither add nor delete. This entry explains the problem better than I can: Why do I get an UnsupportedOperationException when trying to remove an element from a List?.
In the end, I had to do a "manual" conversion:
List<ListItem> items = new ArrayList<ListItem>();
for (ListItem item: itemsArray) {
items.add(item);
}
I suppose I could have added conversion from an array to a list using an List.addAll(items) operation.
Even shorter:
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Using Arrays
This is the simplest way to convert an array to List. However, if you try to add a new element or remove an existing element from the list, an UnsupportedOperationException will be thrown.
Integer[] existingArray = {1, 2, 3};
List<Integer> list1 = Arrays.asList(existingArray);
List<Integer> list2 = Arrays.asList(1, 2, 3);
// WARNING:
list2.add(1); // Unsupported operation!
list2.remove(1); // Unsupported operation!
Using ArrayList or Other List Implementations
You can use a for loop to add all the elements of the array into a List implementation, e.g. ArrayList:
List<Integer> list = new ArrayList<>();
for (int i : new int[]{1, 2, 3}) {
list.add(i);
}
Using Stream API in Java 8
You can turn the array into a stream, then collect the stream using different collectors: The default collector in Java 8 use ArrayList behind the screen, but you can also impose your preferred implementation.
List<Integer> list1, list2, list3;
list1 = Stream.of(1, 2, 3).collect(Collectors.toList());
list2 = Stream.of(1, 2, 3).collect(Collectors.toCollection(ArrayList::new));
list3 = Stream.of(1, 2, 3).collect(Collectors.toCollection(LinkedList::new));
See also:
Why do we use autoboxing and unboxing in Java?
When to use LinkedList over ArrayList?
Another workaround if you use Apache commons-lang:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.asList(ArrayUtils.toObject(numbers));
Where ArrayUtils.toObject converts int[] to Integer[]
In Java 9 you have the even more elegant solution of using immutable lists via the new convenience factory method List.of:
List<String> immutableList = List.of("one","two","three");
(shamelessly copied from here )
One-liner:
List<Integer> list = Arrays.asList(new Integer[] {1, 2, 3, 4});
If you are targeting Java 8 (or later), you can try this:
int[] numbers = new int[] {1, 2, 3, 4};
List<Integer> integers = Arrays.stream(numbers)
.boxed().collect(Collectors.<Integer>toList());
NOTE:
Pay attention to the Collectors.<Integer>toList(), this generic method helps you to avoid the error "Type mismatch: cannot convert from List<Object> to List<Integer>".
you have to cast in to array
Arrays.asList((Object[]) array)
Using Guava:
Integer[] array = { 1, 2, 3};
List<Integer> list = Lists.newArrayList(sourceArray);
Using Apache Commons Collections:
Integer[] array = { 1, 2, 3};
List<Integer> list = new ArrayList<>(6);
CollectionUtils.addAll(list, array);
I've had the same problem and wrote a generic function that takes an array and returns an ArrayList of the same type with the same contents:
public static <T> ArrayList<T> ArrayToArrayList(T[] array) {
ArrayList<T> list = new ArrayList<T>();
for(T elmt : array) list.add(elmt);
return list;
}
Given Array:
int[] givenArray = {2,2,3,3,4,5};
Converting integer array to Integer List
One way: boxed() -> returns the IntStream
List<Integer> givenIntArray1 = Arrays.stream(givenArray)
.boxed()
.collect(Collectors.toList());
Second Way: map each element of the stream to Integer and then collect
NOTE:
Using mapToObj you can covert each int element into string stream, char stream etc by casing i to (char)i
List<Integer> givenIntArray2 = Arrays.stream(givenArray)
.mapToObj(i->i)
.collect(Collectors.toList());
Converting One array Type to Another Type Example:
List<Character> givenIntArray2 = Arrays.stream(givenArray)
.mapToObj(i->(char)i)
.collect(Collectors.toList());
So it depends on which Java version you are trying-
Java 7
Arrays.asList(1, 2, 3);
OR
final String arr[] = new String[] { "G", "E", "E", "K" };
final List<String> initialList = new ArrayList<String>() {{
add("C");
add("O");
add("D");
add("I");
add("N");
}};
// Elements of the array are appended at the end
Collections.addAll(initialList, arr);
OR
Integer[] arr = new Integer[] { 1, 2, 3 };
Arrays.asList(arr);
In Java 8
int[] num = new int[] {1, 2, 3};
List<Integer> list = Arrays.stream(num)
.boxed().collect(Collectors.<Integer>toList())
Reference - http://www.codingeek.com/java/how-to-convert-array-to-list-in-java/
Can you improve this answer please as this is what I use but im not 100% clear. It works fine but intelliJ added new WeatherStation[0]. Why the 0 ?
public WeatherStation[] removeElementAtIndex(WeatherStation[] array, int index)
{
List<WeatherStation> list = new ArrayList<WeatherStation>(Arrays.asList(array));
list.remove(index);
return list.toArray(new WeatherStation[0]);
}
Use this to convert an Array arr to List.
Arrays.stream(arr).collect(Collectors.toList());
An example of defining a generic method to convert an array to a list:
public <T> List<T> fromArrayToList(T[] a) {
return Arrays.stream(a).collect(Collectors.toList());
}
use two line of code to convert array to list if you use it in integer value
you must use autoboxing type for primitive data type
Integer [] arr={1,2};
List<Integer> listInt=Arrays.asList(arr);
As of Java 8, the following should do
int[] temp = {1, 2, 3, 4, 5};
List<Integer> tempList = Arrays.stream(temp).boxed().collect(Collectors.toList());
If you are trying to optimize for memory, etc., (and don't want to pull in external libraries) it's simpler than you think to implement your own immutable "array view list" – you just need to extend java.util.AbstractList.
class IntArrayViewList extends AbstractList<Integer> {
int[] backingArray;
int size;
IntArrayViewList(int[] backingArray, int size) {
this.backingArray = backingArray;
this.size = size;
}
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int i = 0;
#Override
public boolean hasNext() {
return i < size;
}
#Override
public Integer next() {
return get(i++);
}
};
}
public int size() {
return size;
}
public Integer get(int i) {
return backingArray[i];
}
}
int is a primitive. Primitives can’t accept null and have default value. Hence, to accept null you need to use wrapper class Integer.
Option 1:
int[] nos = { 1, 2, 3, 4, 5 };
Integer[] nosWrapped = Arrays.stream(nos).boxed()   
.toArray(Integer[]::new);
nosWrapped[5] = null // can store null
Option 2:
You can use any data structure that uses the wrapper class Integer
int[] nos = { 1, 2, 3, 4, 5 };
List<Integer> = Arrays.asList(nos)
I started looking at this by trying to reduce the amount of code preparing the input of some test cases. I see a lot of effort around trying to include advanced and new features along with Arrays.asList(), but below the code chosen due simplicity:
//Integer input[]
List<Integer> numbers = Arrays.asList(new Integer[]{1, 2 ,3, 4, 5, 4, 3, 2, 1, 3, 4});
//String input[]
List<String> names = Arrays.asList(new String[]{"Jhon", "Lucas", "Daniel", "Jim", "Sam"});
//String input[]
List<Character> letters = Arrays.asList(new Character[]{'A', 'B', 'K', 'J', 'F'});
Please notice that Anonymous array example will work just with Arrays of Non Primitive Types as the API uses Generics, that's the reason you can see several 2 line examples around, more info here: Why don't Java Generics support primitive types?
For newer JDKs there is another simpler option, the below examples are equivalent to the ones show above:
//Integer
List<Integer> numbers = Arrays.asList(1, 2 ,3, 4, 5, 4, 3, 2, 1, 3, 4);
//String
List<String> names = Arrays.asList("Jhon", "Lucas", "Daniel", "Jim", "Sam");
//Character
List<Character> letters = Arrays.asList('A', 'B', 'K', 'J', 'F');

In Java, how can I pass an array directly as a parameter to a function?

I am working on a Java project and have a number of arrays generated by a different script (e.g. {1, 2, 3, 5}).
I have these arrays in a notepad and would like to pass them directly to a function which would put them in a hasmap of the form HashMap<String, Int[]>. These Hashmaps will subsequently be placed in another hashmap.
Now I wrote a function that took in the parameters necessary to create the larger hash (thus Hashmap<Int, Hashmap<String, Int[]>>) which takes Int, String, and Int[] as function parameters.
My problem is that Java wants me to first make up a Int[] (e.g. array_of_numbers1 = {1, 2, 3}) before allowing me to pass that Int[] as a parameter in the function that creates the HashMap.
Is there any way for me to do this directly, wihtout the need to pass to first create a Int[] variable before passing that into the function? So directly pass an array into a function call?
I would love for my function call to look like (where my function is: Public Void AddElement(Int, String, Int[]))
AddElement(1, "Numbers1", {1,2,3}) etc.
Is this way of passing parameters even possible? Could it be done in a different way so that I could still use a list, or a sequence of numbers which would together be placed in an array (in the function) so that I could would not have to initialize a Int[] array first (such as AddElement(1, "Numbers1", 1, 2, 3,....possibly more numbers or not))?
The error I get is "Array initializer not allowed here". What could be done to work around this problem?
Thanks in advance!
The syntax is:
AddElement(1, "Numbers1", new int[]{1,2,3})
——
Only when declaring a variable/field can you use the shorthand version, eg
int[] myArray = {1, 2, 3};
The standard form of making an array, initialized with numbers of your choosing, is:
new int[] {1,2,3}, e.g:
addElement(5, "Hello", new int[] {1, 2, 3});
You can omit the new int[] part of that, but only if you use this expression as the initial value of a new field or variable declaration, and not when passing to a method call:
int[] example = {1, 2, 3};.
If you make your method argument 'varargs', you can just pass an infinite amount of int parameters, though. You are probably looking for this:
public void addElement(int rowKey, String columnKey, int... values) {
map
.computeIfAbsent(rowKey, r -> new HashMap<>())
.computeIfAbsent(columnKey, c -> new HashMap<>())
.put(values);
}
addElement(5, "Hello", 1, 2, 3);
You may get a warning here in some IDEs, which is trying to say that if someone creates an array, and uses that to call a varargs method, they can then later change the array and this changes the array in your map store:
int[] example = {1, 2, 3};
addElement(5, "Hello", example);
example[0] = 6;
System.out.println(getElement(5, "Hello")[0]);
// prints 6 - that may not be what you want
If you dislike this; make a copy of the array in your addElement method: .put(Arrays.copyOf(values));
Note that this problem applies if you use int[] too; it's just assumed that you know about it if you write int[], so most IDEs don't generate the warning then.
I would love for my function call to look like (where my function is: Public Void AddElement(Int, String, Int[]))
AddElement(1, "Numbers1", {1,2,3}) etc.
Use varargs.
public void addElement(int i, String s, int... values)
addElement(1, "Numbers1", 1, 2, 3);
There can only be one varargs parameter, and it must be the last parameter.
Also, notice how almost all the words in the code starts with lowercase letter. The code in the question is not Java.

Java array initialization second variable type

int[] myArray = new int[5];
Hey guys,
I know there are other ways to initialize an array like:
int[] myArray = {1,2,3,4,5};
But can you tell me what the second int in the top example stands for?
I think the first "int" already specifies the array to have integer values, doesn't it?
The construct in int[] myArray = { 1, 2, 3, 4, 5 }; is handled as a special case. The compiler knows that { 1, 2, 3, 4, 5 } is creating an int[] array because of the declared type.
However, the full array creation syntax for the int array is new int[5] (it can vary), which tells the compiler that a new int[] array is created, regardless of the declared type:
For example, you can declare an Object variable, and assign an int array to it:
Object myArrayObject = new int[5]; //OK: this creates an int array
Whereas this won't work:
Object myArrayObject = { 1, 2, 3, 4, 5 }; //won't compile
With int and other primitive types, you're correct, the double declaration is largely superfluous.
But consider this example:
Object [] array = new String[5];
This is valid Java code (even though it is slightly broken*).
On a more general level it follows the same pattern as when you're initialising any variable with a new object:
Foo foo = new Foo();
The first Foo is the type of the variable (what shape the box is), the second is the type of the actual object (what shape is the thing you put in the box).
Note: Starting with Java 10, you can declare local variables with the keyword var, thus eliminating the need to specify the type twice:
var array = new int[5];
The compiler will infer from this that the type of array will be the same as the object it's initialised with. (int[] in this case).
*It is broken because if you try to put into the array anything other than a String, the code only fails at runtime, not during compilation.
So when you type new, you are specifying you want java to allocate memory to the heap. How much memory? Ah yes enough memory for 5 integers, int[5]. So java will allocated enough consecutive memory on the heap to store 5 integers.
int[] myArray = new int[5];
means you declare an array that can contain integer values called myArray (by int[] myArray) and you define it as an integer-array of size 5 afterwards (= new int[5];);
These steps are made inline, you could alternatively do the same in two lines:
int[] myArray; // declaration
myArray = new int[5]; // definition
First is the data type of the array. And other int is for the intialization of 5 integer object in the array

adding an ArrayList to ArrayList

Conceptually what is going on here?
I have an ArrayList
ArrayList<SomeClass> myArray = new ArrayList<SomeClass>();
then I add something to it like this
myArray.add(new myArray(1, 2, 3));
I have an array
ArrayList myArray = new ArrayList();
No, you don't have an array. Arrays are declared using brackes [], like this:
int[] myArray;
You have an object of ArrayList class. This object will use an array internally to store the data and dynamically change the array for a newer one when needs to assign more values.
then I add something to it like this
myArray.add(new myArray(1, 2, 3));
Conceptually, this is wrong and won't even compile. If you want to add more than one value in your array, you should use ArrayList#addAll method, that receives another collection. Then, you have two ways to pass a Collection in one statement:
Create a list using Arrays#asList:
myArray.addAll(Arrays.asList(1, 2, 3));
You can use double brace initialization:
myArray.addAll(new ArrayList() {{
add(1);
add(2);
add(3);
}});
You should take into account that you should specify the generic for your ArrayList instead of using raw data:
ArrayList<Integer> myArray = new ArrayList<Integer>();
myArray.addAll(new ArrayList<Integer>() {{
add(1);
add(2);
add(3);
}});
More info:
Java Tutorial. Collections
Initialization of an ArrayList in one line
Note that if you have an ArrayList that is tied to a custom class, let's say ArrayList<SomeClass> myArrayList, then the compiler prevents you to add elements to the list that doesn't pass the IS-A test (noted by instanceof operator), so this code:
ArrayList<SomeClass> myArrayList = new ArrayList<SomeClass>();
myArrayList..addAll(new ArrayList<Integer>() {{
add(1);
add(2);
add(3);
}});
Won't be able to compile since Integer is not a SomeClass. Similar with this piece of code:
ArrayList<SomeClass> myArrayList = new ArrayList<SomeClass>();
myArray.add(Arrays.asList(1, 2, 3));
But due to type erasure you can trick the compiler by using raw types. For example:
public void addData(ArrayList yourArrayList, Object anyData) {
yourArrayList.add(anyData);
}
Then in your code, you call this method:
ArrayList<String> myArrayList = new ArrayList<String>(); //allows Strings only
myArrayList.add("Hello world");
//myArrayList.add(0); //this won't compile because trying to add an Integer
addData(myArrayList, 0); //this will compile for using raw ArrayList and sending an Integer
System.out.println(myArrayList);
It will print:
[Hello world, 0]
myArray.add(new myArray(1, 2, 3));
This is not correct and don't compile, cause myArray is not a type, it's just a reference, you need something of type integer or if you want to add another list, then use addAll.
You could use something like this.
List<Integer> myArray = new ArrayList<>(); // specify with generics type parameter
//do some things and then
myArray.addAll(Arrays.asList(1,2,3)); // use this helper method to add a list to another list
Or if you want to only create and populate it, just use
List<Integer> myArray = Arrays.asList(1,2,3,4,5);
I think I know what you're trying to do, say you have this.
ArrayList<Integer[]> myArray = new ArrayList<Integer[]>();
and you wanted to add an array to it, you're basically going to call
myArray.add(((Integer[]) new int[] { 0, 1, 2 }));
Which will add the array [0, 1, 2] to the next index in the ArrayList.
For exmaple, the following code
ArrayList<Integer[]> myArray = new ArrayList<Integer[]>();
myArray.add(((Integer[]) new int[] { 0, 1, 2 }));
myArray.add(((Integer[]) new int[] { 1, 0, 2 }));
myArray.add(((Integer[]) new int[] { 3, 2, 1 }));
Would create the following results:
myArray index 0 = [0, 1, 2]
myArray index 1 = [1, 0, 2]
myArray index 2 = [3, 2, 1]
Is this your question?
You don't actually have an array, you have an ArrayList. An ArrayList is a resizable implementation of the List interface. This means, among other things, that you don't have to define a length like you would with an array.
I don't think the code you have provided will actually compile because when you go:
myArray.add(new myArray(1, 2, 3));
you haven't provided a type. You should go something like:
myArray.add(new ArrayList<Integer>(Arrays.asList(1,2,3)));
Using asList() will give almost the equivalent of assigning "default" values to an array. The array equivalent would look something like this:
Integer[] intArray = new Integer[] {1,2,3};
You should also parameterize your ArrayList(s), this basically means you are saying what that ArrayList can hold. For instance:
ArrayList<Integer>
Can hold Integers.
The reason you must use a wrapper class (Integer & not int) is because Collections in Java don't permit you to store primitive types such as int or double.
You can have a Collection holding another Collection. In this case you could go:
ArrayList<ArrayList<Integer>> myArrayList = new ArrayList<ArrayList<Integer>>();
myArrayList.add(new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
myArrayList.add(new ArrayList<Integer>(Arrays.asList(9, 12, 24)));
for (ArrayList<Integer> index : myArrayList) {
System.out.println(index);
}
Here you have an ArrayList holding two other ArrayLists. This results in the printing of:
[1, 2, 3]
[9, 12, 24]

Remove duplicate in a list which contains arrays

I have a list<array[]> in Java, and I want to remove from it all duplicates.
In addition, the array [1,2] is the same as array [2,1].
I want to use Set, but as I understand it, if I declare 2 arrays: int[] array1 = {1, 2} and int[] array2 = {1,2}, Java considers them as 2 different arrays. In addition, It doesn't help me in the case of [1,2] and [2,1]
How can I do it?
I'm not tested but you can do sort the array before adding into set
int[] array1 = {1, 2};
Arrays.sort(array1);
set.add(array1);
Use a HashSet and then add all the contents of your current list(s) into it. Then use the .toArray() method to get back a list.
HashSet<Integer> noDuplicates = new HashSet<Integer>();
Integer[] array = { 1, 2, 2, 3, 3, 4, 1};
System.out.println(Arrays.toString(array));
for (Integer i : array)
noDuplicates.add(i);
array = noDuplicates.toArray(new Integer[] {});
System.out.println(Arrays.toString(array));
If you have to check if two arrays are the same often, then you might just want to use a Set instead of an array as it'll save you the extra step of putting everything into a Set and then checking.
Use Iterator to iterate the list.
Use Arrays.equals(arg0, arg1) method to compare arrays inside list.
If you find equal arrays remove using iterator.remove().
List<int[]> b = new ArrayList<int[]>(); // Your list here
for (int[] ar : b) {
Arrays.sort(ar);
}
Set<int[]> a = new HashSet<>(b);

Categories

Resources