Skip to content

Instantly share code, notes, and snippets.

@drey7925
Created August 13, 2019 22:02
Show Gist options
  • Save drey7925/607329bdc33c27a9f3b6044beec48137 to your computer and use it in GitHub Desktop.
Save drey7925/607329bdc33c27a9f3b6044beec48137 to your computer and use it in GitHub Desktop.
| Welcome to JShell -- Version 11.0.3
| For an introduction type: /help intro
jshell> class Hack implements Comparator<int[]> {
...>
...> @Override
...> public int compare(int[] e1, int[] e2) {
...> int[] copy1 = Arrays.copyOf(e1, e1.length);
...> int[] copy2 = Arrays.copyOf(e2, e2.length);
...> Arrays.sort(copy1);
...> Arrays.sort(copy2);
...> return Arrays.equals(copy1, copy2) ? 0 : 1;
...> }
...> }
| created class Hack
jshell> Set<int[]> result = new TreeSet<>(new Hack());
result ==> []
jshell> int[] x = {1,2,3};
x ==> int[3] { 1, 2, 3 }
jshell> int[] z = {2,1,3};
z ==> int[3] { 2, 1, 3 }
jshell> int[] m = {2,1,3};
m ==> int[3] { 2, 1, 3 }
jshell>
jshell> result.add(x);
$6 ==> true
jshell> result.add(z);
$7 ==> false
jshell> result.add(m);
$8 ==> false
jshell>
jshell> for (int[] arr : result) {
...> System.out.println(Arrays.toString(arr));
...> }
[1, 2, 3]
jshell> result.add(new int[]{4,5,6})
$10 ==> true
jshell> result.add(new int[]{1,5,6})
$11 ==> true
jshell> result.add(new int[]{1,7,6})
$12 ==> true
jshell> result.add(new int[]{3,7,6})
$13 ==> true
jshell> result.add(new int[]{2,3,1})
$14 ==> true
jshell> result.add(new int[]{4,5,6})
$15 ==> false
jshell> result.add(new int[]{4,6,5})
$16 ==> false
jshell> for (int[] arr : result) {
...> System.out.println(Arrays.toString(arr));
...> }
[1, 2, 3]
[4, 5, 6]
[1, 5, 6]
[1, 7, 6]
[3, 7, 6]
[2, 3, 1]
jshell>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment