java—Collection

 

 

List和Set

看結果 得出 LinkedList是無序的、可重複元素

而set它已經排序好了,且不能重複

package com.binglian.collection;

import java.util.LinkedList;
import java.util.TreeSet;

public class DuplicateAndOrderTest {

	public static void main(String[] args){
		LinkedList linkedList=new LinkedList();
		linkedList.add("111");
		linkedList.add("333");
		linkedList.add("222");
		linkedList.add("444");
		linkedList.add("555");
		linkedList.add("111");
		
		System.out.println(linkedList);
		TreeSet treeSet=new TreeSet();
		treeSet.add("111");
		treeSet.add("333");
		treeSet.add("222");
		treeSet.add("444");
		treeSet.add("555");
		treeSet.add("111");
		System.out.println(treeSet);
	}
}

 

 

首先看下實現Comparable的方法

需要重寫 equals compareTo hashCode

相對較麻煩 這裏是升序、如果要自己要降序 改下1返回1 或者-1就可以

 

@Override
	public boolean equals(Object object){
		if(this == object)
			return true;
		if(!(object instanceof Customer))
			return false;
		final Customer other=(Customer) object;
	
		if(this.name.equals(other.getName()) && this.age==other.getAge())
			return true;
		else
			return false;
	}
	
	public int compareTo(Object object){
		Customer other =(Customer) object;
		
		//先按照name屬性排序
		if(this.name.compareTo(other.getName())>0)
			return 1;
		if(this.name.compareTo(other.getName())<0)
			return -1;
		
		//在按照age屬性排序
		if(this.age >other.getAge())
			return 1;
		if(this.age < other.getAge())
			return -1;
		
		return 0;
	}
	
	@Override
	public int hashCode(){
		
		int result;
		result =(name==null?0:name.hashCode());
		result= 29 * result+age;
		return result;
	}

 


   

package com.binglian.collection;

import java.util.Set;
import java.util.TreeSet;

public class Customer implements Comparable{

	private String name;
	
	private int age;
	
	public Customer(String name,int age){
		this.age=age;
		this.name=name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public boolean equals(Object object){
		if(this == object)
			return true;
		if(!(object instanceof Customer))
			return false;
		final Customer other=(Customer) object;
	
		if(this.name.equals(other.getName()) && this.age==other.getAge())
			return true;
		else
			return false;
	}
	
	public int compareTo(Object object){
		Customer other =(Customer) object;
		
		//先按照name屬性排序
		if(this.name.compareTo(other.getName())>0)
			return 1;
		if(this.name.compareTo(other.getName())<0)
			return -1;
		
		//在按照age屬性排序
		if(this.age >other.getAge())
			return 1;
		if(this.age < other.getAge())
			return -1;
		
		return 0;
	}
	
	@Override
	public int hashCode(){
		
		int result;
		result =(name==null?0:name.hashCode());
		result= 29 * result+age;
		return result;
	}

	public static void main(String[] args){
		Set<Customer> set=new TreeSet<Customer>();
		Customer customer1=new Customer("Tom", 16);
		Customer customer2=new Customer("Tom", 15);
		set.add(customer1);
		set.add(customer2);
		System.out.println(set);
		
		for(Customer c:set){
			System.out.println(c.name+" "+c.age);
		}

	}
}

 

第二種實現 Comparator

只需要重寫compare

 

這個是根據Name來排序 反正這個自己改

public int compare(Customer c1, Customer c2) {
        if(c1.getName().compareTo(c2.getName())>0)return -1;
        if(c1.getName().compareTo(c2.getName())<0)return 1;
        return 0;
    }

 

 

package com.binglian.collection;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class CustomerComparator implements Comparator<Customer>{

	
    public int compare(Customer c1, Customer c2) {
        if(c1.getName().compareTo(c2.getName())>0)return -1;
        if(c1.getName().compareTo(c2.getName())<0)return 1;
        return 0;
    }

	
	public static void main(String args[]){
		Set<Customer> set=new TreeSet<Customer>();
		
		Customer customer1=new Customer("Tom", 5);
		Customer customer2=new Customer("Tom", 9);
		Customer customer3=new Customer("Tom", 2);
		
		set.add(customer1);
		set.add(customer2);
		set.add(customer3);
		
		Iterator<Customer> iterator=set.iterator();
		
		while(iterator.hasNext()){
			Customer customer=iterator.next();
			System.out.println(customer.getName()+" "+customer.getAge());
		}
	}
	
	

}