Java集合类总结

转载地址

JAVA集合类总结

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

方法如下:

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

示例图

package zf.collection.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.jws.soap.SOAPBinding;

public class Zfcollection {
    private Collection<Integer> co=new ArrayList<Integer> ();
    public Collection<Integer> getCo() {
        return co;
    }
    public void setCo(Collection<Integer> co) {
        this.co = co;
    }
    private int temp;
    private Iterator<Integer> it;
    public Zfcollection() {
        // TODO Auto-generated constructor stub
        co.add(11);
        co.add(12);
        co.add(13);
        co.add(14);
        co.add(15);    
    }
    public void print(){

        it=co.iterator();
        while(it.hasNext()){
            temp=it.next();
            System.out.print(temp+" ");
            System.out.println();
        }
    }
    public void delete(){
        co.remove(11);
        co.remove(12);
        it=co.iterator();
        while(it.hasNext()){
            temp=it.next();
            System.out.print(temp+" ");
        }
        System.out.println();
    }
    public void size(){
        System.out.println(co.size());
        co.remove(13);
        co.remove(14);
        co.remove(15);
        System.out.println(co.isEmpty());
    }
}
package Test;

import zf.collection.list.*;
import zf.collection.set.*;
import zf.map.Zfmap;


public class Main {
    public static void main(String args[]){
//        System.out.println("1.Collection接口的方法…………");
//        Zfcollection zfcollection=new Zfcollection();
//        zfcollection.print();
//        zfcollection.delete();
//        zfcollection.size();
//        System.out.println(".................................................................");
        
//        System.out.println("2.List接口的方法…………");
//        Zflist zl=new Zflist();
//        zl.getsetprint();
//        System.out.println(".................................................................");
        
//        System.out.println("3.Set接口的方法…………");
//        Zfset zs=new Zfset();
//        zs.print();
//        zs.printtreenewfangf();
//        System.out.println(".................................................................");
        
        System.out.println("4.Map接口的方法…………");
        Zfmap zm=new Zfmap();
        
    }
}

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

get(int index)
 
set(int index,Object obj)
package zf.collection.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import zf.collection.list.*;

public class Zflist {
    private Zfcollection zc=new Zfcollection();
    ArrayList<Integer> list=(ArrayList<Integer>) zc.getCo();
    public void getsetprint(){
        int a=list.get(0);
        System.out.println("在第一个节点的是:"+a);
        list.set(4, 2345);//前提是存在这个节点。
    //    Iterator<Integer>  iterator=list.iterator();
        for(int li:list){
            System.out.println(li);
        }
    }
}

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

package zf.collection.set;

import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Set;
//import java.util.Collection.*;

public class Zfset {
    
//    Set<Integer> s=new HashSet<Integer>();
    TreeSet<Integer> s=new TreeSet<Integer>();
    Iterator<Integer> it;
    int temp;
    
    public Zfset() {
        // TODO Auto-generated constructor stub
        s.add(11);
        s.add(12);    
        s.add(13);
        s.add(14);    
        s.add(15);
        s.add(16);    
        s.add(17);
        s.add(18);    
        s.add(19);
        s.add(20);    
        s.add(11);
        s.add(12);    
    }
    
    public void print(){
        it=s.iterator();
        while(it.hasNext()){
            temp=it.next();
            System.out.print(temp+" ");
            System.out.println();
        }
    }
    
    public void printtreenewfangf(){
        /**
         * 以下方法是TresSet类新增的方法。
         * 只有Tree类可以调用新方法,其父类不可。
         * */
        int begin=s.first();
        int terminal=s.last();
        Comparator cp=s.comparator();
        Set<Integer> headset=s.subSet(12, 17);//规则是:[),headSet  subSet tailSet
        
        System.out.println(begin+" "+terminal+" ");
//        for(int a:s){
//            System.out.println(a);
//        }
        System.out.println("headset:");
        for(int a:headset){
            System.out.println(a);
        }
        //System.out.println(cp.toString());
    }
}

Map

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

package zf.map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Zfmap {
    Map<Integer, Integer> zm=new HashMap<Integer, Integer>();
    public Zfmap() {
        // TODO Auto-generated constructor stub
        zm.put(1, 101);
        zm.put(2, 102);
        zm.put(3, 103);
        zm.put(4, 104);
        zm.put(5, 105);
        Set<Integer> set=zm.keySet();
        for(int a:set){
            System.out.println(a);
        }
        Collection<Integer> collection=zm.values();
        for(int b:collection){
            System.out.println(b);
        }
        zm.remove(5);
        Iterator<Integer> iterable=zm.keySet().iterator(); 
        while(iterable.hasNext()){
            int a=iterable.next();
            int b=zm.get(a);
            System.out.println("key:"+a+" "+"value:"+b);
        }
        System.out.println(zm.containsKey(2)+" "+zm.containsValue(103)+" "+zm.get(4));
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值