为什么HashMap 默认loadFactor是0.75

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put).

大概意思是:一般来说,默认的负载因子(0.75)在时间和空间成本之间提供了很好的权衡。更高的值减少了空间开销,但增加了查找成本(反映在HashMap类的大多数操作中,包括get和put)。

试想一下,如果我们把负载因子设置成1,容量使用默认初始值16,那么表示一个HashMap需要在”满了”之后才会进行扩容。

那么在HashMap中,最好的情况是这16个元素通过hash算法之后分别落到了16个不同的桶中,否则就必然发生哈希碰撞。而且随着元素越多,哈希碰撞的概率越大,查找速度也会越低。

Others:
HashMap默认容量为16
根据HashMap的扩容机制,他会保证capacity的值永远都是2的幂。

HashMap构造参数

SpringBoot 中使用Aop

前言

在Spring中,不管是学习还是面试,有两个点是永远绕不过去的,一个是IOC,另一个就是Aop,Spring中使用Aop是非常简单的,可以通过xml和注解两种方式写出aop的代码,那么在SpringBoot中有什么变化吗?

在SpringBoot中使用aop实际上没有太多的变化,关于SpringBoot实际上就是对Spring和SpringMVC的进一步封装,因此在 SpringBoot 中同样支持Spring框架中AOP切面编程,不过在SpringBoot中为了快速开发仅仅提供了注解方式的切面编程。

SpringBoot 注解式 aop 的使用

本文所有的内容,将会在下面的demo上进行操作。 请去Git仓库下载: https://gitee.com/bingqilinpeishenme/Lu-JavaNodes

1.引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.aop相关注解讲解

/**
    @Aspect 用来类上,代表这个类是一个切面
    @Before 用在方法上代表这个方法是一个前置通知方法 
    @After 用在方法上代表这个方法是一个后置通知方法 
    @Around 用在方法上代表这个方法是一个环绕的方法
    @Order(数字)用在类上,数字越小进入越早
**/
/**
    环绕,前置,后置全部存在
    先进入环绕,在进入前置,离开前置,离开环绕,进入后置,离开后置
**/

前置切面

/**
* @Aspect  用来类上,代表这个类是一个切面
* @Component 用来告诉SpringBoot 给当前类创建对象 等价于@Service
*/
@Aspect
@Component
public class MyAspect {
    //@Before 用在方法上代表这个方法是一个前置通知方法  注解的参数是切点表达式
    @Before("execution(* com.lu.service.*.*(..))")
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知");
//JoinPoint 通过该对象可以获取到被切入的对象的信息
        joinPoint.getTarget();//目标对象
        joinPoint.getSignature();//方法签名
        joinPoint.getArgs();//方法参数
    }
}

后置切面

@Aspect
@Component
public class MyAspect {
    @After("execution(* com.lu.service.*.*(..))")
    public void before(JoinPoint joinPoint){
        System.out.println("后置通知");
        joinPoint.getTarget();//目标对象
        joinPoint.getSignature();//方法签名
        joinPoint.getArgs();//方法参数
    }
}

注意: 前置通知和后置通知都没有返回值,方法参数都为joinpoint

环绕切面

@Aspect
@Component
public class MyAspect {
    @Around("execution(* com.lu.service.*.*(..))")
    public Object before(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("进入环绕通知");
        proceedingJoinPoint.getTarget();//目标对象
        proceedingJoinPoint.getSignature();//方法签名
        proceedingJoinPoint.getArgs();//方法参数
        Object proceed = proceedingJoinPoint.proceed();//放行执行目标方法  这一步必须存在
        System.out.println("目标方法执行之后回到环绕通知");
        return proceed;//返回目标方法返回值
    }
}

注意: 环绕通知存在返回值,参数为ProceedingJoinPoint

  • 如果不执行放行,不会执行目标方法
  • 一旦放行必须将目标方法的返回值返回,否则调用者无法接受返回数据

总结

以上就是SpringBoot中aop的简单使用, 今天实际上只是简单的测试了aop的Api,关于aop的应用,例如:通过Aop记录日志信息,通过Aop操作redis缓存等实战应用,会在后续文章中写出 。

From: http://www.cnblogs.com/bingyang-py/p/12354037.html

visitor


layout: pattern
title: Visitor
folder: visitor
permalink: /patterns/visitor/
categories: Behavioral
tags:

– Gang of Four

Intent

Represent an operation to be performed on the elements of an object
structure. Visitor lets you define a new operation without changing the classes
of the elements on which it operates.

Class diagram

alt text

Applicability

Use the Visitor pattern when

  • An object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes
  • Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid “polluting” their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them
  • The classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it’s probably better to define the operations in those classes

Real world examples

Credits

value-object


layout: pattern
title: Value Object
folder: value-object
permalink: /patterns/value-object/
categories: Creational
tags:

– Instantiation

