集合类的常见用法

可以用集合类实现栈、队列等常见需求

package cn.ibeans.test;

import java.util.LinkedList;
/**
 * 本示例程序通过LinkedList集合类现实栈,即体现后进先出的思想
 * @author llf
 *
 */
public class MyStack {
	LinkedList ll = new LinkedList();
	
	/**
	 * 入栈
	 * @param o
	 */
	public void push(Object o){
		ll.addFirst(o);
	}
	
	/**
	 * 出栈
	 * @return
	 */
	public Object pop(){
		return ll.removeFirst();
	}
	
	/**
	 * 检查栈内是否为空
	 * @return
	 */
	public boolean isEmpty(){
		return ll.isEmpty();
	}
	
	public static void main(String[] args) {
		MyStack stack = new MyStack();
		stack.push("one");
		stack.push("two");
		stack.push("three");
		
		while(!stack.isEmpty()){
			System.out.println(stack.pop());			
		}
	}
}



队列:
package cn.ibeans.test;

import java.util.LinkedList;
import java.util.List;
/**
 * 本示例程序实现先进先出的队列思想
 * @author llf
 *
 */
public class MyQueue {
	private LinkedList ll = new LinkedList();
	
	/**
	 * 入列
	 * @param o
	 */
	public void put(Object o){
		ll.addLast(o);
	}
	
	/**
	 * 出列
	 * @return
	 */
	public Object get(){
		return ll.removeFirst();
	}
	
	public boolean isEmpty(){
		return ll.isEmpty();
	}
	
	public static void main(String[] args) {
		MyQueue queue = new MyQueue();
		queue.put("one");
		queue.put("two");
		queue.put("three");
		
		while(!queue.isEmpty()){
			System.out.println(queue.get());
		}
	}
}


hashset:
package cn.ibeans.test;

import java.util.HashSet;
import java.util.Iterator;

/**
 * 本示例程序通过hashset实现不重复的set集合
 * 为此,集合中的每个元素应该实现hashCode()和equals()方法
 * @author llf
 *
 */
public class MyHashSet {
	
	public static void main(String[] args) {
		HashSet hs = new HashSet();
		hs.add(new Student("zhangsan",1));
		hs.add(new Student("lishi",2));
		hs.add(new Student("zhangsan",1));
		hs.add(new Student("wangwu",3));
		
		
		Iterator it = hs.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
	
}


class Student{
	int age;
	String name;
	Student(String name,int age){
		this.age=age;
		this.name = name;
	}
	
	public int hashCode(){
		return age*name.hashCode();
	}
	
	public boolean equals(Object o){
		Student s = (Student)o;
		return s.age==age && s.name.equals(name);
	}
	
	public String toString(){
		return "name : "+age;
	}
}


treeset:
package cn.ibeans.test;

import java.util.Iterator;
import java.util.TreeSet;

/**
 * 通过treeset实现一个排序的集合对象
 * 其内每个元素是实现comparable接口,或是用一个比较器来构造treeset对象
 * @author llf
 *
 */
public class MyTreeSet {
	public static void main(String[] args) {
		TreeSet hs = new TreeSet();
		hs.add(new Man("zhangsan",1));
		hs.add(new Man("lishi",2));
		hs.add(new Man("zhangsan",1));
		hs.add(new Man("wangwu",3));
		hs.add(new Man("wangwu",0));
		
		
		Iterator it = hs.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
}

class Man implements Comparable{
	int age;
	String name;
	Man(String name,int age){
		this.age=age;
		this.name = name;
	}
	
	public int compareTo(Object o){
		Man s = (Man)o;
		int result = age>s.age ? 1:(age==s.age?0:-1);
		if(result==0){
			result = name.compareTo(s.name);
		}
		return result;
	}
	
	public int hashCode(){
		return age*name.hashCode();
	}
	
	public boolean equals(Object o){
		Student s = (Student)o;
		return s.age==age && s.name.equals(name);
	}
	
	public String toString(){
		return name +" : "+age;
	}
}

你可能感兴趣的:(java)