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

持有对象-泛型和类型安全的容器

我们需要管理一批对象序列,但是又对实际运行的时候的对象类型和对象序列长度不确定的时候,用简单的对象引用无法满足,java有ArrayList,Map,Set等这些容器类提供,这些都实现了Collections接口,所以都属于Collections类。

package com.example.demo.demos.web;public class apple extends fruit{
}
package com.example.demo.demos.web;public class orange extends fruit{
}
package com.example.demo.demos.web;public class fruit {
}
package com.example.demo;import com.example.demo.demos.web.apple;
import com.example.demo.demos.web.fruit;
import com.example.demo.demos.web.orange;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class TestClass {public static void main(String[] args)  {List<fruit> fruits = new ArrayList<fruit>();fruits = Arrays.asList(new apple(),new orange());for (fruit fruit : fruits) {System.out.println(fruit);}}}
com.example.demo.demos.web.apple@452b3a41
com.example.demo.demos.web.orange@4a574795

输出了具体类名和对应的散列码(通过hashCode生成再转为十六进制)

package com.example.demo;import com.example.demo.demos.web.apple;
import com.example.demo.demos.web.fruit;
import com.example.demo.demos.web.orange;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class TestClass {public static void main(String[] args)  {List<fruit> fruits = new ArrayList<fruit>();fruits = Arrays.asList(new apple(),new orange());for (fruit fruit : fruits) {System.out.println(fruit);System.out.println(Integer.toHexString(fruit.hashCode()));}}}
com.example.demo.demos.web.apple@452b3a41
452b3a41
com.example.demo.demos.web.orange@4a574795
4a574795
http://www.lryc.cn/news/585765.html

相关文章:

  • 线程通信V
  • 【Linux】系统引导修复
  • InnoDB 存储引擎的 架构
  • 渗透测试之木马后门实验
  • 世界现存燃油汽车品牌起源国别梳理
  • k8s新增jupyter服务
  • 中国国际会议会展中心模块化解决方案的技术经济分析报告
  • 【机器学习应用】基于集成学习的电力负荷预测系统实战案例
  • Linux设备树(dts/dtsi/dtb、设备树概念,设备树解析,驱动匹配)
  • kubernetes单机部署踩坑笔记
  • 【linux网络】深入理解 TCP/UDP:从基础端口号到可靠传输机制全解析
  • 【理念●体系】Windows AI 开发环境搭建实录:六层架构的逐步实现与路径治理指南
  • ATAM与效用树:架构评估的核心方法论
  • 鸿蒙 Secure Boot 全流程解析:从 BootROM 到内核签名验证的实战指南
  • 使用 lstrip() 和 rstrip() 方法
  • OpenAI 将推 AI Agent 浏览器:挑战 Chrome,重塑上网方式
  • C语言文件读写操作详解:fgetc与feof函数的应用
  • 上位机知识篇---Git符号链接
  • vue3 el-input 通过数组 获取显示
  • 【构建Tomcat版本检查工具:自动检测并提醒版本更新】
  • [面试] 手写题-插入排序
  • Redis命令参考手册
  • (C++)set集合相关知识(STL标准库)(C++教程)(set集合基础教程)
  • 供应链管理-计划:产能策略
  • 匿名函数作递归函数引用
  • 声明式 vs 编程式:Spring事务管理全对比
  • Prometheus+Grafana部署及企业微信邮件/群消息告警通知配置
  • linux系统-----Redis数据库基础
  • 迭代器(c++)、智能指针
  • LDO选型