当前位置: 首页 > news >正文

MongoDB - 聚合阶段 $match、$sort、$limit

文章目录

    • 1. $match 聚合阶段
      • 1. 构造测试数据
      • 2. $match 示例
      • 3. $match 示例
    • 2. $sort 聚合阶段
      • 1. 排序一致性问题
      • 2. $sort 示例
    • 3. $limit 聚合阶段

1. $match 聚合阶段

$match 接受一个指定查询条件的文档。

$match 阶段语法:

{ $match: { <query> } }

$match 查询语法与读取操作查询语法相同,即 $match 不接受原始聚合表达式。要在 $match 中包含聚合表达式,请使用 $expr 查询表达式:

{ $match: { $expr: { <aggregation expression> } } }

尽可能早地将 $match 放在聚合管道中,由于 $match 限制了聚合管道中的文档总数,因此早期的 $match 操作会最大限度地减少管道中的处理量。

1. 构造测试数据

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 60,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])

2. $match 示例

使用 $match 来执行简易等值匹配,$match 会选择 author 字段等于 dave 的文档,而聚合返回以下内容:

db.articles.aggregate([ { $match : { author : "dave" } } ]
);
// 1
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 2
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}

SpringBoot 整合 MongoDB 实现:

@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $match阶段Criteria criteria = Criteria.where("author").is("dave");MatchOperation match = Aggregation.match(criteria);Aggregation aggregation = Aggregation.newAggregation(match);// 执行聚合管道操作AggregationResults<Article> results= mongoTemplate.aggregate(aggregation, Article.class, Article.class);List<Article> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//Article(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//Article(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)}
}

这里输出文档直接使用了Article.class,可以重新定义实体类接收输出文档的字段:

@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $match阶段Criteria criteria = Criteria.where("author").is("dave");MatchOperation match = Aggregation.match(criteria);Aggregation aggregation = Aggregation.newAggregation(match);// 执行聚合管道操作AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)}
}

3. $match 示例

使用 $match 管道操作符选择要处理的文档,然后将结果导入到 $group 管道操作符,以计算文档的数量:

db.articles.aggregate( [// 第一阶段{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },// 第二阶段{ $group: { _id: null, count: { $sum: 1 } } }
] );

第一阶段:

$match 阶段选择 score 大于 70 但小于 90views 大于或等于 1000 的文档。

第二阶段:

m a t c h 阶段筛选的文档通过管道传送到 ‘ match 阶段筛选的文档通过管道传送到 ` match阶段筛选的文档通过管道传送到group` 阶段进行计数。

// 1
{"_id": null,"count": 5
}

SpringBoot 整合 MongoDB 实现:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}
@Data
public class AggregationResult {private String id;private Integer count;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// 第一阶段Criteria criteria = new Criteria();criteria.orOperator(Criteria.where("score").gt(70).lt(90), Criteria.where("views").gte(1000));MatchOperation match = Aggregation.match(criteria);// 第二阶段GroupOperation group = Aggregation.group().count().as("count");// 组合上面的2个阶段Aggregation aggregation = Aggregation.newAggregation(match,group);// 执行聚合管道操作AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);// AggregationResult(id=null, count=5)}
}

2. $sort 聚合阶段

$sort 将所有输入文档进行排序,然后按照排序将其返回至管道。

{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }

$sort 接受排序依据的字段及相应排序顺序的文档。当 sort order=1时升序排序,sort order=-1 降序排序。

如果对多个字段进行排序,则按从左到右的顺序进行排序。例如,在上面的表单中,文档首先按 field1 排序。然后,具有相同 field1 值的文档将按 field2 进一步排序。

1. 排序一致性问题

MongoDB 不按特定顺序将文档存储在集合中。对包含重复值的字段进行排序时,可能会以任何顺序返回包含这些值的文档。如果需要一致的排序顺序,请在排序中至少纳入一个包含唯一值的字段。

db.restaurants.drop()db.restaurants.insertMany( [{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
] )

以下命令使用 $sort 阶段对 borough 字段进行排序:

db.restaurants.aggregate([{ $sort : { borough : 1 } }]
)

在此示例中,排序顺序可能不一致,因为 borough 字段包含 ManhattanBrooklyn 的重复值。文档按 borough 的字母顺序返回,但具有 borough 重复值的文档的顺序在多次执行同一排序中可能不相同。

要实现一致的排序,可以在排序中添加一个仅包含唯一值的字段。以下命令使用 $sort 阶段对 borough 字段和 _id 字段进行排序:

db.restaurants.aggregate([{ $sort : { borough : 1, _id: 1 } }]
)

由于 _id 字段始终保证包含唯一值,因此在同一排序的多次执行中返回的排序顺序将始终相同。

2. $sort 示例

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])

