Java 提供了一个用于存储和操作数据的接口,称为集合接口。集合是 Set 接口的超级接口,有助于存储任何类型的对象并对其进行操作。 Set 接口作为一个不允许重复数据的 Collection 脱颖而出,即如果 d1 和 d2 是同一个 Set 中的两个数据条目,则 d1.equals(d2) 的结果应该为 false。 Set 中几乎允许有一个空元素。集合对数学集合抽象进行建模。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
集合的一些实现是 HashedSet、LinkedHashSet 或 TreeSet 作为排序表示。
用 Java 实现 Set 接口的示例
下面是Java中Set Interface的例子:
1.哈希集
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.*;
public class Main{
public static void main(String[] args)
{
Set<Integer > hash = new HashSet<Integer>();
hash.add(1);
hash.add(4);
hash.add(1);
hash.add(3);
hash.add(2);
System.out. print ( "Set output without duplicates" );
System.out.println(hash);
}
}
|
登录后复制
输出:

2.树集
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import java.util.*;
public class Main{
public static void main(String[] args)
{
Set<Integer> tree = new TreeSet<Integer>();
tree.add(1);
tree.add(4);
tree.add(1);
tree.add(3);
tree.add(2);
System.out. print ( "Set output without duplicates and sorted data " ); System.out.println(tree);
}
}
|
登录后复制
输出:

Java中Set接口的方法
Set 支持的用于各种数据对象存储和操作的方法。
-
add(Element e): 在集合中添加指定元素。
-
addAll(Collection> c): 添加指定集合中存在的所有元素。
-
clear():从集合中删除所有元素。
-
contains(Object o):如果 Set 包含与指定对象相同的对象,则返回 true。
-
containsAll(Collection> c): 如果 set 包含指定集合中的所有元素,则返回 true。
-
size(): 返回 Set 中元素的数量。
-
equals(Object o): 如果我们的对象等于指定的对象,则比较并返回 true。
-
hashCode():它返回集合的哈希码值。
-
isEmpty(): 如果集合不包含元素,则返回 true。
-
iterator():它返回集合上的迭代器,这有助于跟踪整个集合。
-
remove(Object o): 从现有集合中删除指定元素。
-
removeAll(Collection> c): 从现有集合中删除指定集合。
-
toArray(): 它返回包含 Set 中所有元素的特定数组。
我们代码中方法的使用:
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.LinkedHashSet;
public class Main
{
public static void main(String[] args)
{
LinkedHashSet<String> linked = new LinkedHashSet<String>();
linked.add( "Apple" );
linked.add( "Ball" );
linked.add( "Cat" );
linked.add( "Dog" );
linked.add( "Apple" );
linked.add( "Egg" );
System.out.println( "Size of LinkedHashSet: " + linked.size());
System.out.println( "Old LinkedHashSet:" + linked);
System.out.println( "Remove Dog from LinkedHashSet: " + linked.remove( "Dog" ));
System.out.println( "Trying Remove Zoo which is not present " + "present: " + linked.remove( "Zoo" ));
System.out.println( "Check if Apple is present=" + linked.contains( "Apple" ));
System.out.println( "New LinkedHashSet: " + linked);
}
}
|
登录后复制
输出:

将 HashSet 转换为 TreeSet
HashSet一般用于查找、删除和插入操作。 HashSet 比 TreeSet 更快并且使用哈希表。 TreeSet 由于其排序数据存储属性而用于存储目的。 TreeSet 在 Java 后端使用 TreeMap。为了存储排序的数据,请将元素插入到哈希图中,然后将数据插入到树中以对其进行排序。
有 3 种方法可以做到这一点:
1.传递创建的HashSet
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.*;
public class Main
{
public static void main(String[] args)
{
Set<Integer > hash = new HashSet<Integer>();
hash.add(1);
hash.add(4);
hash.add(1);
hash.add(3);
hash.add(2);
System.out. print ( "HashSet" );
System.out.println(hash);
Set< Integer> treeSet = new TreeSet<>(hash);
System.out.println( "TreeSet: " + treeSet);
}
}
|
登录后复制
输出:

2。 使用 addAll() 方法
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.*;
public class Main
{
public static void main(String[] args)
{
Set<Integer > hash = new HashSet<Integer>();
hash.add(1);
hash.add(4);
hash.add(1);
hash.add(3);
hash.add(2);
System.out. print ( "HashSet" );
System.out.println(hash);
Set<Integer> treeSet = new TreeSet<>();
treeSet.addAll(hash);
System.out.println( "TreeSet: " + treeSet);
}
}
|
登录后复制
输出:

3.使用 for-each 循环
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.*;
public class Main
{
public static void main(String[] args)
{
Set<Integer > hash = new HashSet<Integer>();
hash.add(1);
hash.add(4);
hash.add(1);
hash.add(3);
hash.add(2);
System.out. print ( "HashSet" );
System.out.println(hash);
Set<Integer> treeSet = new TreeSet<>();
for (Integer i : hash)
{
treeSet.add(i);
}
System.out.println( "TreeSet: " + treeSet);
}
}
|
登录后复制
输出:

推荐文章
这是 Java 中设置接口的指南。这里我们讨论Set接口的简介以及它如何在Java中存储和操作数据及其方法。您还可以浏览我们其他推荐的文章以了解更多信息 –
- Java 中的布局
- Java 编译器
- Java 并行流
- Java BufferedReader
以上是Java 中的设置接口的详细内容。更多信息请关注PHP中文网其他相关文章!