這篇“Java集合方法怎么使用”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Java集合方法怎么使用”文章吧。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供徽州企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、網(wǎng)站制作、HTML5、小程序制作等業(yè)務(wù)。10年已為徽州眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進行中。
java集合類存放于java.util包中,是一個用來存放對象的容器
集合只能存放對象
集合存放的是多個對象的引用,對象本身還是存放在堆內(nèi)存中
集合可以存放不同類型,不限數(shù)量的數(shù)據(jù)類型
Set: 無序,不可重復(fù)的集合
List: 有序,可重復(fù)的集合
Map:具有映射關(guān)系的集合
在JDK5之后,增加了泛型,java集合可以記住容器中對象的數(shù)據(jù)類型
不能保證元素的排列順序(位置由該值的hashcode決定)
不可重復(fù)(指的是hashcode不相同)
HashSet不是線程安全的
集合元素可以存null
HashSet類實現(xiàn)set接口,set接口繼承Collection接口
HashSet集合判斷兩個元素相等的標準:兩個對象通過equals()方法比較相等,并且兩個對象的hashCode()方法返回值也相等。
如果兩個對象通過equals()方法返回true,這兩個對象的hashCode值也應(yīng)該相同。
如果要set集合存相同類型的對象需使用泛型
package com.aggregate.demo; import com.sun.corba.se.spi.ior.IORTemplateList; import java.util.HashSet; import java.util.Iterator; public class set { public static void main(String[] args) { HashSet<Object> set = new HashSet<>(); set.add(1); set.add("a");//增加元素 System.out.println(set); set.remove(1);//移除元素 System.out.println(set); System.out.println(set.contains("a"));//判斷集合中是否存在該元素 set.clear();//清空集合 System.out.println(set); //遍歷集合 set.add("a"); set.add("b"); set.add("c"); set.add("d"); //1.使用迭代器遍歷集合 Iterator<Object> iterator = set.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + "\t"); } System.out.println("==============="); //2.for each迭代集合 for (Object i : set) { System.out.print(i + "\t"); } System.out.println("==============="); System.out.println(set.size());//獲取元素的個數(shù) set.add(null); System.out.println(set); //使用泛型存相同類型的元素 HashSet<String> set1 = new HashSet<>(); set1.add("123"); // set1.add(2); } }
TreeSet是SortedSet接口的實現(xiàn)類,TreeSet可以確保集合元素處于排序狀態(tài)。
TreeSet支持兩種排序方法:自然排序和定制排序。默認情況下,TreeSet采用自然排序
排序:TreeSet會調(diào)用集合元素的compareTo(Object obj)方法來比較元素之間的大小關(guān)系,然后將集合元素按升序排列
自定義類如何排序?
import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; public class Tree { public static void main(String[] args) { TreeSet<Integer> treeSet = new TreeSet<>(); //TreeSet自然排序 treeSet.add(5); treeSet.add(1); treeSet.add(3); treeSet.add(2); treeSet.add(4); System.out.println(treeSet); treeSet.remove(3); System.out.println(treeSet); System.out.println(treeSet.contains(0)); treeSet.clear(); System.out.println(treeSet); Iterator<Integer> iterator = treeSet.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println("============="); for (Integer i : treeSet) { System.out.println(i); } Person P1 = new Person(23, "張三"); Person P2 = new Person(25, "李四"); Person P3 = new Person(12, "王五"); Person P4 = new Person(5, "Lucy"); Person P5 = new Person(99, "hhhh"); TreeSet<Person> people = new TreeSet<>(new Person()); people.add(P1); people.add(P2); people.add(P3); people.add(P4); people.add(P5); for (Person i : people) { System.out.println(i.name + " " + i.age); } } } //把person對象存到TreeSet中并且按照年齡排序 class Person implements Comparator<Person> { int age; String name; public Person() { } public Person(int age, String name) { this.age = age; this.name = name; } @Override public int compare(Person o1, Person o2) {//年齡正序排序 if (o1.age > o2.age) { return 1; } else if (o1.age < o2.age) { return -1; } else { return 0; } } }
List代表一個元素有序、且可重復(fù)的集合,集合中的每個元素都有其對應(yīng)的順序索引
List允許使用重復(fù)元素,可以通過索引來訪問指定位置的集合元素
List默認按元素的添加順序設(shè)置元素的索引
List集合里添加了一些根據(jù)索引來操作集合元素的方法
ArrayList和Vector是List接口的兩個典型實現(xiàn)
區(qū)別:
Vector是一個古老的集合,通常建議使用ArrayList
ArrayList是線程不安全的,而Vector是線程安全的
即使為保證List集合線程安全,也不推薦使用VectorMap
用于保存具有映射關(guān)系的數(shù)據(jù),因此Map集合里保存著兩組值,一組值用于保存Map里key,另外一組用于保存Map里的Value
Map中的key和value都可以是任何引用類型的數(shù)據(jù)
Map中的key不允許重復(fù),即同一個Map對象的任何兩個Key通過equals方法比較返回false
key和value之間存在單向一對一關(guān)系,即通過指定的key總能找到唯一的,確定的Value
HashMap & Hashtable
HashMap和Hashtable是Map接口的兩個典型實現(xiàn)類
區(qū)別:
Hashtable是一個古老的Map實現(xiàn)類,不建議使用
Hashtable是線程安全的Map實現(xiàn),但HashMap是線程不安全的
Hashtable不允許使用null作為key和value,而HashMap可以
與HashSet集合不能保證元素的順序一樣,Hashtable、HashMap也不能保證其中key-value對的順序
Hashtable、HashMap判斷兩個key的標準是:key通過equals方法返回true,hashCode值也相等
Hashta5ble相等的標準是:兩個Value通過equalHashMap判斷兩個Value方法返回true
import java.util.HashMap; import java.util.Map; import java.util.Set; public class MapDemo { public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("b", 1);//添加數(shù)據(jù) map.put("c", 2); map.put("d", 3); System.out.println(map); System.out.println(map.get("d"));//根據(jù)key取值 map.remove("c"); System.out.println(map);//根據(jù)key鍵值對 System.out.println(map.size());//map集合的長度 System.out.println(map.containsKey("a"));//判斷當前的map集合是否包含指定的key System.out.println(map.containsValue(10));//判斷當前的map集合是否包含指定的value // map.clear();//清空集合 Set<String> keys = map.keySet();//可以獲取map集合的key的集合 map.values();//獲取集合的所有value值 //遍歷map集合,通過map.keySet(); for (String key : keys) { System.out.println("key:" + key + ", value:" + map.get(key)); } //通過map.entrySet();遍歷集合 Set<Map.Entry<String, Integer>> entries = map.entrySet(); for (Map.Entry<String, Integer> entry : entries) { System.out.println("key:" + entry.getKey() + ", value:" + entry.getValue()); } } }
TreeMap存儲key-value對時,需要根據(jù)key對key-value對進行排序。TreeMap可以保證所有的key-value對處于有序狀態(tài)
TreeMap的key排序
自然排序:TreeMap的所有的key必須實現(xiàn)Comparable接口,而且所有的key應(yīng)該是同一個類的對象,否則將會拋出ClassCastException
定制排序(了解):創(chuàng)建TreeMap時,傳入一個Comparator對象,該對象負責(zé)對TreeMap中的所有key排序。此時不需要Map的key實現(xiàn)Comparator接口
import java.util.Map; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) { //TreeMap的自然排序是字典 Map<Integer, String> treemap = new TreeMap<Integer, String>(); treemap.put(4, "a"); treemap.put(3, "b"); treemap.put(2, "c"); treemap.put(1, "d"); System.out.println(treemap); Map<String, String> map = new TreeMap<String, String>(); map.put("a", "a"); map.put("c", "a"); map.put("d", "a"); map.put("b", "a"); map.put("ab", "a"); System.out.println(map); } }
Collections是一個操作Set 、List和Map等集合的工具類
Collections中提供了大量方法對集合元素進行排序、查詢和修改等操作,還提供了對集合對象設(shè)置不可變,對集合對象實現(xiàn)同步控制等方法
排序操作:
reverse(List):反轉(zhuǎn)List中元素的順序
shuffle(List):對List集合元素進行隨機排序
sort(List):根據(jù)元素的自然順序?qū)χ付↙ist集合元素升序排序
sort(List,Comparator):根據(jù)指定的Comparator產(chǎn)生的順序?qū)ist集合元素進行排序s
wap(List,int,int):將指定list集合中的i處元素和j處元素進行交換
Object max(Collection):
根據(jù)元素的自然順序,返回給定集合中的最大元素
Object max(Collection,Comparator):
根據(jù)Comparator指定的順序,返回給定集合中的最大元素
Object min(Collection)
Object min(Collection,Comparator)
int frequency(Collection,Object):
返回指定集合中指定元素的出現(xiàn)次數(shù)
boolean replaceAll(List list,Object oldVal,Object newVal):
使用新值替換List對象的所有舊值
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("a"); list.add("c"); list.add("d"); list.add("f"); list.add("b"); System.out.println(list); Collections.reverse(list);//反轉(zhuǎn)List中元素的順序 System.out.println(list); Collections.shuffle(list);//對list集合元素進行順序排序 System.out.println(list); Collections.sort(list);//list集合字典升序排序 System.out.println(list); Student s1 = new Student(14, "張三"); Student s2 = new Student(12, "李四"); Student s3 = new Student(13, "王五"); Student s4 = new Student(11, "小劉"); List<Student> students = new ArrayList<Student>(); students.add(s1); students.add(s2); students.add(s3); students.add(s4); for (Student student : students) { System.out.println(student.name + "," + student.age); } Collections.sort(students, new Student()); System.out.println("=========="); for (Student student : students) { System.out.println(student.name + "," + student.age); } Collections.swap(list, 1, 3);//將指定list集合中的i處元素和j處元素進行交換 System.out.println(list); System.out.println(Collections.max(list)); System.out.println(Collections.min(list)); Student max = Collections.max(students, new Student()); Student min = Collections.min(students, new Student()); System.out.println(max.name + ", " + max.age); System.out.println(min.name + ", " + min.age); System.out.println(Collections.frequency(list, "a")); System.out.println(Collections.replaceAll(list, "a", "aa")); System.out.println(list); } } class Student implements Comparator<Student> { int age; String name; public Student() { } public Student(int age, String name) { this.age = age; this.name = name; } @Override //根據(jù)年齡升序排序?qū)ο? public int compare(Student o1, Student o2) { if (o1.age > o2.age) { return 1; } else if (o1.age < o2.age) { return -1; } else { return 0; } } }
Collections類中提供了多個synchronizedxxx()方法該方法可使指定集合包裝成線程同步的集合;從而解決多線程并訪問集合時的線程安全問題。
集合中使用泛型時只有指定類型才可以添加到集合中,類型安全
java中的泛型,只在編譯階段有效。
泛型類
對象實例化時不指定泛型,默認為:object
泛型不同的引用不能相互賦值
public class Test2 { public static void main(String[] args) { A<String> a = new A<String>(); a.setKey("rexx"); String s = a.getKey(); System.out.println(s); } } class A<T> { private T key; public T getKey() { return key; } public void setKey(T key) { this.key = key; } }
泛型接口
定義一個泛型接口
未傳入泛型實參時,與泛型類的定義相同,在聲明類的時候,需將泛型的聲明也一起加到類中
泛型方法
package com.aggregate.demo; public class Test3 { public static void main(String[] args) { B1<Object> b1 = new B1<Object>(); B1<String> b2 = new B1<String>(); B2 b3 = new B2(); Cc cc = new Cc(); cc.test("xxx"); //泛型方法,在調(diào)用之前沒有固定的數(shù)據(jù)類型 //在調(diào)用時,傳入的參數(shù)是什么類型,就會把泛型改成什么類型 //也就是說,泛型方法會在調(diào)用時確定泛型具體的數(shù)據(jù)類型 Integer integer = cc.test1(2); Boolean aBoolean = cc.test1(true); } } //定義泛型接口 interface IB<T> { T test(T t); } //未傳入泛型實參時,與泛型類的定義相同,在聲明類的時候,需將泛型的聲明也一起加到類中 class B1<T> implements IB<T> { @Override public T test(T t) { return null; } } //傳入實際參數(shù) //如果實現(xiàn)接口時指定接口的泛型的具體數(shù)據(jù)類型 //這個類實現(xiàn)接口所有方法的位置都要泛型替換實際的具體數(shù)據(jù)類型 class B2 implements IB<String> { @Override public String test(String s) { return null; } } //泛型方法 class Cc { public void test() { } //無返回值的泛型方法 public <T> void test(T s) { T t = s; } public String test1(String s) { return s; } //有返回值的泛型方法 public <T> T test1(T s) { return s; } public void test2(String... strs) { for (String s : strs) { System.out.println(s); } } //形參為可變參數(shù)的泛型方法 public <T> void test2(T... strs) { for (T str : strs) { System.out.println(str); } } } //帶泛型的類可以在類里面定義泛型的變量 class Dd<E> { private E e; //靜態(tài)的泛型方法 public static <T> void test3(T t) { //System.out.println(this.e); //在靜態(tài)方法中,不能使用類定義泛型,如果要使用泛型,只能使用靜態(tài)方法自己定義的泛型 System.out.println(t); } //在類上定義的泛型,可以在普通的方法中使用 public <T> void test(T s) { System.out.println(this.e); T t = s; } }
通配符
1.有限制的通配符
(無窮小,Person]只允許泛型為Person及Person子類的引用調(diào)用
[Person,無窮大)只允許泛型為Person及Person父類的引用調(diào)用
只允許泛型為實現(xiàn)Comparable接口的實現(xiàn)類的引用調(diào)用
在某些情況下,一個類的對象是有限而且固定的。例如季節(jié)類,只能有4個對象。
手動實現(xiàn)枚舉類:
private修飾構(gòu)造器
屬性使用private final修飾
把該類的所有實例都使用public static final來修飾
實現(xiàn)接口的枚舉類
和普通Java類一樣枚舉類可以實現(xiàn)一個或多個接口
若需要每個枚舉值在調(diào)用實現(xiàn)的接口方法呈現(xiàn)出不同的行為方式,則可以讓每個枚舉值分別來實現(xiàn)該方法
public class Test5 { public static void main(String[] args) { //Season.SPRING,這段執(zhí)行就是獲取一個Season的對象 Season spring = Season.SPRING; spring.showInfo(); Season summer = Season.SUMMER; summer.showInfo(); Season spring1 = Season.SPRING; //每次執(zhí)行Season.SPRING獲得是相同的對象,枚舉類中的每個枚舉都是單例模式的 System.out.println(spring.equals(spring1)); spring1.test(); } } enum Season implements ITest { SPRING("春", "春暖花開"),//此處相當于調(diào)用有參的私有構(gòu)造 SUMMER("夏", "夏日炎炎"), AUTUMN("秋", "秋高氣爽"), WINTER("冬", "寒風(fēng)凜冽"); private final String name; private final String desc; Season(String name, String desc) { this.name = name; this.desc = desc; } public void showInfo() { System.out.println(this.name + ":" + this.desc); } @Override public void test() { System.out.println("這是實現(xiàn)的ITest接口的test方法"); } } interface ITest { void test(); }
Annotation其實就是代碼里的特殊標記,這些標記可以在編譯,類加載,運行時被讀取,并執(zhí)行相應(yīng)的處理。通過使用Annotation,程序員可以在不改變原有邏輯的情況下,在源文件中嵌入一些補充信息
Annotation可以像修飾符一樣被使用,可用于修飾包,類,構(gòu)造器,方法,成員變量,參數(shù),局部變量的聲明,這些信息被保存在Annotation的name=value對中
Anotation能被用來為程序元素(類,方法,成員變量等)設(shè)置元數(shù)據(jù)
使用Annotation時要在其前面增加@符號,并把該Annotation當成一個修飾符使用。用于修飾它支持的程序元素
三個基本的Annotation:
@Override:限定重寫父類方法,該注釋只能用于方法
Deprecated:用于表示某個程序元素(類、方法等)已過時
@SuppressWarnings:抑制編譯器警告
自定義新的Annotation類型使用@interface關(guān)鍵字
Annotation的成員變量在Annotation定義中以無參數(shù)方法的形式來聲明。其方法名和返回值定義了該成員的名字和類型
可以在定義Annotation的成員變量時為其指定初始值,指定成員變量的初始值可使用default關(guān)鍵字
沒有成員定義的Annotation稱為標記;包含成員變量的Annotation稱為元數(shù)據(jù)的Annotation
import java.lang.annotation.*; import java.util.ArrayList; import java.util.List; public class Test6 { public static void main(String[] args) { new TestB().test01(); @SuppressWarnings({}) List list = new ArrayList(); } } class TestA { public void test() { } } class TestB extends TestA { @TestAnn(id = 100, desc = "姓名") String name; @Override public void test() { super.test(); } @Deprecated public void test01() { } } @Target(ElementType.FIELD)//這個注解類是給其他類的屬性做注解 @Retention(RetentionPolicy.RUNTIME)//定義注解的聲明周期 @Documented @interface TestAnn { public int id() default 0; public String desc() default ""; }
以上就是關(guān)于“Java集合方法怎么使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標題:Java集合方法怎么使用
網(wǎng)址分享:http://www.chinadenli.net/article10/iejggo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、ChatGPT、網(wǎng)站策劃、網(wǎng)站內(nèi)鏈、自適應(yīng)網(wǎng)站、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)