Advertisement
Jgug

Comparable TreeSet of int[]

Nov 11th, 2014
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. package by.vshkl;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. import java.util.Set;
  6. import java.util.TreeSet;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         int[] arr1 = {1,2,3,4};
  12.         int[] arr2 = {2,3,4,5};
  13.         int[] arr3 = {3,4,5,6};
  14.         int[] arr4 = {4,5,6,7};
  15.         int[] arr5 = {1,2,3,4};
  16.         int[] arr6 = {3,4,5,6};
  17.         Set<int[]> set = new TreeSet<>(new Comparator<int[]>() {
  18.             @Override
  19.             public int compare(int[] o1, int[] o2) {
  20.                 return Arrays.equals(o1, o2) ? 0 : Arrays.hashCode(o1) - Arrays.hashCode(o2);
  21.             }
  22.         });
  23.  
  24.         set.add(arr1);
  25.         set.add(arr2);
  26.         set.add(arr3);
  27.         set.add(arr4);
  28.         set.add(arr5);
  29.         set.add(arr6);
  30.  
  31.         for (int[] i : set) {
  32.             for (int j : i) {
  33.                 System.out.print(j + " ");
  34.             }
  35.             System.out.println();
  36.         }
  37.     }
  38. }
  39.  
  40. // OUTPUT
  41. // 1 2 3 4
  42. // 2 3 4 5
  43. // 3 4 5 6
  44. // 4 5 6 7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement