Java-collections用法代码示例总结
纸上得来终觉浅,绝知此事要躬行
--陆游 问渠那得清如许,为有源头活水来 --朱熹
类Collections是一个包装类。它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,服务于Java的Collection框架。
java.lang.Object java.util.Collections
Collections中常用的方法:
(1)sort()排序方法
函数定义:public static
自然顺序对指定列表按升序进行排序。
参数:要排序的列表。
函数定义: public static
参数:list-要排序的列表;c-确定列表顺序的比较器。
(2)binarySearch()二分查找方法
函数定义:public static
使用二分搜索法搜索指定列表,以获得指定对象,在进行此方法调用前比较要将列表元素按照升序排序,否则结果不确定,此方法会执行O(n)次链接遍历和O(log n)次元素比较。
参数: list-要搜索的链表,key-要搜索的键。
函数定义: public static
参数:list-要搜索的列表,key-要搜索的键,c-排序列表的比较器。
(3)reverse()反转方法
函数定义:public static void reverse(List> list),反转指定列表中元素的顺序,此方法以线性时间运行。
参数:list-元素要被反转的列表
(4)shuffle()改组方法
函数定义:public static void shuffle(List> list),使用默认随机源对指定列表进行置换,所有置换发生的可能性都是大致相等的。
参数:list-要改组的列表
函数定义:public static void shuffle(List> list,Random rnd),使用指定的随机源对指定列表进行置换。
参数:list-要改组的列表,rnd-用来改组列表的随机源。
(5)swap()交换方法
函数定义:public static void swap(List> list,int i,int j),在指定列表的指定位置处交换元素。
参数:list-进行元素交换的列表,i-要交换的一个元素的索引,j-要交换的另一个元素的索引。
(6)fill()替换方法
函数定义:public static
参数:list-使用指定元素填充的列表,obj-用来填充指定列表的元素。
(7)copy()复制方法
函数定义:public static
参数:dest-目标列表,src-源列表。
(8)min()最小值法
函数定义:public static
参数:coll-将确定其最小元素的collection。
函数定义:public static
参数:coll-将确定其最小元素的collection,comp-用来确定最小元素的比较器。
(9)max()最大值方法
函数定义:public static
参数:coll-将确定其最大元素的collection。
函数定义:public static
参数:coll-将确定其最大元素的collection,comp-用来确定最大元素的比较器
(10)rotate()轮换方法
函数定义:public static void rotate(List> list,int distance),根据指定的距离轮转指定列表中的元素。
参数:list-要轮换的列表,distance-列表轮换的距离,可以使0、负数或者大于list.size()的数。
(11)replaceAll()替换所有函数
函数定义:public static
参数:list-在其中进行替换的列表;oldVal-将被替换的原值;newVal-替换oldVald的新值。
示例代码:
public class Hello { public static void main(String[] args) { System.out.println("sort"); List list=new ArrayList<Double>(); double array[] = {112, 111, 23, 456, 231 }; for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } Collections.sort(list);//自然排序 for (int i = 0; i < array.length; i++) { System.out.println(list.get(i)); } System.out.println("shuffle"); Collections.shuffle(list);//置换 for (int i = 0; i < array.length; i++) { System.out.println(list.get(i)); } Collections.sort(list);//自然排序 System.out.println("reverse"); Collections. reverse (list);//反转 for (int i = 0; i < array.length; i++) { System.out.println(list.get(i)); } Collections.sort(list);//自然排序 System.out.println("copy"); List li = new ArrayList(); double arr[] = {1131,333}; for(int j=0;j<arr.length;j++){ li.add(new Double(arr[j])); } Collections.copy(list,li);//拷贝 for (int i = 0; i <list.size(); i++) { System.out.println(list.get(i)); } System.out.println("min"); System.out.println(Collections.min(list));//返回最小值 System.out.println("max"); System.out.println(Collections.max(list));//返回最大值 System.out.println("rotate"); Collections.rotate(list,-1);//循环 for (int i = 0; i <list.size(); i++) { System.out.println( list.get(i)); } System.out.println("binarySearch"); Collections.sort(list); System.out.println(list); System.out.println(Collections.binarySearch(list, 333.0));//二分查找 } }
以上是Collections比较常用的方法,Collections还有很多其他的方法,如下表:
方法摘要 | |
---|---|
static |
<span style="background-color:inherit; color:rgb(51,51,51)">addAll</span>(Collection<?
super T> c, T... elements) 将所有指定元素添加到指定 collection 中。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">asL<a href="http://www.php.cn/wiki/109.html" target="_blank">if</a>oQueue</span>(Deque<T> deque) 以后进先出 (Lifo) Queue 的形式返回某个 Deque 的视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">binarySearch</span>(List<?
extends Comparable<? super T>> list, T key) 使用二分搜索法搜索指定列表,以获得指定对象。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">binarySearch</span>(List<?
extends T> list, T key, Comparator<? super T> c) 使用二分搜索法搜索指定列表,以获得指定对象。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">checkedCollection</span>(Collection<E> c,
Class<E> type) 返回指定 collection 的一个动态类型安全视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">checke<a href="http://www.php.cn/wiki/596.html" target="_blank">dL</a>ist</span>(List<E> list,
Class<E> type) 返回指定列表的一个动态类型安全视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">checkedMap</span>(Map<K,V> m,
Class<K> keyType, Class<V> valueType) 返回指定映射的一个动态类型安全视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">checkedSet</span>(Set<E> s,
Class<E> type) 返回指定 set 的一个动态类型安全视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">checkedSortedMap</span>(SortedMap<K,V> m,
Class<K> keyType, Class<V> valueType) 返回指定有序映射的一个动态类型安全视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">checkedSortedSet</span>(SortedSet<E> s,
Class<E> type) 返回指定有序 set 的一个动态类型安全视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">copy</span>(List<? super T> dest,
List<? extends T> src) 将所有元素从一个列表复制到另一个列表。 |
static boolean |
<span style="background-color:inherit; color:rgb(51,51,51)">disjoint</span>(Collection<?> c1,
Collection<?> c2) 如果两个指定 collection 中没有相同的元素,则返回 true 。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">emptyList</span>() 返回空的列表(不可变的)。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">emptyMap</span>() 返回空的映射(不可变的)。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">emptySet</span>() 返回空的 set(不可变的)。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">enumeration</span>(Collection<T> c) 返回一个指定 collection 上的枚举。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">fill</span>(List<? super T> list,
T obj) 使用指定元素替换指定列表中的所有元素。 |
static int |
<span style="background-color:inherit; color:rgb(51,51,51)">frequency</span>(Collection<?> c,
Object o) 返回指定 collection 中等于指定对象的元素数。 |
static int |
<span style="background-color:inherit; color:rgb(51,51,51)">indexOfSubList</span>(List<?> source,
List<?> target) 返回指定源列表中第一次出现指定目标列表的起始位置;如果没有出现这样的列表,则返回 -1。 |
static int |
<span style="background-color:inherit; color:rgb(51,51,51)">lastIndexOfSubList</span>(List<?> source,
List<?> target) 返回指定源列表中最后一次出现指定目标列表的起始位置;如果没有出现这样的列表,则返回 -1。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">list</span>(Enumeration<T> e) 返回一个数组列表,它按返回顺序包含指定枚举返回的元素。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">max</span>(Collection<? extends
T> coll) 根据元素的自然顺序,返回给定 collection 的最大元素。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">max</span>(Collection<? extends
T> coll, Comparator<? super T> comp) 根据指定比较器产生的顺序,返回给定 collection 的最大元素。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">min</span>(Collection<? extends
T> coll) 根据元素的自然顺序 返回给定 collection 的最小元素。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">min</span>(Collection<? extends
T> coll, Comparator<? super T> comp) 根据指定比较器产生的顺序,返回给定 collection 的最小元素。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">nCopies</span>(int n, T o) 返回由指定对象的 n 个副本组成的不可变列表。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">newSetFromMap</span>(Map<E,Boolean> map) 返回指定映射支持的 set。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">replaceAll</span>(List<T> list,
T oldVal, T newVal) 使用另一个值替换列表中出现的所有某一指定值。 |
static void |
<span style="background-color:inherit; color:rgb(51,51,51)">reverse</span>(List<?> list) 反转指定列表中元素的顺序。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">reverseOrder</span>() 返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的自然顺序。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">reverseOrder</span>(Comparator<T> cmp) 返回一个比较器,它强行逆转指定比较器的顺序。 |
static void |
<span style="background-color:inherit; color:rgb(51,51,51)">rotate</span>(List<?> list,
int distance) 根据指定的距离轮换指定列表中的元素。 |
static void |
<span style="background-color:inherit; color:rgb(51,51,51)">shuffle</span>(List<?> list) 使用默认随机源对指定列表进行置换。 |
static void |
<span style="background-color:inherit; color:rgb(51,51,51)">shuffle</span>(List<?> list,
Random rnd) 使用指定的随机源对指定列表进行置换。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">singleton</span>(T o) 返回一个只包含指定对象的不可变 set。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">singletonList</span>(T o) 返回一个只包含指定对象的不可变列表。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">singletonMap</span>(K key,
V value) 返回一个不可变的映射,它只将指定键映射到指定值。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">sort</span>(List<T> list) 根据元素的自然顺序 对指定列表按升序进行排序。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">sort</span>(List<T> list,
Comparator<? super T> c) 根据指定比较器产生的顺序对指定列表进行排序。 |
static void |
<span style="background-color:inherit; color:rgb(51,51,51)">swap</span>(List<?> list,
int i, int j) 在指定列表的指定位置处交换元素。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">syn<a href="http://www.php.cn/wiki/1332.html" target="_blank">chr</a>onizedCollection</span>(Collection<T> c) 返回指定 collection 支持的同步(线程安全的)collection。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">synchronizedList</span>(List<T> list) 返回指定列表支持的同步(线程安全的)列表。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">synchronizedMap</span>(Map<K,V> m) 返回由指定映射支持的同步(线程安全的)映射。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">synchronizedSet</span>(Set<T> s) 返回指定 set 支持的同步(线程安全的)set。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">synchronizedSortedMap</span>(SortedMap<K,V> m) 返回指定有序映射支持的同步(线程安全的)有序映射。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">synchronizedSortedSet</span>(SortedSet<T> s) 返回指定有序 set 支持的同步(线程安全的)有序 set。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">unmodifiableCollection</span>(Collection<?
extends T> c) 返回指定 collection 的不可修改视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">unmodifiableList</span>(List<?
extends T> list) 返回指定列表的不可修改视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">unmodifiableMap</span>(Map<?
extends K,? extends V> m) 返回指定映射的不可修改视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">unmodifiableSet</span>(Set<?
extends T> s) 返回指定 set 的不可修改视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">unmodifiableSortedMap</span>(SortedMap<K,?
extends V> m) 返回指定有序映射的不可修改视图。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">unmodifiableSortedSet</span>(SortedSet<T> s) 返回指定有序 set 的不可修改视图。 |
Atas ialah kandungan terperinci Java-collections用法代码示例总结. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas



