依赖注入的方式测试ArrayList和LinkedList的效率(对依赖注入的再次理解)
9/20 号再进行学习
在C++中,main函数尽可能的简单,只要调用子函数的一句话就实现了功能。
java开发中,controller就相同于是main函数,其他类的方法不在本类中时候,
1.可以用new对象的方法,调用那个方法
2.用依赖注入的方式,将其他类的对象注入到main函数的类中,然后就可以调用这个方法了
先贴结果
项目结构
8/22添加
链表的优势是插入和删除,劣势是查找
数组的优势的查找,劣势的插入删除
在9999999*2个整数中进行查找的效率对比
ArrayListTest used a total time is 1 millisecondes !(有时测出来是0)
linkedList used a total time is 15 millisecondes !
从序列中删除一个数字的效率对比
ArrayListTest used a total time is 15 millisecondes !
linkedList used a total time is 0 millisecondes !
使用配置文件的方式
package com.baobaotao1;
import java.util.Date;
import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
//往这个类里面注入了2个对象
private List<Integer> list01;
private List<Integer> list02; public void setList01(List<Integer> list01) {
this.list01 = list01;
}
public void setList02(List<Integer> list02) {
this.list02 = list02;
}
public void vs(List<Integer> list){
long t1 = new Date().getTime();
list.add(1); list.add(2);list.add(3);
for(int i=1;i<99999;i++){
list.add(1,i);
}
long t2 = new Date().getTime();
System.out.println("时间是"+(t2-t1));
}
public void test(){
this.vs(list01);
this.vs(list02);
}
public static void main(String[] args) {
//new CarTest().test();
//上面的这种测试的话,加载不了配置文件,空指针异常,在tomcat服务器中运行的web项目中会自动加载
// web项目中,要么自动去加载xml文件,要么根据web.xml文件的配置去加载application.xml文件
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
Test c = (Test) ctx.getBean("carTest");
c.test();
} }
在WEB-INF 这个目录下,这个文件夹下的web.xml文件会被自动加载并读取,
回答:tomcat会自动加载web.xml文件,部署在tomcat的WEB-INF文件夹下的.xm文件,
只有web.xml文件会被自动加载,如在这个目录下还有其他的xml文件,需要在web.xml文件里配置
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="list1" class="java.util.ArrayList" ></bean>
<bean id="list2" class="java.util.LinkedList"></bean> <bean id="carTest" class="com.baobaotao1.CarTest">
<property name="list01" ref="list1"/>
<property name="list02" ref="list2"/>
</bean> </beans>
注解进行测试
package com.baobaotao1; import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
/*下面的这2个注解用于 new ClassPathXmlApplicationContext*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
@Resource //按照list01这个名称进行注入
private List<Integer> list01;
@Resource
private List<Integer> list02;
//面向接口编程List<Integer>完全降低了耦合,完全没有实现类的影子
public void vs(List<Integer> list){
long t1 = new Date().getTime();
list.add(1); list.add(2);list.add(3);
for(int i=1;i<9999;i++){
list.add(1,i);
}
long t2 = new Date().getTime();
System.out.println("时间是"+(t2-t1));
} public void test(){
this.vs(list01);
this.vs(list02);
} public static void main(String[] args) {
// new CarTest().test(); 用这种方式去加载xml配置文件的话,也会空指针
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
Test c = (Test) ctx.getBean("carTest");
c.test();
} }
配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!--这句话必须有,以及上面的那些必须有(这是进行注解开发所必须要的)-->
<context:component-scan base-package="com.baobaotao1"></context:component-scan> <bean id="list01" class="java.util.ArrayList" ></bean>
<bean id="list02" class="java.util.LinkedList"></bean>
<bean id="carTest" class="com.baobaotao1.Test"></bean>
//传入这个包名+类名 一般用到的都是反射机制
</beans>
依赖注入的方式测试ArrayList和LinkedList的效率(对依赖注入的再次理解)的更多相关文章
- 老徐和阿珍的故事:ArrayList和LinkedList的效率到底哪个高?
人物背景: 老徐,男,本名徐福贵,从事Java相关研发工作多年,职场老油条,摸鱼小能手,虽然岁数不大但长的比较着急,人称老徐.据说之前炒某币败光了所有家产,甚至现在还有欠债. 阿珍,女,本名陈家珍,刚 ...
- Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor
Webservice WCF WebApi 注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...
- Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件
1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...
- java的list几种实现方式的效率(ArrayList、LinkedList、Vector、Stack),以及 java时间戳的三种获取方式比较
一.list简介 List列表类,顺序存储任何对象(顺序不变),可重复. List是继承于Collection的接口,不能实例化.实例化可以用: ArrayList(实现动态数组),查询快(随意访问或 ...
- MVVM模式用依赖注入的方式配置ViewModel并注册消息
最初的想法 这次主要讨论下给View指定ViewModel的事情.一般来说给View指定ViewModel常用的方式有两种,一种是在View的后台代码中写DataContext = new ViewM ...
- 极简SpringBoot指南-Chapter02-Spring依赖注入的方式
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
- 基准测试了 ArrayList 和 LinkedList ,发现我们一直用 ArrayList 也是没什么问题的
ArrayList 应该是 Java 中最常用的集合类型了,以至于我们说到集合就会自然而然的想到 ArrayList.很多同学都没有用过除了 ArrayList 之外的其他集合,甚至于都已经忘了除了 ...
- ArrayList、LinkedList、Vector、Array和HashMap、HashTable
就 ArrayList 与 Vector 主要从二方面来说. 一.同步性:Vector 是线程安全的,也就是说是同步的,而ArrayList 是线程序不安全的,不是同步的 二.数据增长:当需要增长时, ...
随机推荐
- [HDU1890]RoboticSort
Problem 每次找到最小值,然后把它和它前面的数翻转,然后找第二小数······ 然后输出这些数的下标. Solution 用splay维护,每次找到最小值,然后翻转前面区间. Notice 细节 ...
- OO第四次课程总结分析
OO第四次课程总结分析 测试与正确性论证的效果差异及优缺点 测试,即使用测试样例来验证我们的程序是否能完成相应功能的过程.测试数据的产生基于前置条件和后置条件,通过执行测试数据检查方法输出是否满足需求 ...
- python logging模块,升级print调试到logging。
简介: 我们在写python程序的时候,很多时候都有bug,都是自己写的,自己造的孽,又的时候报错又是一堆,不知道是那部分出错了. 我这初学者水平,就是打print,看哪部分执行了,哪部分没执行,由此 ...
- 读书笔记 C# 接口中的索引器之浅析
在C#中,可以在类.结构或接口中用this关键字声明索引器,在索引器内部用get或set访问器访问类中集合的某项值.因此可以将索引器看作是类的属性一样去定义.索引器常用定义格式如下: public i ...
- :状态模式:GumballMachine
#ifndef __STATE_H__ #define __STATE_H__ #include <iostream> #include<stdlib.h> using nam ...
- 在嵌入式设计中使用MicroBlaze(Vivado版本)(转)
原文Xilinx官方文档<ug898-vivado-embedded-design>第三章 一.MicroBlaze处理器设计介绍(略) 二.创建带有MicroBlaze处理器的IP设计 ...
- turtle
画一组同切圆 输入 import turtle turtle.color('red') turtle.circle(30) turtle.circle(60) turtle.circle(90) tu ...
- 玩转X-CTR100 l STM32F4 l MPU6050加速度陀螺仪传感器
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 板载加速度 ...
- SharePoint Framework 企业向导(九)
博客地址:http://blog.csdn.net/FoxDave 管理SPFx解决方案的容量 所有部署到租户的SPFx解决方案必须被租户管理员审批通过.这是通过上传SPFx包(.sppkg)到A ...
- Linux文件系统命令 split
命令:split 功能:将文件按照一定的规则进行切割 用法:-l 表示按照行数进行切割. -b 表示按照字节进行切割,切割后的文件名为自己定义的文件名+aa,ab,ac类似的后缀. eg: 按照行数进 ...