当前位置: 首页 > 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.dtcms.com/a/274997.html

相关文章:

  • 深度学习中的归一化技术详解:BN、LN、IN、GN
  • Kubernetes 高级调度特性
  • C语言:位运算
  • Redis 哨兵机制
  • 多代理系统(multi-agent)框架深度解析:架构、特性与未来
  • 无代码自动化测试工具
  • STM32G473串口通信-USART/UART配置和清除串口寄存器状态的注意事项
  • 隆重介绍 Xget for Chrome:您的终极下载加速器
  • 开源界迎来重磅核弹!月之暗面开源了自家最新模型 K2
  • 从延迟测试误区谈起:SmartPlayer为何更注重真实可控的低延迟?
  • gitee 代码仓库面试实际操作题
  • Cadence Virtuoso中如何集成Calibre
  • Java进阶---并发编程
  • 打造未来制造核心力:虚拟调试的价值与落地思路
  • YOLO-DETR如何提升小目标的检测效果
  • 【数据结构与算法】数据结构初阶:详解顺序表和链表(三)——单链表(上)
  • OpenCV实现感知哈希(Perceptual Hash)算法的类cv::img_hash::PHash
  • 商城网站建设实务
  • Ragflow-plus本地部署和智能问答及报告编写应用测试
  • 标准化模型格式ONNX介绍:打通AI模型从训练到部署的环节
  • C语言易错点(二)
  • C++包管理工具:conan2常用命令详解
  • JVM-----【并发可达性分析】
  • Android 12系统源码_分屏模式(一)从最近任务触发分屏模式
  • 微信小程序核心知识点速览
  • OpenCV图像基本操作:读取、显示与保存
  • OpenLLMetry 助力 LLM 应用实现可观测性
  • 1-Git安装配置与远程仓库使用
  • uniapp---入门、基本配置了解
  • springboot-2.3.3.RELEASE升级2.7.16,swagger2.9.2升级3.0.0过程