简体   繁体   中英

How can i check double array for uniqueness?

For example we have

String [][] doubleArray = {{"a","1"},{"b","3"},{"a","1"},{"c","1"}}; .

How can i create a new array with only uniqueness subarrays :

{{"a","1"},{"b","3"},{"c","1"}}

What i tryed :

Set <String []> uniq = new HashSet<String []>(Arrays.asList(doubleArray));

But Java doesnt see the difference between {"a","1"} and {"a","1"} , so uniq returns me set of all subarrays of doubleArray , including clones.

You can use Map like this:

String [][] doubleArray = {{"a","1"},{"b","3"},{"a","1"},{"c","1"}};
Map<String, String> map = new HashMap<>();
for (String[] strings : doubleArray) {
    map.put(strings[0], strings[1]);
}
System.out.println("map = " + map);

The result:

map = {a=1, b=3, c=1}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM