当前位置:网站首页>Hash table, generic
Hash table, generic
2022-06-25 16:19:00 【weixin_ forty-eight million six hundred and forty-four thousand】
- Set
- HashSet Use
public static void main(String[] args) { HashSet set = new HashSet(); set.add(1); set.add("asd"); set.remove("asd"); System.out.println(set.size()); set.isEmpty(); for (Object object : set) { } }
- Hash table
- summary
- HashSet
The above two screenshots can be seen , When we use HashSet When , In fact, it is equal to using again HashMap
When adding data , Even though it's called HashSet Of add Method , But the essence is to call map Of put Method
Ps : stay map in ,put Is an add operation
and map in What needs to be saved is k and v The mapping relationship , So in set There is a variable in that holds value Value
So let's go on set When adding , It's just the operation map Medium key,value We no longer care about
- Map
- HashMap
// establish map
HashMap map = new HashMap();
// add to K-V
map.put("A", "one");
map.put("B", "two");
map.put("C", "three");
map.put(65,100);
map.put('A', "2222");
// key repeat , Don't add ,value Replace
map.put("A", "2222");
// Support K and V all null, But it doesn't make sense
map.put(null,null);
// Number
System.out.println(map.size());
// get : according to K obtain V Value
System.out.println(map.get("A"));
// To determine whether or not to include a key
System.out.println(map.containsKey(65));
// To determine whether or not to include a value
System.out.println(map.containsValue("one"));
// according to key Delete The mapping relationship , Return the corresponding value value
map.remove(65);
// Get all value, And put it in the collection to return
Collection values = map.values();
for (Object object : values) {
System.out.println(object);
}
System.out.println("======");
// keySet : Get all key, Package to set Object and return
Set keys = map.keySet();
for (Object object : keys) {
System.out.println(object +":"+map.get(object));
}
// hold map Convert to set
// Entry Class , Save the K and V Two variables , hold map Medium k and v Convert to entry Class
// So we just need to save entry object , It's like saving k and v
Set set = map.entrySet();
for (Object object : set) {
// C=three
System.out.println(object);
// Convert to entry type
Entry entry = (Entry) object;
// obtain k and v
System.out.println(entry.getKey()+" : "+entry.getValue());
}- TreeMap
- Generic
- summary
- Usage mode
public static void main(String[] args) {
// Add custom type , also Product in Realized Comparable Interface
TreeMap map = new TreeMap();
map.put(new Product(" Apple ", 5.3), 10);
map.put(new Product(" Banana ", 5.6), 10);
System.out.println(map.size());
// Add an existing type , And this type implements Comparable Interface ,Integer Default ascending order
// If The type does not implement Comparable Interface , perhaps We want to be in descending order , You need to use Comparator
// map = new TreeMap();
map = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
return i2 - i1;
}
});
map.put(11, "a");
map.put(2, "a");
map.put(23, "a");
map.put(3, "a");
System.out.println(map.size());
System.out.println(map);
}
}
class Product implements Comparable {
private String name;
private double price;
public Product(String name, double price) {
super();
this.name = name;
this.price = price;
}
@Override
public int compareTo(Object o) {
if (o instanceof Product) {
Product p = (Product) o;
if (this.price > p.price) {
return 1;
} else if (this.price < p.price) {
return -1;
}
}
return 0;
}
}- Be careful
- Custom generics
- topic
public static void main(String[] args) { // Generics are not used List list = new ArrayList(); // Anything can be put list.add(new A()); list.add(1); // When I pick it up , Get is Object for (Object object : list) { // Use requires judgment and downward transformation if (object instanceof A) { A a = (A) object; a.m1(); } } // Use generics Regulations In this collection Save only A type List<A> list2 = new ArrayList<A>(); list2.add(new A()); // Add other Report errors , And it is a compilation error // list2.add(2); // When you use it , What you get is A type , There will be no type conversion exception // Because as long as it can run , There must be A for (A a : list2) { a.m1(); } // Generic Cannot write basic type , Only reference types can be written // If you want to save basic types , You need to write the corresponding packing class type // List<int> list3 = new ArrayList<int>(); List<Integer> list4 = new ArrayList<Integer>(); // If Need to save a variety of data , You don't need to use generics // But if you need to save the same type of data , Be sure to use generics , It's easy to operate // set and map Use generics Set<String> set = new HashSet<String>(); Map<String, Integer> map = new HashMap<String, Integer>(); } } class A { public void m1(){ System.out.println("m1 Yes "); } }
边栏推荐
- Tensorflow loading cifar10 dataset
- 什么是NFT数字藏品?
- Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution
- Uniapp converts graphic verification codes in the form of file streams into images
- What exactly is a handler
- Go language - lock operation
- Dino: Detr with improved detecting anchor boxes for end to end object detection
- Rxjs TakeUntil 操作符的学习笔记
- JS add custom attributes to elements
- Principle analysis of ThreadLocal source code
猜你喜欢

Free books! AI across the Internet paints old photos. Here is a detailed tutorial!

error Parsing error: Unexpected reserved word ‘await‘.

Uncover gaussdb (for redis): comprehensive comparison of CODIS

炮打司令部,别让一个UI框架把你毁了

Detailed explanation of IVX low code platform series -- Overview (I)

The paid video at station B caused the up master to lose more than ten thousand fans

不要小看了积分商城,它的作用可以很大!

Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way

DOM event flow, event delegate

cmd。。。。。。
随机推荐
Message format of Modbus (PLC)
面试官:你简历上说精通mysql,那你说下聚簇/联合/覆盖索引、回表、索引下推
About the use of Aidl, complex data transmission
What are the reasons why the game industry needs high defense servers?
AutoK3s v0.5.0 发布 延续简约和友好
Built in function globals() locals()
LeCun预言AGI:大模型和强化学习都是斜道!我的「世界模型」才是新路
说下你对方法区演变过程和内部结构的理解
Overall MySQL architecture and statement execution process
什么是骨干网
Introduction to database transactions
Converting cifar10 datasets
Describe your understanding of the evolution process and internal structure of the method area
镁光256Gb NAND Flash芯片介绍
Summary of 2022 spring moves of ordinary people (Alibaba and Tencent offer)
基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022
Go language - lock operation
When inputting text in the shutter textfield, if the page is refreshed, the cursor position will change.
Webgl and webgpu comparison [4] - uniform
Shuttle pop-up returns to the upper level