java集合类总结

本文深入探讨了Java集合框架的核心概念,包括Collection、List、Set和Map接口的主要特性及其实现。详细介绍了各种集合类型的用途、操作方法,并通过具体示例展示了如何使用这些集合来管理和操作数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java集合类总结

Collection接口通常不能直接使用,但是该接口提供了添加删除管理数据的方法,由于List接口和Set接口都实现了他的方法,因此这些方法对List和Set集合都是通用的。

方法如下:

 

1
2
3
4
5
add(Element e)                                    添加特定对象进集合
remove(Object o)                                  移除
isEmpty()-----Boolean                             判断集合是否为空
iterator()------iterator                          返回迭代器用来遍历集合中的对象
size()--------int                                 获取集合元素的个数

 

复制代码
 1 package zf.collection.list;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 import java.util.Iterator;
 6 
 7 import javax.jws.soap.SOAPBinding;
 8 
 9 public class Zfcollection {
10     private Collection<Integer> co=new ArrayList<Integer> ();
11     public Collection<Integer> getCo() {
12         return co;
13     }
14     public void setCo(Collection<Integer> co) {
15         this.co = co;
16     }
17     private int temp;
18     private Iterator<Integer> it;
19     public Zfcollection() {
20         // TODO Auto-generated constructor stub
21         co.add(11);
22         co.add(12);
23         co.add(13);
24         co.add(14);
25         co.add(15);    
26     }
27     public void print(){
28 
29         it=co.iterator();
30         while(it.hasNext()){
31             temp=it.next();
32             System.out.print(temp+" ");
33             System.out.println();
34         }
35     }
36     public void delete(){
37         co.remove(11);
38         co.remove(12);
39         it=co.iterator();
40         while(it.hasNext()){
41             temp=it.next();
42             System.out.print(temp+" ");
43         }
44         System.out.println();
45     }
46     public void size(){
47         System.out.println(co.size());
48         co.remove(13);
49         co.remove(14);
50         co.remove(15);
51         System.out.println(co.isEmpty());
52     }
53 }
复制代码
复制代码
 1 package Test;
 2 
 3 import zf.collection.list.*;
 4 import zf.collection.set.*;
 5 import zf.map.Zfmap;
 6 
 7 
 8 public class Main {
 9     public static void main(String args[]){
10 //        System.out.println("1.Collection接口的方法…………");
11 //        Zfcollection zfcollection=new Zfcollection();
12 //        zfcollection.print();
13 //        zfcollection.delete();
14 //        zfcollection.size();
15 //        System.out.println(".................................................................");
16         
17 //        System.out.println("2.List接口的方法…………");
18 //        Zflist zl=new Zflist();
19 //        zl.getsetprint();
20 //        System.out.println(".................................................................");
21         
22 //        System.out.println("3.Set接口的方法…………");
23 //        Zfset zs=new Zfset();
24 //        zs.print();
25 //        zs.printtreenewfangf();
26 //        System.out.println(".................................................................");
27         
28         System.out.println("4.Map接口的方法…………");
29         Zfmap zm=new Zfmap();
30         
31     }
32 }
复制代码

 

List接口中元素允许重复,顺序就是对象插入的顺序,类似java数组,用户可通过使用索引来访问集合中的元素。

1
2
3
get(int index)
 
set(int index,Object obj)

 

复制代码
 1 package zf.collection.list;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 import java.util.Iterator;
 6 import java.util.List;
 7 
 8 import zf.collection.list.*;
 9 
10 public class Zflist {
11     private Zfcollection zc=new Zfcollection();
12     ArrayList<Integer> list=(ArrayList<Integer>) zc.getCo();
13     public void getsetprint(){
14         int a=list.get(0);
15         System.out.println("在第一个节点的是:"+a);
16         list.set(4, 2345);//前提是存在这个节点。
17     //    Iterator<Integer>  iterator=list.iterator();
18         for(int li:list){
19             System.out.println(li);
20         }
21     }
22 }
复制代码

 