Panduan Nombor Sempurna di Jawa. Di sini kita membincangkan Definisi, Bagaimana untuk menyemak nombor Perfect dalam Java?, contoh dengan pelaksanaan kod.

Panduan untuk Penjana Nombor Rawak di Jawa. Di sini kita membincangkan Fungsi dalam Java dengan contoh dan dua Penjana berbeza dengan contoh lain.

Panduan untuk Weka di Jawa. Di sini kita membincangkan Pengenalan, cara menggunakan weka java, jenis platform, dan kelebihan dengan contoh.

Panduan untuk Nombor Smith di Jawa. Di sini kita membincangkan Definisi, Bagaimana untuk menyemak nombor smith di Jawa? contoh dengan pelaksanaan kod.

Dalam artikel ini, kami telah menyimpan Soalan Temuduga Spring Java yang paling banyak ditanya dengan jawapan terperinci mereka. Supaya anda boleh memecahkan temuduga.

Java 8 memperkenalkan API Stream, menyediakan cara yang kuat dan ekspresif untuk memproses koleksi data. Walau bagaimanapun, soalan biasa apabila menggunakan aliran adalah: bagaimana untuk memecahkan atau kembali dari operasi foreach? Gelung tradisional membolehkan gangguan awal atau pulangan, tetapi kaedah Foreach Stream tidak menyokong secara langsung kaedah ini. Artikel ini akan menerangkan sebab -sebab dan meneroka kaedah alternatif untuk melaksanakan penamatan pramatang dalam sistem pemprosesan aliran. Bacaan Lanjut: Penambahbaikan API Java Stream Memahami aliran aliran Kaedah Foreach adalah operasi terminal yang melakukan satu operasi pada setiap elemen dalam aliran. Niat reka bentuknya adalah

Panduan untuk TimeStamp to Date di Java. Di sini kita juga membincangkan pengenalan dan cara menukar cap waktu kepada tarikh dalam java bersama-sama dengan contoh.

Java ialah bahasa pengaturcaraan popular yang boleh dipelajari oleh pembangun pemula dan berpengalaman. Tutorial ini bermula dengan konsep asas dan diteruskan melalui topik lanjutan. Selepas memasang Kit Pembangunan Java, anda boleh berlatih pengaturcaraan dengan mencipta program "Hello, World!" Selepas anda memahami kod, gunakan gesaan arahan untuk menyusun dan menjalankan program, dan "Hello, World!" Pembelajaran Java memulakan perjalanan pengaturcaraan anda, dan apabila penguasaan anda semakin mendalam, anda boleh mencipta aplikasi yang lebih kompleks.
