Storing arrays in Set and avoiding duplicates

前端 未结 5 1773
你的背包
你的背包 2020-12-24 06:09
HashSet boog = new HashSet();
boog.add(new String[]{\"a\", \"b\", \"c\"});
boog.add(new String[]{\"a\", \"b\", \"c\"});
boog.add(new          


        
5条回答
  •  轮回少年
    2020-12-24 06:38

    Actually, you can. You can use TreeSet with provided Comparator. In your case it will be something like:

    Set boog = new TreeSet<>((o1, o2) -> {
        for (int i = 0; i < o1.length; i++){
            int cmp = o1[i].compareTo(o2[i]);
            if (cmp != 0) {
                return cmp;
            }
        }
        return o1.length - o2.length;
    });
    

    Under the hood it will looks like alphabetic sorted tree.

提交回复
热议问题