Java8特征性使用

最近在工作中加大了lambda表达式的使用,特此记录一些,会不断更新。
list转成map,收集实体本身

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity()));
}

list转成map,处理key重复的情况
1)、toMap有个重载方法,可以传入一个合并的函数来解决key冲突问题
下面只是简单的使用后者覆盖前者来解决key重复问题

public Map<String, Account> getNameAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2));
}

2)、指定具体收集的map
toMap还有另一个重载方法,可以指定一个Map的具体实现,来收集数据

public Map<String, Account> getNameAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
}

list转map,分组

//List 以ID分组 Map<Integer,List<Apple>>
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));

list转成map,其中model本身做key,list中重复个数为value

Map<String, Long> resultMap = productList.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

过滤map中value的值大于1的键值对

Map<String, Long> tempMap = resultMap.entrySet().stream()
									 .filter(r -> r.getValue() > 1)
		.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
以下是Java使用OpenCV进行特征点匹配的代码示例: ```java import org.opencv.core.*; import org.opencv.features2d.*; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; public class FeatureMatching { public static void main(String[] args) { // 加载OpenCV库 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 读取图片 Mat img1 = Imgcodecs.imread("img1.jpg"); Mat img2 = Imgcodecs.imread("img2.jpg"); // 初始化ORB特征检测器和描述子生成器 ORB orb = ORB.create(); // 检测特征点和描述子 MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); Mat descriptors1 = new Mat(); Mat descriptors2 = new Mat(); orb.detectAndCompute(img1, new Mat(), keypoints1, descriptors1); orb.detectAndCompute(img2, new Mat(), keypoints2, descriptors2); // 初始化描述子匹配器 DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING); // 匹配描述子 MatOfDMatch matches = new MatOfDMatch(); matcher.match(descriptors1, descriptors2, matches); // 绘制匹配结果 Mat imgMatches = new Mat(); Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches, imgMatches); HighGui.imshow("匹配结果", imgMatches); HighGui.waitKey(); } } ``` 该示例使用ORB特征检测器和描述子生成器来检测两张图片的特征点和描述子,然后使用描述子匹配器进行描述子匹配,并使用OpenCV的绘图函数绘制匹配结果。 需要注意的是,该示例只是演示了如何使用ORB进行特征点匹配,并不能保证匹配结果的准确性。在实际应用,需要根据具体的需求选择适合的特征检测器和描述子生成器,并进行参数调整和优化,以得到更好的匹配结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mandy_i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值