java中HashSet有什么用,举例说明?

2.1 HashSet的用法 

对象也必须定义hashCode(),比如下面例子2.1.1中的String类中就定义了hashCode方法。 h.add("1");  但随着Set的增大,它的性能不会大打折扣。这是由Hash的低层结构决定的。HashSet不保证输出的顺序。

例:2.1.1

import java.util.*;

public class TestMark_to_win {
    public static void main(String args[]) {
        HashSet h = new HashSet();
        h.add("1");
        h.add("2");
        h.add("3");
        h.add("4");
        System.out.println(h);
    }
}

 

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/102707052

posted @ 2021-06-24 08:56  小龙虾1  阅读(117)  评论(0)    收藏  举报