初识JAVA---集合接口 线性表、栈、队列、HashSet、TreeSet(有例程)(8)

Collection 接口

他有  add(element:Object):boolean   

          remove(element:Object):boolean

          size();int

          isEmpty();boolean

          contains(element:Object):boolean

          iterator():Iterator

List 线性表   主要实现的类是ArrayListLinkdList    以及早期的Vector

import java.util.*;

class TestList
{
    public static void main(String[] args) 
    {
        List<Photo> album=new LinkedList<>();//<>里面是什么 表示它就是什么的线性表
        //尖括号这种表示较泛型
        album.add(new Photo("one",new Date(),"classroom"));
        album.add(new Photo("two",new Date(),"library"));
        album.add(new Photo("three",new Date(),"gym"));
        album.add(new Photo("three",new Date(),"dorm"));
        
        Iterator<Photo> iterator=album.iterator();
        while(iterator.hasNext()) 
        {
            Photo photo=iterator.next();
            System.out.println(photo.toString());
        }
        
        for(Photo photo:album) 
        {
            System.out.println(photo);
        }
    }


}
class Photo
{
    String title;
    Date date;
    String memo;
    Photo(String title,Date date,String memo)
    {
        this.title=title;
        this.date=date;
        this.memo=memo;
    }
    @Override
    public String toString() 
    {
        return title+"("+date+")"+memo;
    }
}
输出

one(Fri Dec 20 21:10:35 CST 2019)classroom
two(Fri Dec 20 21:10:35 CST 2019)library
three(Fri Dec 20 21:10:35 CST 2019)gym
three(Fri Dec 20 21:10:35 CST 2019)dorm
one(Fri Dec 20 21:10:35 CST 2019)classroom
two(Fri Dec 20 21:10:35 CST 2019)library
three(Fri Dec 20 21:10:35 CST 2019)gym
three(Fri Dec 20 21:10:35 CST 2019)dorm

栈  通过下面程序可知道  这种数据结构后进先出

import java.util.*;
public class TestStack{
    static String[] months= {
            "January","Fabruary","March","April",
            "May","June","July","August","September",
            "October","November","December"};
    public static void main(String[] args) {
        Stack<String> stk=new Stack<>();
        for(int i=0;i<months.length;i++)
            stk.push(months[i]+" ");
        System.out.println("stk = "+stk);
        System.out.println("popping elements: ");
        while(!stk.empty())
            System.out.println(stk.pop());
    }
}

队列Queue  重要的线性数据结构   先进先出  FIFO 

import java.util.*;
class TestQueue{
    public static void main(String[] args) {
        Queue<Integer> q=new LinkedList<>();
        for(int i=0;i<5;i++) {
            q.offer(i);//offer入队的意思   
            //可以智能的把整形转换成Integer
        }
        while(!q.isEmpty()) 
            System.out.println(q.poll());
    }
}

输出结果可见是先进先出

几个早期的类或接口

Vector 现在多用ArrayList

Stack 现在多用LinkedList

Hashtable 现在多用HashMap

Enumeration   现在多用Iterator

Set  两个重要的实现 HashSet TreeSet   TreeSet底层是用TreeMap来实现的

Set中对象不重复    hashCode()不等  

package equals;
import java.util.*;
class TestSet
{
	
	public static void main(String[] args) {
		Set<String>set=new HashSet<String>();//建立一个哈希表
		//Set<String> set=new TreeSet<String>();
		set.add("Brazil");
		set.add("Russia");
		set.add("India");
		set.add("China");
		set.add("South Africa");
		
		System.out.println(set.contains("China"));
		
		for(String obj:set)
			System.out.println(obj);//最后输出的时候,会发现和存储的顺序不一样
	}
}

Map类  HashMap类    TreeMap类:用红黑树的算法

import java.util.*;
class TestMap
{
	public static void main(String[] args) {
		//Map<String,String> map=new HashMap<String,String>();
		Map<String,String> map=new TreeMap<String,String>();
		map.put("b","Brazil");
		map.put("r","Russia");
		map.put("i","India");
		map.put("c","China");
		map.put("k","South Africa");
		System.out.println(map.get("c"));
		//先输出 c对应的键值
		
		for(String key:map.keySet())
			System.out.println(key+":"+map.get(key));
		//第一种遍历方法
		
		
		for(String value:map.values())
			System.out.println(value);
		//只输出键值
		
		for(Map.Entry<String, String>entry:map.entrySet())
			System.out.println(entry.getKey()+":"+entry.getValue());
		//第二种遍历方法  推荐  尤其是容量大的时候
		
		
		Iterator it=map.entrySet().iterator();
		while(it.hasNext()) {
			Map.Entry<String, String> entry=(Map.Entry<String, String>)it.next();
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
		//第三种遍历方法 
		//先构造迭代器   然后用迭代器遍历
	}
}
发布了103 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39653453/article/details/103638314