当前位置:网站首页>Set interface
Set interface
2022-07-27 12:39:00 【Rippling rippling】
Relationship

Hashset Common methods
1.add(Object obj):
effect :
towards Set Add elements to the collection .
however , Adding duplicate data is not allowed .
because HashMap Of key Repetition is not allowed, so HashSet The added elements are not allowed to be repeated
HashSet add The essence of the method is map Global variable called put Method , Save the data to key.
notes : It does not guarantee set Order of iteration ; In particular, it does not guarantee that the order is permanent .
example :
package second;
import java.util.HashMap;
import java.util.HashSet;
public class Test {
public static void main(String [] args) {
HashSet<String> set =new HashSet<String>();
set.add("ufdhu");
set.add("hwsfihojnckfh");
for(String name:set) {
System.out.println(name);
}
}
}
result :
ufdhu
hwsfihojnckfh
If repeat :
example :
package second;
import java.util.HashMap;
import java.util.HashSet;
public class Test {
public static void main(String [] args) {
HashSet<String> set =new HashSet<String>();
set.add("ufdhu");
set.add("hwsfihojnckfh");
set.add("hwsfihojnckfh");
System.out.println(set.size());
for(String name:set) {
System.out.println(name);
}
}
}
result :
2
ufdhu
hwsfihojnckfh
2.size() :
effect :
return Set The number of elements in the collection
example :
package second;
import java.util.HashMap;
import java.util.HashSet;
public class Test {
public static void main(String [] args) {
HashSet<String> set =new HashSet<String>();
set.add("ufdhu");
set.add("hwsfihojnckfh");
System.out.println(set.size());
for(String name:set) {
System.out.println(name);
}
}
}
result :
2
ufdhu
hwsfihojnckfh
3.remove(Object obj) :
effect :
Delete Set The elements in the collection , Delete successful return true, Otherwise return to false.
example :
package second;
import java.util.HashMap;
import java.util.HashSet;
public class Test {
public static void main(String [] args) {
HashSet<String> set =new HashSet<String>();
set.add("ufdhu");
set.add("hwsfihojnckfh");
System.out.println(set.size());
for(String name:set) {
System.out.println(name);
}
boolean flag1=set.remove("fdfsfs");
System.out.println(flag1);
boolean flag2=set.remove("ufdhu");
System.out.println(flag2);
}
}
result :
2
ufdhu
hwsfihojnckfh
false
true
4.isEmpty() :
effect :
If Set No elements , Then return to true , Otherwise return to false.
example :
package second;
import java.util.HashMap;
import java.util.HashSet;
public class Test {
public static void main(String [] args) {
HashSet<String> set =new HashSet<String>();
set.add("ufdhu");
set.add("hwsfihojnckfh");
System.out.println(set.isEmpty());
boolean flag1=set.remove("hwsfihojnckfh");
boolean flag2=set.remove("ufdhu");
System.out.println(set.isEmpty());
}
}
result :
false
true
5.clear() :
effect :
To move in Set All elements in .
example :
package second;
import java.util.HashMap;
import java.util.HashSet;
public class Test {
public static void main(String [] args) {
HashSet<String> set =new HashSet<String>();
set.add("ufdhu");
set.add("hwsfihojnckfh");
System.out.println(set.isEmpty());
set.clear();
System.out.println(set.isEmpty());
}
}
result :
false
true
6.iterator() :
effect :
Back here Set Iterators that iterate over elements in
example :
package second;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
HashSet<String> set =new HashSet<String>();
set.add("ufdhu");
set.add("hwsfihojnckfh");
System.out.println(set.isEmpty());
Iterator<String> iterator =set.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
result :
false
ufdhu
hwsfihojnckfh
7.contains(Object o):
effect :
Determine whether the set contains obj Elements .
If Set Contains the specified elements , Then return to true, Otherwise return to false
example :
package second;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
HashSet<String> set =new HashSet<String>();
set.add("ufdhu");
set.add("hwsfihojnckfh");
System.out.println(set.isEmpty());
System.out.println(set.contains("ufdhu"));
System.out.println(set.contains("dfff"));
}
}
result :
false
true
false
边栏推荐
- Chapter 13 IO flow
- An overview of kernel compilation system
- 详述反射中构造方法、属性和普通方法
- II. Analysis of the execution process of make menuconfig
- 堆
- Simple blockchain day based on bolt database (2)
- 2021-03-15
- US pressure surges tiktok changes global safety director
- P1321 word overlay restore [getting started]
- Solution: the idea project does not display a tree view
猜你喜欢

Wechat applet session holding

The song of the virtual idol was originally generated in this way!

Why does MySQL index use b+ tree instead of jump table?

Fundamentals of mathematics 02 - sequence limit
QT | qcheckbox of control

Watermelon Book + pumpkin book chapter 1-2

Set接口

详述HashSet的add方法

Detail throw and throws

JS true / false array conversion
随机推荐
Redis distributed online installation
Plus版SBOM:流水线物料清单PBOM
Application parameters of Southern biotech HRP labeled avidin avidin avidin
2022 global Vocational Education Industry Development Report
关于 CMS 垃圾回收器,你真的懂了吗?
Keil mdk5.37 and above can add AC5 (armcc) compiler by themselves
Top 10 international NFT exchanges
POJ1988_ Cube Stacking
js日期时间格式化(年月日、时分秒、星期、季度、获取时间差、日期与时间戳转换的功能)
Lambda 表达式
为什么需要外键?
Chain representation and implementation of queues
2021-3-17-byte-hu Pai
[database data recovery] a data recovery case in which the disk partition where the SQL Server database is located is insufficient and an error is reported
Error: slf4j: class path contains multiple slf4j bindings
II. Analysis of the execution process of make menuconfig
SSM practical project - front back separation (easy to understand)
JVM memory layout detailed, illustrated, well written!
关于 CMS 垃圾回收器,你真的懂了吗?
POJ1611_The Suspects