Set集合中的对象不按特定方式排序,只是简单地把对象加入集合中,Set集合中不包含重复对象

 

Method Summary

All Methods  Instance Methods  Concrete Methods 
Modifier and TypeMethod and Description
booleanadd(E e)
Adds the specified element to this set if it is not already present.
booleanaddAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this set.
Eceiling(E e)
Returns the least element in this set greater than or equal to the given element, or  null if there is no such element.
voidclear()
Removes all of the elements from this set.
Objectclone()
Returns a shallow copy of this  TreeSet instance.
Comparator<? super E>comparator()
Returns the comparator used to order the elements in this set, or  null if this set uses the  natural ordering of its elements.
booleancontains(Object o)
Returns  true if this set contains the specified element.
Iterator<E>descendingIterator()
Returns an iterator over the elements in this set in descending order.
NavigableSet<E>descendingSet()
Returns a reverse order view of the elements contained in this set.
Efirst()
Returns the first (lowest) element currently in this set.
Efloor(E e)
Returns the greatest element in this set less than or equal to the given element, or  null if there is no such element.
SortedSet<E>headSet(E toElement)
Returns a view of the portion of this set whose elements are strictly less than  toElement.
NavigableSet<E>headSet(E toElement, boolean inclusive)
Returns a view of the portion of this set whose elements are less than (or equal to, if  inclusive is true)  toElement.
Ehigher(E e)
Returns the least element in this set strictly greater than the given element, or  null if there is no such element.
booleanisEmpty()
Returns  true if this set contains no elements.
Iterator<E>iterator()
Returns an iterator over the elements in this set in ascending order.
Elast()
Returns the last (highest) element currently in this set.
Elower(E e)
Returns the greatest element in this set strictly less than the given element, or  null if there is no such element.
EpollFirst()
Retrieves and removes the first (lowest) element, or returns  null if this set is empty.
EpollLast()
Retrieves and removes the last (highest) element, or returns  null if this set is empty.
booleanremove(Object o)
Removes the specified element from this set if it is present.
intsize()
Returns the number of elements in this set (its cardinality).
Spliterator<E>spliterator()
Creates a  late-binding and  fail-fast  Spliterator over the elements in this set.
NavigableSet<E>subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
Returns a view of the portion of this set whose elements range from  fromElement to  toElement.
SortedSet<E>subSet(E fromElement, E toElement)
Returns a view of the portion of this set whose elements range from  fromElement, inclusive, to  toElement, exclusive.
SortedSet<E>tailSet(E fromElement)
Returns a view of the portion of this set whose elements are greater than or equal to  fromElement.
NavigableSet<E>tailSet(E fromElement, boolean inclusive)
Returns a view of the portion of this set whose elements are greater than (or equal to, if  inclusive is true)  fromElement.

复制代码
 1 package zf.collection.set;
 2 
 3 import java.util.Collection;
 4 import java.util.Comparator;
 5 import java.util.HashSet;
 6 import java.util.TreeSet;
 7 import java.util.Iterator;
 8 import java.util.Set;
 9 //import java.util.Collection.*;
10 
11 public class Zfset {
12     
13 //    Set<Integer> s=new HashSet<Integer>();
14     TreeSet<Integer> s=new TreeSet<Integer>();
15     Iterator<Integer> it;
16     int temp;
17     
18     public Zfset() {
19         // TODO Auto-generated constructor stub
20         s.add(11);
21         s.add(12);    
22         s.add(13);
23         s.add(14);    
24         s.add(15);
25         s.add(16);    
26         s.add(17);
27         s.add(18);    
28         s.add(19);
29         s.add(20);    
30         s.add(11);
31         s.add(12);    
32     }
33     
34     public void print(){
35         it=s.iterator();
36         while(it.hasNext()){
37             temp=it.next();
38             System.out.print(temp+" ");
39             System.out.println();
40         }
41     }
42     
43     public void printtreenewfangf(){
44         /**
45          * 以下方法是TresSet类新增的方法。
46          * 只有Tree类可以调用新方法,其父类不可。
47          * */
48         int begin=s.first();
49         int terminal=s.last();
50         Comparator cp=s.comparator();
51         Set<Integer> headset=s.subSet(12, 17);//规则是:[),headSet  subSet tailSet
52         
53         System.out.println(begin+" "+terminal+" ");
54 //        for(int a:s){
55 //            System.out.println(a);
56 //        }
57         System.out.println("headset:");
58         for(int a:headset){
59             System.out.println(a);
60         }
61         //System.out.println(cp.toString());
62     }
63 }
复制代码

