当前位置:网站首页>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
边栏推荐
猜你喜欢

20210518-Cuda

Dominoes staged: the beginning and end of the three arrow capital crash

20210512 recursive formula

Watermelon book chapter 3 (first & second)

虚拟偶像的歌声原来是这样生成的!

Use redis' sorted set to make weekly hot reviews

Fundamentals of mathematics 01

Photoshop web design tutorial

Will MySQL fail to insert data? Why?

Set接口
随机推荐
What should I do if I can't see any tiles on SAP Fiori launchpad?
Detail throw and throws
2021-03-15
5V升压9V芯片
20210519 leetcode double pointer
20210422 consolidation interval
浪潮之巅——读书笔记+摘录+感悟
Chapter 13 IO flow
Vi. analysis of makefile.build
The song of the virtual idol was originally generated in this way!
What are metauniverse and NFT digital collections?
JVM memory layout detailed, illustrated, well written!
No matching distribution found for flask_ A solution to compat
Play CSDN editor
2021-3-23-meituan-regular sequence
Will MySQL fail to insert data? Why?
nodejs body-parser中间件处理类型 multipart/form-data 的 POST 表单数据,req.body无法接收到数据
详述jdbc查询方法执行过程
(07) flask is OK if you have a hand -- flask Sqlalchemy
Why does MySQL index use b+ tree instead of jump table?