当前位置:网站首页>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 "); } }
边栏推荐
- Inter thread synchronization semaphore control
- Precautions for function default parameters (formal parameter angle)
- 地理位置数据存储方案——Redis GEO
- Once the code was encrypted by the company's computer, the compilation failed
- Introduction to MgO 256gb NAND flash chip
- Power representation in go language
- Bugly hot update usage
- When inputting text in the shutter textfield, if the page is refreshed, the cursor position will change.
- Several ways of SQL optimization
- Check whether the port number is occupied
猜你喜欢
Consumer and producer cases of inter thread synchronization (condition variable)

合宙Air32F103CBT6開發板上手報告

Educational administration system development (php+mysql)

Resolve the format conflict between formatted document and eslint

The style of the mall can also change a lot. DIY can learn about it!

Uncover gaussdb (for redis): comprehensive comparison of CODIS

Continuous integration of aspnetcore & cloud flow

Learning notes of rxjs takeuntil operator

Vscode有什么好用的插件?
Inter thread synchronization semaphore control
随机推荐
DOM event flow, event delegate
一行代码可以做什么?
mysql整体架构和语句的执行流程
Dart syntax
Detailed explanation of IVX low code platform series -- Overview (I)
Constructor Pattern
一文带你搞懂 JWT 常见概念 & 优缺点
GO语言-锁操作
不要再「外包」AI 模型了!最新研究发现:有些破坏机器学习模型安全的「后门」无法被检测到
Read the configuration, explain the principle and read the interview questions. I can only help you here...
Swift responsive programming
地理位置数据存储方案——Redis GEO
leetcode-8. String to integer (ATOI)
What exactly is a handler
Write one file to the marked location of another file
sql优化的几种方式
Dino: Detr with improved detecting anchor boxes for end to end object detection
Resolve the format conflict between formatted document and eslint
Mt60b1g16hc-48b:a micron memory particles FBGA code d8bnk[easy to understand]
JS add custom attributes to elements