Map

提供key到value的映射,Map不能包含相同的key,每个key只能映射一个value。key决定了存储对象在映射中的存储位置,但不是由key对象本身决定的,通过散列技术进行处理。

Method Summary

All Methods  Instance Methods  Abstract Methods  Default Methods 
Modifier and TypeMethod and Description
voidclear()
Removes all of the mappings from this map (optional operation).
default Vcompute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or  null if there is no current mapping).
default VcomputeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to  null), attempts to compute its value using the given mapping function and enters it into this map unless  null.
default VcomputeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
booleancontainsKey(Object key)
Returns  true if this map contains a mapping for the specified key.
booleancontainsValue(Object value)
Returns  true if this map maps one or more keys to the specified value.
Set<Map.Entry<K,V>>entrySet()
Returns a  Set view of the mappings contained in this map.
booleanequals(Object o)
Compares the specified object with this map for equality.
default voidforEach(BiConsumer<? super K,? super V> action)
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
Vget(Object key)
Returns the value to which the specified key is mapped, or  null if this map contains no mapping for the key.
default VgetOrDefault(Object key, V defaultValue)
Returns the value to which the specified key is mapped, or  defaultValue if this map contains no mapping for the key.
inthashCode()
Returns the hash code value for this map.
booleanisEmpty()
Returns  true if this map contains no key-value mappings.
Set<K>keySet()
Returns a  Set view of the keys contained in this map.
default Vmerge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
Vput(K key, V value)
Associates the specified value with the specified key in this map (optional operation).
voidputAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map (optional operation).
default VputIfAbsent(K key, V value)
If the specified key is not already associated with a value (or is mapped to  null) associates it with the given value and returns  null, else returns the current value.
Vremove(Object key)
Removes the mapping for a key from this map if it is present (optional operation).
default booleanremove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.
default Vreplace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.
default booleanreplace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.
default voidreplaceAll(BiFunction<? super K,? super V,? extends V> function)
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
intsize()
Returns the number of key-value mappings in this map.
Collection<V>values()
Returns a  Collection view of the values contained in this map.

 

 

复制代码
 1 package zf.map;
 2 
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Iterator;
 6 import java.util.Map;
 7 import java.util.Set;
 8 
 9 public class Zfmap {
10     Map<Integer, Integer> zm=new HashMap<Integer, Integer>();
11     public Zfmap() {
12         // TODO Auto-generated constructor stub
13         zm.put(1, 101);
14         zm.put(2, 102);
15         zm.put(3, 103);
16         zm.put(4, 104);
17         zm.put(5, 105);
18         Set<Integer> set=zm.keySet();
19         for(int a:set){
20             System.out.println(a);
21         }
22         Collection<Integer> collection=zm.values();
23         for(int b:collection){
24             System.out.println(b);
25         }
26         zm.remove(5);
27         Iterator<Integer> iterable=zm.keySet().iterator(); 
28         while(iterable.hasNext()){
29             int a=iterable.next();
30             int b=zm.get(a);
31             System.out.println("key:"+a+" "+"value:"+b);
32         }
33         System.out.println(zm.containsKey(2)+" "+zm.containsValue(103)+" "+zm.get(4));
34         
35     }
36 }
复制代码

 

贵有恒何必三更眠五更起,最无益只怕一日曝十日寒。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值