This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:java-best-practices/replace-vector-with-list
Language: Java
Severity: Warning
Category: Best Practices
Description
Replace your Vector class usage with the newer java.util.ArrayList, unless you need expensive thread-safe operations.
Vector uses unnecessary synchronization, which can slow down single-threaded applications whereas ArrayList can perform better in such cases. In addition, it offers modern features which make the transition easy while retaining flexibility for thread safety when needed.
Non-Compliant Code Examples
publicclassFoo{voidbar(){Vectorvector1=newVector();// consider using java.util.List insteadVector<Integer>vector2=newVector<>();}}
Compliant Code Examples
packagecom.dd.logs.rum.sessionreducer;import staticio.vavr.API.Vector;importcom.dd.logs.launcher.Features;importcom.dd.logs.launcher.ModuleLauncher;importcom.dd.logs.rule_engine.reducer.main.ReducerWorkloadBundle;importcom.dd.logs.usage.UsageComponent;importcom.dd.logs.usage.UsageTrackerClientBundle;importcom.dd.logs.workload.assigner.AssignerClientBundle;importio.vavr.collection.Vector;importjava.util.List;publicclassMain{publicstaticvoidmain(String[]args){newModuleLauncher(List.of(newAssignerClientBundle(),newUsageTrackerClientBundle(UsageComponent.SESSIONIZATION),newReducerWorkloadBundle(newRumReducerWorkloadBundleParams())),// disconnected from mongoFeatures.builder().withKafka().build()).launchOrExit();Vector<String>x=returnSomeVavrVector();x.stdout();}privatestaticVector<String>returnSomeVavrVector(){returnVector();}}