Java比较效率对象

时间:2018-06-16 15:05:25

标签: java

在Java里面比较String对象的Hashcode和直接比较String对象谁更高效,为什么呢

!SESSION 2018-06-13 21:59:34.613 -----------------------------------------------
eclipse.buildId=unknown
java.version=10.0.1
java.vendor=Oracle Corporation
BootLoader constants: OS=macosx, ARCH=x86_64, WS=cocoa, NL=en_IN
Framework arguments:  /studio-rcp/resources/icons/linux/studio.xpm -keyring /Users/vamsikrishna/.eclipse_keyring
Command-line arguments:  -os macosx -ws cocoa -arch x86_64 /studio-rcp/resources/icons/linux/studio.xpm -keyring /Users/vamsikrishna/.eclipse_keyring

!ENTRY org.eclipse.osgi 4 0 2018-06-13 21:59:43.056
!MESSAGE Application error
!STACK 1
org.eclipse.e4.core.di.InjectionException: java.lang.NoClassDefFoundError: javax/annotation/PostConstruct
        at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:386)
        at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:294)
        at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:162)
        at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createDefaultHeadlessContext(E4Application.java:490)
        at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createDefaultContext(E4Application.java:504)
        at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createE4Workbench(E4Application.java:203)
        at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:632)
        at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
        at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:610)
        at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
        at org.apache.directory.studio.Application.start(Application.java:51)
        at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:673)
        at org.eclipse.equinox.launcher.Main.basicRun(Main.java:610)
        at org.eclipse.equinox.launcher.Main.run(Main.java:1519)
Caused by: java.lang.NoClassDefFoundError: javax/annotation/PostConstruct
        at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:151)
        at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:375)
        ... 22 more
Caused by: java.lang.ClassNotFoundException: javax.annotation.PostConstruct cannot be found by org.eclipse.e4.core.di_1.6.1.v20160712-0927
        at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:410)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:372)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:364)
        at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:161)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)

2 个答案:

答案 0 :(得分:0)

hashCode是捷径,不是对平等的真正考验。两个相等的对象应该具有相同的哈希码,但相反的情况不一定如此。例如,字符串"BBAa""AaBB"具有相同的哈希码(至少使用OpenJDK 8,它们都是2031744),但绝对是相等。

答案 1 :(得分:0)

String上的hashcode是第一次计算可以很长,但是在第二次调用时它很短但是相同和哈希码根本就没有意思相同!

平等和哈希码相等之间的唯一关系就是这些:

不同的hashcode =>不平等

equality =>相同的哈希码

那些完全错误:

相同的hashCode =>平等

不平等=>不同的哈希码