Intent

Provide objects which follow value semantics rather than reference semantics.
This means value objects’ equality are not based on identity. Two value objects are
equal when they have the same value, not necessarily being the same object.

Class diagram

alt text

Applicability

Use the Value Object when

  • You need to measure the objects’ equality based on the objects’ value

Real world examples

Credits

update-method


layout: pattern
title: Update Method
folder: update-method
permalink: /patterns/update-method/
categories: Behavioral
tags:

– Game programming

Intent

Update method pattern simulates a collection of independent objects by telling each to process one frame of behavior at a time.

Explanation

The game world maintains a collection of objects. Each object implements an update method that simulates one frame of the object’s behavior. Each frame, the game updates every object in the collection.

To learn more about how the game loop runs and when the update methods are invoked, please refer to Game Loop Pattern.

Class diagram

alt text

Applicability

If the Game Loop pattern is the best thing since sliced bread, then the Update Method pattern is its butter. A wide swath of games featuring live entities that the player interacts with use this pattern in some form or other. If the game has space marines, dragons, Martians, ghosts, or athletes, there’s a good chance it uses this pattern.

However, if the game is more abstract and the moving pieces are less like living actors and more like pieces on a chessboard, this pattern is often a poor fit. In a game like chess, you don’t need to simulate all of the pieces concurrently, and you probably don’t need to tell the pawns to update themselves every frame.

Update methods work well when:

  • Your game has a number of objects or systems that need to run simultaneously.
  • Each object’s behavior is mostly independent of the others.
  • The objects need to be simulated over time.

Credits

unit-of-work


layout: pattern
title: Unit Of Work
folder: unit-of-work
permalink: /patterns/unit-of-work/

categories: Architectural
tags:

– Data access

Intent

When a business transaction is completed, all the these updates are sent as one
big unit of work to be persisted in a database in one go so as to minimize database trips.

Class diagram

alt text

Applicability

Use the Unit Of Work pattern when

  • To optimize the time taken for database transactions.
  • To send changes to database as a unit of work which ensures atomicity of the transaction.
  • To reduce number of database calls.

Credits

typeobjectpattern


layout: pattern
title: Type-Object
folder: typeobjectpattern
permalink: /patterns/typeobjectpattern/
categories: Behavioral
tags:

  • Game programming

– Extensibility

Intent

As explained in the book Game Programming Patterns by Robert Nystrom, type object pattern helps in

Allowing flexible creation of new “classes” by creating a single class, each instance of which represents a different type of object

Explanation

Say, we are working on a game which has a hero and many monsters which are going to attack the hero. These monsters have certain attributes like attack, points etc. and come in different ‘breeds’ like zombie or ogres. The obvious answer is to have a base Monster class which has some fields and methods, which may be overriden by subclasses like the Zombie or Ogre class. But as we continue to build the game, there may be more and more breeds of monsters added and certain attributes may need to be changed in the existing monsters too. The OOP solution of inheriting from the base class would not be an efficient method in this case.
Using the type-object pattern, instead of creating many classes inheriting from a base class, we have 1 class with a field which represents the ‘type’ of object. This makes the code cleaner and object instantiation also becomes as easy as parsing a json file with the object properties.

Class diagram

alt text

Applicability

This pattern can be used when:

  • We don’t know what types we will need up front.
  • We want to be able to modify or add new types without having to recompile or change code.
  • Only difference between the different ‘types’ of objects is the data, not the behaviour.

Credits

twin


layout: pattern
title: Twin
folder: twin
permalink: /patterns/twin/
categories: Structural
tags:

– Extensibility

Intent

Twin pattern is a design pattern which provides a standard solution to simulate multiple
inheritance in java

Class diagram

alt text

Applicability

Use the Twin idiom when

  • To simulate multiple inheritance in a language that does not support this feature.
  • To avoid certain problems of multiple inheritance such as name clashes.

Credits

trampoline


layout: pattern
title: Trampoline
folder: trampoline
permalink: /patterns/trampoline/
categories: Behavioral
tags:

– Performance

Intent

Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack
and to interleave the execution of functions without hard coding them together
It is possible by representing a computation in one of 2 states : done | more
(completed with result, or a reference to the reminder of the computation,
something like the way a java.util.Supplier does).

Explanation

Trampoline pattern allows to define recursive algorithms by iterative loop.

Class diagram

alt text

Applicability

Use the Trampoline pattern when

  • For implementing tail recursive function. This pattern allows to switch on a stackless operation.
  • For interleaving the execution of two or more functions on the same thread.

Known uses(real world examples)

  • Trampoline refers to using reflection to avoid using inner classes, for example in event listeners.
    The time overhead of a reflection call is traded for the space overhead of an inner class.
    Trampolines in Java usually involve the creation of a GenericListener to pass events to an outer class.

Tutorials

Credits

粤ICP备15017284号