对于要作为排序依据的一个或多个字段,可以将排序顺序设置为 1-1 以分别指定升序或降序。

db.articles.aggregate([{ $sort : { score : -1, views: 1 } }]
)
// 1
{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000
}// 2
{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999
}// 3
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}// 4
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 5
{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000
}// 6
{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50
}// 7
{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000
}

SpringBoot 整合 MongoDB:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $sort阶段SortOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC, "score")).and(Sort.by(Sort.Direction.ASC, "views"));Aggregation aggregation = Aggregation.newAggregation(sortOperation);// 执行聚合查询AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=55f5a1d3d4bede9ac365b25b, author=ty, score=95, views=1000)//AggregationResult(id=55f5a1d3d4bede9ac365b25a, author=li, score=94, views=999)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=55f5a192d4bede9ac365b257, author=ahn, score=60, views=1000)//AggregationResult(id=55f5a1d3d4bede9ac365b259, author=annT, score=55, views=50)//AggregationResult(id=55f5a192d4bede9ac365b258, author=li, score=55, views=5000)}
}

3. $limit 聚合阶段

$limit 聚合阶段限制传递至管道。$limit 取一个正整数,用于指定传递的最大文档数量:

{ $limit: <positive 64-bit integer> }

如果将 $limit 阶段与以下任何一项一起使用:

  • $sort 聚合阶段
  • sort() 方法
  • sort 命令

在将结果传递到$limit阶段之前,请务必在排序中至少包含一个包含唯一值的字段。

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 60,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])
db.articles.aggregate([{ $limit : 5 }
]);
// 1
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 2
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}// 3
{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000
}// 4
{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000
}// 5
{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50
}

SpringBoot 整合 MongoDB:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $sort阶段LimitOperation limitOperation = Aggregation.limit(5);Aggregation aggregation = Aggregation.newAggregation(limitOperation);// 执行聚合查询AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)//AggregationResult(id=55f5a192d4bede9ac365b257, author=ahn, score=60, views=1000)//AggregationResult(id=55f5a192d4bede9ac365b258, author=li, score=55, views=5000)//AggregationResult(id=55f5a1d3d4bede9ac365b259, author=annT, score=55, views=50)}
}

相关文章:

MongoDB - 聚合阶段 $match、$sort、$limit

文章目录 1. $match 聚合阶段1. 构造测试数据2. $match 示例3. $match 示例 2. $sort 聚合阶段1. 排序一致性问题2. $sort 示例 3. $limit 聚合阶段 1. $match 聚合阶段 $match 接受一个指定查询条件的文档。 $match 阶段语法&#xff1a; { $match: { <query> } }$ma…...

ModuleNotFoundError: No module named ‘scrapy.utils.reqser‘

在scrapy中使用scrapy-rabbitmq-scheduler会出现报错 ModuleNotFoundError: No module named scrapy.utils.reqser原因是新的版本的scrapy已经摒弃了该方法,但是scrapy-rabbitmq-scheduler 没有及时的更新,所以此时有两种解决方法 方法一.将scrapy回退至旧版本,找到对应的旧版…...

vue3+ts+vite+electron+electron-packager打包成exe文件

目录 1、创建vite项目 2、添加需求文件 3、根据package.json文件安装依赖 4、打包 5、electron命令运行 6、electron-packager打包成exe文件 Build cross-platform desktop apps with JavaScript, HTML, and CSS | Electron 1、创建vite项目 npm create vitelatest 2、添…...

使用脚本搭建MySQL数据库基础环境

数据库的基本概念 数据&#xff08;Data&#xff09; 描述事物的符号记录 包括数字&#xff0c;文字&#xff0c;图形。图像&#xff0c;声音&#xff0c;档案记录等。 以记录形式按统一格式进行存储 表 将不同的记录组织在一起 用来储存具体数据 数据库 表的集合&#xff0c;是…...

