依赖注入的方式测试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 是线程序不安全的,不是同步的 二.数据增长:当需要增长时, ...
随机推荐
- selenium(七)webdriverwait,高级等待,替代sleep
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from seleni ...
- tomcat 线程数与 mysql 连接数综合调优
目前线上系统包含 数据收集+数据分析+中心服务,三个均为 tomcat,共用一个mysql服务. 由于tomcat最大线程数200 *3 =600,最大并发时,会有600个jdbc连接.当然这是极端情 ...
- javascript进阶笔记(3)
本篇文章我们来学习和讨论一下js中的闭包.闭包是纯函数式编程的一个特性,因为它们能够大大简化复杂的操作.在js中,闭包的重要性不言而喻! 简单的说,闭包(closure)是 一个函数在创建时 允许 该 ...
- java关于集合的遍历与增强for循环(foreach)的使用
java集合类的使用可以说是无处不在,总的我们可以将之分为三大块,分别是从Collection接口延伸出的List.Set和以键值对形式作存储的Map类型集合. 许多情况需要我们遍历出集合 ...
- 二十. Python基础(20)--面向对象的基础
二十. Python基础(20)--面向对象的基础 1 ● 类/对象/实例化 类:具有相同属性.和方法的一类人/事/物 对象(实例): 具体的某一个人/事/物 实例化: 用类创建对象的过程→类名(参数 ...
- mybatis column 和property
mybatis map文件中 resultMap中column和sql查询结果对应, property和实体private对应 <resultMap id="VideoYcAppRes ...
- <Spark><Introduction to Spark>
What Is Apache Spark? 速度方面:Spark扩展了MapReduce模型,可以更高效地提供多种类型的计算,包括交互式查询和流处理.Spark为speed所提供的最大能力就是内存计算 ...
- L265 - 5 questions to ask yourself before you ask for a raise or promotion
You’ve been in your role for a while now, giving 110% to every assignment your manager hands out. Yo ...
- day 96 关于分页的使用
分页的学习主要四步骤 from django.db import models # Create your models here. class Book(models.Model): title = ...
- day 57 data 插件 表的增删改查
一 data的含义 在匹配的元素集合中的所有元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值. 1 .data(key, value): 描述:在匹配 ...