容器

如今作用是实现算法不必写一堆数据结构,直接调用,确实简单非常多。

经常使用容器层次结构:

容器

一个小样例实现上面所说容器:

import java.util.*;
import java.io.*;
public class test {
	public static void main(String[] args){
	/*	//IO 输入
		try{
			BufferedReader buf;
			buf=new BufferedReader(new InputStreamReader(System.in));
			String str=buf.readLine();
			System.out.println("IO输入 输出结果:");
			System.out.print(str);
		}catch(Exception ex){
			System.out.println(ex.toString());
		}*/
		
		//Iterator 遍历练习 
		iterator it=new iterator();
		it.play();
		it.show();
		
		
		//Collection 容器
		Collect col=new Collect();
		col.play();
		
		//LinkedList 容器
		linkedlist link=new linkedlist();
		link.display();
		
		//arraylist 容器
		arraylist array=new arraylist();
		array.display();
		
		//treeset 容器
		treeset trset=new treeset();
		trset.display();
		
		//hashs 容器
		hashset hashs=new hashset();
		hashs.display();
		
		//sortedset 容器
		sortedset sortset=new sortedset();
		sortset.display();
		
		//map 容器
		map m=new map();
		m.display();
		
		//treeset 实现自己主动排序  要实现Comparable接口
		Set<Student> treeset=new TreeSet<Student>();
		treeset.add(new Student("小王",0));
		treeset.add(new Student("小li",1));
		treeset.add(new Student("小mei",5));
		treeset.add(new Student("小bu",3));
		treeset.add(new Student("xiaodui",9));
		treeset.add(new Student("小ge",6));
		System.out.println("\n输出结果:"+treeset);
	}
	

}
//Iterator 遍历练习 
class iterator{
	//Iterator  仅仅能从后到前
	public void play(){
		System.out.println("\n"+ "Iterator 输出结果:");
		List list=new LinkedList();
		list.add("zhangsan");
		list.add("lisi");
		list.add("wangwu");
		Iterator it=list.iterator();
		while(it.hasNext()){
			String str=(String)it.next();
			if("lisi".equals(str)){
				it.remove();
			}
			//System.out.println("\n "+ list);
			System.out.print("  "+str);
			
			
		}
	}
	//ListIterator  两个方向遍历向上和向下
	public void show(){
		System.out.println("\n"+"ListIterator 输出结果:");
		List<String> list=new LinkedList<String>();
		list.add("zhangsan");
		list.add("lisi");
		list.add("wangwu");
		ListIterator it=list.listIterator();
		//Iterator it=list.iterator();
		while(it.hasNext()){
			String str=(String)it.next();
			if("lisi".equals(str)){
				it.remove();
			}
			//String str=(String)it.next();
		}
		if(it.hasPrevious()==false){
			System.out.println("前面没有内容");
		}
		while(it.hasPrevious()){
			String str1=(String)it.previous();
			System.out.print(str1 + "  ");
			
		}
	}
}
	//Collection 容器
	class Collect{
		public void play(){
			Collection<Integer> c1=new LinkedList<Integer>();
			Collection<Integer> c2=new LinkedList<Integer>();
			System.out.println("\n"+"Collection 输出结果:");
			for(int i=0;i<5;i++){
				c1.add(i);
			}
			System.out.print(c1);
		}
		
	}
	//LinkedList 容器
	class linkedlist{
		public void display(){
			System.out.println("\n\n"+"LinkedList 输出结果:");
			LinkedList list;
			list =new LinkedList();
			list.add("张三");
			list.add("李四");
			System.out.println("原来大小:"+list.size());
			System.out.println("是否包括元素张三"+list.contains("张三"));
			System.out.println("输出各个元素:");
			for(int i=0;i<list.size();i++){
				
				System.out.print(list.get(i)+" ");
				//System.out.println(list.remove(i));
				//list.remove(i);
			}
			System.out.println("删除第一个元素"+list.remove(1));
			System.out.print("输出如今元素个数"+list.size());
		}
		
	}
	//ArrayList 容器
	class arraylist{
		void display(){
			System.out.println("\n\n"+"ArrayList 输出结果:");
			ArrayList list;
			list=new ArrayList();
			list.add("你好");
			list.add("张三");
			list.add(new Integer(10));
			System.out.println("原来大小:" + list.size());
			list.add("你好");
			System.out.println("如今大小:" + list.size());
			System.out.println("内容:" + list);
		}
	}
	
	//TreeSet 容器
	class treeset{
		public void display(){
			System.out.println("\n\n"+"TreeSet 输出结果:");
			Set s=new TreeSet();
			s.add(2);
			s.add(1);
			s.add(1);
			
			System.out.println("输出各个元素:"+s);
			System.out.print("set大小:"+s.size());	
		}
	}
	
	//hashset 容器
	class hashset{
		public void display(){
			Set s=new HashSet();
			s.add(1);
			s.add(1);
			s.add(2);
			System.out.println("\n\n"+"HashSet 输出结果:");
			System.out.println("输出结果是:"+s);
			System.out.println("hashset大小是:"+s.size());
		}
	}
	
	//SortedSet 容器
	class sortedset{
		public void display(){
			SortedSet<Integer> sortset=new TreeSet<Integer>();
			System.out.println("\n\n"+"SortedSet 输出结果:");
			for(int i=1;i<7;i++){
				sortset.add(i);
			}
			System.out.println("输出第一个元素是:"+sortset.first());
			System.out.println("输出最后一个元素是:"+sortset.last());
		}
	}
	
	class map{
		public void display(){
			System.out.println("\n\n"+"HashMap 输出结果:");
			Map m = new HashMap();
			m.put(1, new String("11"));
			m.put("two", new Integer(2));
			m.put(1, new String("33"));
			m.put(3, new String("44"));
			System.out.println("元素个数是:"+m.size());
			System.out.println("获得键值是1的元素:"+m.get(1));
			System.out.println("是否包括键值1:"+m.containsKey(1));
			System.out.println("是否包括元素11:"+m.containsValue("11"));
			m.remove(1);
			System.out.println("删除一个元素后个数:"+m.size());
			if(m.containsKey("two")){
				int i = ((Integer)(m.get("two"))).intValue();
				System.out.println("将字符窜转化为int:"+i);
			}
		
		}
	}
	class Student implements Comparable<Student> {
		private String name;
		private int num;
		public Student(String name,int num){
			this.name=name;
			this.num=num;
		}
		public String toString(){
			return "name :"+ this.name +";number :"+ this.num+"\n";
		}
		public int compareTo(Student stu){
			if(this.num>stu.num){
				return 1;
			}else if(this.num<stu.num){
				return -1;
			}else{
				return 0;
			}
		}

	}




上一篇:TreeSet详解


下一篇:Redis - 秒杀功能:不带锁的,只是个基础功能,后续学习带锁的秒杀