org.springframework.util.CollectionUtils
集合的判斷
boolean hasUniqueObject(Collection collection)
從原始碼註解來看,是用來判斷List/Set中的每個元素是否唯一,即List/Set 中不存在重複元素。但這裡要告訴大家千萬不要用這個方法,因為這個方法有bug,為什麼呢?下面是Spring-core-5.2.13.RELEASE.jar中的源碼,且看12行,細心的人會發現兩個物件之間比較是否相等用的是!=。還記得「==」和「equals」的差別嗎? 「==」運算子特別用來比較兩個變數的值是否相等,equals()方法是用來比較兩個獨立物件的內容是否相同。所以這裡如果集合中的元素是數值,可以用「==」比較,如果是普通的引用對象,就不會得到正確的結果了。
public static boolean hasUniqueObject(Collection<?> collection) { if (isEmpty(collection)) { return false; } boolean hasCandidate = false; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; }
boolean containsInstance(Collection collection, Object element)
從原始碼的註解上看,是用來判斷集合中是否包含某個物件。這個方法也不建議使用,因為與上一個方法有相同的問題,而且看原始碼的第4行,依然用的是「==」。
public static boolean containsInstance(@Nullable Collection<?> collection, Object element) { if (collection != null) { for (Object candidate : collection) { if (candidate == element) { return true; } } } return false; }
boolean isEmpty(Collection collection)
這個方法已驗證過可以放心用,用於判斷List/Set 是否為空;
@Test public void test1(){ Collection<String> list=new ArrayList<>(); boolean empty = CollectionUtils.isEmpty(list); Assert.isTrue(empty, "集合list不为空"); System.out.println("集合list增加一元素"); list.add("happy"); boolean empty2 = CollectionUtils.isEmpty(list); Assert.isTrue(empty2, "集合list不为空"); }
boolean isEmpty(Map map )
用來判斷Map 是否為空。
@Test public void test2(){ Map<String,String> map = new HashMap<>(); boolean empty = CollectionUtils.isEmpty(map); Assert.isTrue(empty, "map不为空"); System.out.println("map中增加元素"); map.put("name", "jack"); boolean empty2 = CollectionUtils.isEmpty(map); Assert.isTrue(empty2, "map不为空"); }
boolean containsAny(Collection source, Collection candidates)
從原始碼上的註解看,是用來判斷集合source中是否包含另一個集合candidates的任一個元素,即集合candidates中的元素是否完全包含於集合soruce。
從原始碼這個方法中的元素之間的比較用到了「equals」方法,而呼叫的是集合內物件的equals方法,因此使用這個方法想要得到正確的結果的前提是,比較的物件要重寫hashCode()和eauals()方法。
@Test public void test4(){ Employee lisi = new Employee("lisi"); Employee zhangsan = new Employee("zhangsan"); Employee wangwu = new Employee("wangwu"); List<Employee > list=new ArrayList<>(); list.add(zhangsan); list.add(lisi); List<Employee> list2=new ArrayList<>(); list2.add(wangwu); //这里可以用是因为比较的时候调用的是equals方法 boolean b = CollectionUtils.containsAny(list, list2); Assert.isTrue(b, "list1没有包含有list2中任意一个元素"); }
集合的操作
void mergeArrayIntoCollection(Object array, Collection collection)
將陣列array中的元素都加入到 List/Set 中。
@Test public void test6(){ List<Employee > list=new ArrayList<>(); Employee lisi = new Employee("lisi"); list.add(lisi); Employee zhangsan = new Employee("zhangsan"); Employee[] employees={zhangsan}; CollectionUtils.mergeArrayIntoCollection(employees, list); Assert.isTrue(list.size()==2, "把数据中的元素合并到list失败了"); }
void mergePropertiesIntoMap(Properties props, Map map)
將 Properties 中的鍵值對都加入 Map 中。
@Test public void test7(){ Properties properties = new Properties(); properties.setProperty("name", "zhangsan"); Map<String,String > map = new HashMap<>(); CollectionUtils.mergePropertiesIntoMap(properties, map); Assert.isTrue(map.get("name").equals("zhangsan"), "把properties中的元素合并到map中失败了"); } @Test public void test7(){ Properties properties = new Properties(); properties.setProperty("name", "zhangsan"); Map<String,String > map = new HashMap<>(); CollectionUtils.mergePropertiesIntoMap(properties, map); Assert.isTrue(map.get("name").equals("zhangsan"), "把properties中的元素合并到map中失败了"); }
T lastElement(List list)
回傳 List 中最後一個元素。
@Test public void test9(){ Employee lisi = new Employee("lisi"); Employee zhangsan = new Employee("zhangsan"); List<Employee > list=new ArrayList<>(); list.add(zhangsan); list.add(lisi); Employee employee = CollectionUtils.firstElement(list); Assert.isTrue(employee.equals(zhangsan), "获取集合第一个元素失败了"); }
T firstElement(List list)
傳回集合中第一個元素。
@Test public void test10(){ Employee zhangsan = new Employee("zhangsan"); Employee[] employees={zhangsan}; List list = CollectionUtils.arrayToList(employees); Assert.isTrue(list.size()==1, "把数据转换成集合失败了"); }
List arrayToList(Object source)
把一個陣列轉換成一個集合。
@Test public void test10(){ Employee zhangsan = new Employee("zhangsan"); Employee[] employees={zhangsan}; List list = CollectionUtils.arrayToList(employees); Assert.isTrue(list.size()==1, "把数据转换成集合失败了"); }
以上是Springboot內建的工具類別CollectionUtils怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中