HashSet不';你不能保证分类吗? import java.util.*; 公共类DuplicateCheckMain{ 公共静态void main(字符串[]gopal){ 整数[]args={6,9,2,55100,1,6,8,9}; 整数[]args1={3,6,2,3,5}; Set S=新的HashSet(); 检查重复(S,args,新字符串(“HashSet”); Set S1=新的HashSet(); 检查重复(S1,args1,新字符串(“HashSet”); S=新树集(); 检查重复(S,args,新字符串(“树集”); S=新的LinkedHashSet(); 检查重复(S,args,新字符串(“LinkedHashSet”); } } 公共类重复检查{ 公共静态void checkDuplicate(集合S,整数[]参数,字符串setname){ for(int i=0;i

HashSet不';你不能保证分类吗? import java.util.*; 公共类DuplicateCheckMain{ 公共静态void main(字符串[]gopal){ 整数[]args={6,9,2,55100,1,6,8,9}; 整数[]args1={3,6,2,3,5}; Set S=新的HashSet(); 检查重复(S,args,新字符串(“HashSet”); Set S1=新的HashSet(); 检查重复(S1,args1,新字符串(“HashSet”); S=新树集(); 检查重复(S,args,新字符串(“树集”); S=新的LinkedHashSet(); 检查重复(S,args,新字符串(“LinkedHashSet”); } } 公共类重复检查{ 公共静态void checkDuplicate(集合S,整数[]参数,字符串setname){ for(int i=0;i,java,Java,HashSet绝对不能保证排序。排序根本不能保证 从迭代器()方法的文档中: 返回此集合中元素的迭代器。元素不按特定顺序返回 HashSet旨在通过相等的方式快速插入和检查元素的存在。仅此而已 如果需要排序,应使用SortedSet的实现,例如TreeSet或ConcurrentSkipListSet哈希集使用mod将数字存储到存储桶条目中 在args1数组中,所有数字都小于默认哈希集大小16。这就是它最终被排序的原因。HashSet不保证排序。要具有排序功能,请使用TreeSet或类似的方法。

HashSet
绝对不能保证排序。排序根本不能保证

迭代器()
方法的文档中:

返回此集合中元素的迭代器。元素不按特定顺序返回

HashSet
旨在通过相等的方式快速插入和检查元素的存在。仅此而已


如果需要排序,应使用
SortedSet
的实现,例如
TreeSet
ConcurrentSkipListSet

哈希集使用mod将数字存储到存储桶条目中


args1
数组中,所有数字都小于默认哈希集大小16。这就是它最终被排序的原因。

HashSet
不保证排序。要具有排序功能,请使用
TreeSet
或类似的方法。

java.util.Set
保证“没有重复的元素”


java.util.SortedSet
保证排序和“无重复元素”。

请注意,除了不保证排序外,
HashSet
的迭代顺序可能会在插入新元素时完全改变。例如:

import java.util.*;
public class DuplicateCheckMain {
 public static void main(String[] gopal){
  Integer[] args = {6,9,2,55,100,1,6,8,9};
  Integer[] args1 = {3,6,2,3,5};
  Set S = new HashSet();
  DuplicateCheck.checkDuplicate(S,args,new String("HashSet"));
  Set S1 = new HashSet();
  DuplicateCheck.checkDuplicate(S1,args1,new String("HashSet"));

  S = new TreeSet();
  DuplicateCheck.checkDuplicate(S,args,new String("TreeSet"));

  S = new LinkedHashSet();
  DuplicateCheck.checkDuplicate(S,args,new String("LinkedHashSet"));

 }
}

public class DuplicateCheck {

 public static void checkDuplicate(Set S, Integer[] args, String setname){
  for(int i = 0;i<args.length;i++){
   if(!S.add(args[i])){System.out.println("Duplicate element "+args[i]);}
  }
  System.out.println(S +" "+ setname);
 }
}

请注意插入单个元素是如何完全改变顺序的。

请使用代码标记。它会更可读!请提供输出?输出:Duplicate element 6 Duplicate element 9[1,100,2,55,6,8,9]哈希集Duplicate element 3[2,3,5,6]哈希集Duplicate element 6 Duplicate element 9[1,2,6,8,9,55,100]TreeSet Duplicate element 6 Duplicate element 9[6,9,2,55,100,1,8]LinkedHashSet您不需要复制字符串文字,它已经是字符串了。如果是这样的话,为什么它在命令行输入到main时不排序?提供的数组数小于16。@gopal请发布“命令行输入到main”代码…正如其他答案所指出的,
HashSet
不能保证排序。从来没有。我只是解释了为什么在
args1
的情况下它会被排序。
public class TestHashSet {
    public static void main(String[] foo) {
        Set<Integer> set = new HashSet<Integer>();
        set.addAll(Arrays.asList(new Integer[] {6,9,2,55,100,1,6,8,9}));
        System.out.println(set);
        set.addAll(Arrays.asList(new Integer[] {7,3,13,37,66}));
        System.out.println(set);
        set.add(42);
        System.out.println(set);
    }
}
[1, 100, 2, 55, 6, 8, 9]
[1, 100, 2, 3, 55, 66, 6, 37, 7, 8, 9, 13]
[1, 2, 100, 3, 6, 66, 7, 37, 8, 42, 9, 13, 55]