Parameter index out of range (2 > number of parameters, which is 1【已解决】

文章目录 1、SysLogMapper.xml添加注释导致的2、解决方法3、总结 1、SysLogMapper.xml添加注释导致的 <!--定义一个查询方法&#xff0c;用于获取日志列表--><!--方法ID为getLogList&#xff0c;返回类型com.main.server.api.model.SysLogModel,参数类型为com.main.se…...

rk3588s 定制版 USB adb , USB2.0与USB3.0 区别,adb 由typeC 转换到USB3.0(第二部分)

硬件资源&#xff1a; rk3588s 核心板定制的地板 软件资源&#xff1a; 网盘上的 android12 源码 1 硬件上 客户只想使用 type c 接口中的 usb2.0 OTG 。在硬件上&#xff0c;甚至连 CC芯片都没有连接。 关于一些前置的知识。 1 USB2.0 与 USB3.0 的区别。 usb3.0 兼容2.0 …...

Cookie与Session 实现登录操作

Cookie Cookie 是网络编程中使用最广泛的一项技术&#xff0c;主要用于辨识用户身份。 客户端&#xff08;浏览器&#xff09;与网站服务端通讯的过程如下图所示&#xff1a; 从图中看&#xff0c;服务端既要返回 Cookie 给客户端&#xff0c;也要读取客户端提交的 Cookie。所…...

通过IEC104转MQTT网关轻松接入阿里云平台

随着智能电网和物联网技术的飞速发展&#xff0c;电力系统中的传统IEC 104协议设备正面临向现代化、智能化转型的迫切需求。阿里云作为全球领先的云计算服务提供商&#xff0c;其强大的物联网平台为IEC 104设备的接入与数据处理提供了强大的支持。本文将深入探讨钡铼网关在MQTT…...

lua 游戏架构 之 游戏 AI (五)ai_autofight_find_way

这段Lua脚本定义了一个名为 ai_autofight_find_way 的类&#xff0c;继承自 ai_base 类。 lua 游戏架构 之 游戏 AI &#xff08;一&#xff09;ai_base-CSDN博客文章浏览阅读238次。定义了一套接口和属性&#xff0c;可以基于这个基础类派生出具有特定行为的AI组件。例如&…...

vue3+openLayers点击标记事件

<template><!--地图--><div class"distributeMap" id"distributeMap"></div> </template> <script lang"ts" setup> import { onMounted, reactive } from "vue"; import { Feature, Map, View }…...

深入分析 Android ContentProvider (三)

文章目录 深入分析 Android ContentProvider (三)ContentProvider 的高级使用和性能优化1. 高级使用场景1.1. 数据分页加载示例&#xff1a;分页加载 1.2. 使用 Loader 实现异步加载示例&#xff1a;使用 CursorLoader 加载数据 1.3. ContentProvider 与权限管理示例&#xff1…...

养宠浮毛异味双困扰?性价比高的宠物空气净化器推荐

家里养了两只银渐层&#xff0c;谁懂啊&#xff01;一下班打开家门就看到家里飘满了猫浮毛雪&#xff0c;空气中还传来隐隐约约的异味。每天不是在吸毛的路上&#xff0c;就是在洗猫砂盆的路上&#xff0c;而且空气中的浮毛还很难清理干净&#xff0c;这是最让人头疼的问题。 …...

maven项目容器化运行之3-优雅的利用Jenkins和maven使用docker插件调用远程docker构建服务并在1Panel中运行

一.背景 在《maven项目容器化运行之1》中&#xff0c;我们开启了1Panel环境中docker构建服务给到了局域网。在《maven项目容器化运行之2》中&#xff0c;我们基本实现了maven工程创建、远程调用docker构建镜像、在1Panel选择镜像运行容器三大步骤。 但是&#xff0c;存在一个问…...

docker 打包orbbec

docker pull humble容器 sudo docker run -it osrf/ros:humble-desktop docker 启动容器 sudo docker run -u root --device/dev/bus/usb:/dev/bus/usb -it -v /home/wl:/share --name wl4 osrf/ros:humble-desktop /bin/bash新开一个终端 查看本地存在的容器&#xff1a;…...

无涯·问知财报解读,辅助更加明智的决策

财报解读就像是给公司做一次全面的体检&#xff0c;是理解公司内部运作机制和市场表现的一把钥匙&#xff0c;能够有效帮助投资者、分析师、管理层以及所有市场参与者判断一家公司的健康程度和发展潜力。 星环科技无涯问知的财经库内置了企业年报及财经类信息&#xff0c;并对…...

【Apache Doris】数据副本问题排查指南

【Apache Doris】数据副本问题排查指南 一、问题现象二、问题定位三、问题处理 本文主要分享Doris中数据副本异常的问题现象、问题定位以及如何处理此类问题。 一、问题现象 问题日志 查询报错 Failed to initialize storage reader, tablet{tablet_id}.xxx.xxx问题说明 查…...

【HarmonyOS】关于鸿蒙消息推送的心得体会(二)

【HarmonyOS】关于鸿蒙消息推送的心得体会&#xff08;二&#xff09; 前言 推送功能的开发与传统功能开发还是有很大区别。首先最大的区别点就在于需要多部门之间的协同&#xff0c;作为鸿蒙客户端开发&#xff0c;你需要和产品&#xff0c;运营&#xff0c;以及后台开发一起…...

零基础入门:创建一个简单的Python爬虫管理系统

摘要&#xff1a; 本文将手把手教你&#xff0c;从零开始构建一个简易的Python爬虫管理系统&#xff0c;无需编程基础&#xff0c;轻松掌握数据抓取技巧。通过实战演练&#xff0c;你将学会设置项目、编写基本爬虫代码、管理爬取任务与数据&#xff0c;为个人研究或企业需求奠…...

【Node.js基础04】node.js模块化

一&#xff1a;什么是模块化 在Node.js中&#xff0c;每个文件都可视为一个独立的模块。模块化提高了代码的复用性&#xff0c;按需加载&#xff0c;具有独立的作用域 二&#xff1a;如何实现多个文件间导入和导出 1 CommonJS标准&#xff08;默认&#xff09;-导入和导出 …...

数据库——单表查询

一、建立数据库mydb8_worker mysql> use mydb8_worker; 二、建立表 1.创建表 mysql> create table t_worker(department_id int(11) not null comment 部门号,-> worder_id int(11) primary key not null comment 职工号,-> worker_date date not null comment…...

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…...

手游刚开服就被攻击怎么办?如何防御DDoS?

开服初期是手游最脆弱的阶段&#xff0c;极易成为DDoS攻击的目标。一旦遭遇攻击&#xff0c;可能导致服务器瘫痪、玩家流失&#xff0c;甚至造成巨大经济损失。本文为开发者提供一套简洁有效的应急与防御方案&#xff0c;帮助快速应对并构建长期防护体系。 一、遭遇攻击的紧急应…...

深入剖析AI大模型:大模型时代的 Prompt 工程全解析

今天聊的内容&#xff0c;我认为是AI开发里面非常重要的内容。它在AI开发里无处不在&#xff0c;当你对 AI 助手说 "用李白的风格写一首关于人工智能的诗"&#xff0c;或者让翻译模型 "将这段合同翻译成商务日语" 时&#xff0c;输入的这句话就是 Prompt。…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

【生成模型】视频生成论文调研

工作清单 上游应用方向&#xff1a;控制、速度、时长、高动态、多主体驱动 类型工作基础模型WAN / WAN-VACE / HunyuanVideo控制条件轨迹控制ATI~镜头控制ReCamMaster~多主体驱动Phantom~音频驱动Let Them Talk: Audio-Driven Multi-Person Conversational Video Generation速…...

Go 语言并发编程基础:无缓冲与有缓冲通道

在上一章节中&#xff0c;我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道&#xff0c;它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好&#xff0…...

C++:多态机制详解

目录 一. 多态的概念 1.静态多态&#xff08;编译时多态&#xff09; 二.动态多态的定义及实现 1.多态的构成条件 2.虚函数 3.虚函数的重写/覆盖 4.虚函数重写的一些其他问题 1&#xff09;.协变 2&#xff09;.析构函数的重写 5.override 和 final关键字 1&#…...

LRU 缓存机制详解与实现(Java版) + 力扣解决

&#x1f4cc; LRU 缓存机制详解与实现&#xff08;Java版&#xff09; 一、&#x1f4d6; 问题背景 在日常开发中&#xff0c;我们经常会使用 缓存&#xff08;Cache&#xff09; 来提升性能。但由于内存有限&#xff0c;缓存不可能无限增长&#xff0c;于是需要策略决定&am…...

C# 表达式和运算符(求值顺序)

求值顺序 表达式可以由许多嵌套的子表达式构成。子表达式的求值顺序可以使表达式的最终值发生 变化。 例如&#xff0c;已知表达式3*52&#xff0c;依照子表达式的求值顺序&#xff0c;有两种可能的结果&#xff0c;如图9-3所示。 如果乘法先执行&#xff0c;结果是17。如果5…...

小木的算法日记-多叉树的递归/层序遍历

&#x1f332; 从二叉树到森林&#xff1a;一文彻底搞懂多叉树遍历的艺术 &#x1f680; 引言 你好&#xff0c;未来的算法大神&#xff01; 在数据结构的世界里&#xff0c;“树”无疑是最核心、最迷人的概念之一。我们中的大多数人都是从 二叉树 开始入门的&#xff0c;它…...