package com.zhanglw.tips.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.zhanglw.tips.base.TestTips;

public class CollectTest<K,V>  implements TestTips{

	//Ã»ÓÐ¼Ì³ÐÈÎºÎÀà
	@SuppressWarnings("unused")
	private Map<K,V> map = new HashMap<>();
	
	//¼Ì³Ð×ÔCollection<E>  ÏßÐÔÈÝÆ÷,°´ÊýÖµË÷Òý·ÃÎÊÔªËØ
	@SuppressWarnings("unused")
	private List<K> list = new ArrayList<>();
	
	//¼Ì³Ð×ÔCollection<E>  ´æ´¢ÁãÉ¢ÇÒ²»ÖØ¸´µÄÔªËØ
	@SuppressWarnings("unused")
	private Set<V> set = new HashSet<>();
	
	/**
	 * ArrayListºÍVector ¶¼ÊÇÊ¹ÓÃÊý×é·½Ê½´æ´¢Êý¾Ý
	 * ArrayList ²éÑ¯¿ì£¬²åÈëÉ¾³ýÂý
	 * Vector ÖÐµÄ·½·¨Ìí¼ÓÁËsynchronized ÐÞÊÎ,ÐÔÄÜ½Ï²î,ÊÇÒÅÁôÈÝÆ÷
	 * LinkedList Ê¹ÓÃÁËË«ÏòÁ´±íÊµÏÖ´æ´¢,±ÈÊý×é´æ´¢·½·¨Ïà±È,ÀûÓÃÂÊ¸ü¸ß,²åÈëÊ±,Ö»ÐèÒª¼ÇÂ¼±¾ÏîµÄÇ°ºóÏî¼´¿É,¹Ê²åÈë¸ü¿ì
	 * JavaÒÅÁôÈÝÆ÷ÓÐ: Vector,Hashtable,Dictionary,BitSet,Stack,Properties £¬²»ÍÆ¼öÊ¹ÓÃ
	 * ArrayListºÍLinkedList ÊÇ·ÇÏß³Ì°²È«µÄ, ÔÚ¶àÏß³ÌÇé¿öÏÂ,¿ÉÒÔÍ¨¹ýCollectionsµÄsynchronizedList·½·¨×ª»»
	 */
	private LinkedList<String> linkedList = new LinkedList<>();
	
	@Override
	public void test(){
		Collections.synchronizedList(linkedList).add("AB");//Ê¹ÓÃÁË×°ÊÎÄ£Ê½
		
		Set<Pers> newSet = new HashSet<>();
		Pers p1 = new Pers();
		p1.setName("AB");
		p1.setAge(9);
		
		Pers p2 = new Pers();
		p2.setName("AB");
		p2.setAge(10);
		newSet.add(p1);
		newSet.add(p2);
		System.out.println(p2.equals(p1));
		System.out.println(p1.hashCode());
		System.out.println(p2.hashCode());
		System.out.println(newSet.size());
		for (Pers pers : newSet) {
			System.out.println(pers.getName());
		}
	}
	
	/**
	 * Collection ÊÇList,SetµÈµÄ¸¸Àà½Ó¿Ú,CollectionsÊÇ¼¯ºÏ¹¤¾ßÀà,¸ºÔðËÑË÷ÅÅÐò,Ïß³Ì°²È«»¯µÈ¹¦ÄÜ
	 * List ÒÔË÷ÒýÈ¡Öµ,ÔªËØ¿ÉÒÔÖØ¸´,  Set ÔªËØ²»¿ÉÒÔÖØ¸´(ÓÃ¶ÔÏóµÄequal·½·¨Çø·ÖÊÇ·ñÖØ¸´,Í¬Ê±hasCodeÒ²ÒªÏàÍ¬)
	 * Map ÊÇ¼üÖµÒ»¶ÔÒ»,keyÊÇ²»ÖØ¸´µÄ,Value¿ÉÒÔÖØ¸´,(Á½ÖÖÊµÏÖ°æ±¾:¹þÏ£´æ´¢ºÍÅÅÐòÊ÷)
	 */
	public static void main(String[] args) {
		CollectTest<String,String> text = new CollectTest<String, String>();
//		text.test();
		text.test1();
	}
	
	/**
	 * TreeMap
	 */
	public void test1(){
		Set<Student> stuSet = new TreeSet<>();//Java 7 ×êÊ¯Óï·¨(¹¹ÔìÆ÷ºóÃæµÄ¼âÀ¨ºÅ²»ÐèÒªÐ´ÀàÐÍ)
		stuSet.add(new Student("Hao LUO", 33));
		stuSet.add(new Student("XJ WANG", 32));
		stuSet.add(new Student("Bruce LEE", 60));
		stuSet.add(new Student("Bob YANG", 22));
		
//		Collections.sort(stuSet);
		for (Student student : stuSet) {
			System.out.println(student);
		}
		
		List<Student> list = new ArrayList<>();     // Java 7µÄ×êÊ¯Óï·¨(¹¹ÔìÆ÷ºóÃæµÄ¼âÀ¨ºÅÖÐ²»ÐèÒªÐ´ÀàÐÍ)
        list.add(new Student("Hao LUO", 33));
        list.add(new Student("XJ WANG", 32));
        list.add(new Student("Bruce LEE", 60));
        list.add(new Student("Bob YANG", 22));
        
        Collections.sort(list, new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});
        
        for (Student student : list) {
			System.out.println(student);
		}
	}
}

class Pers{

	private String name;
	
	private int age;
	
	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 obj) {
		if(obj!=null&&(obj instanceof Pers)){
			if(((Pers)obj).name.equals(this.name)){
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return 31;
	}
}


class Student implements Comparable<Student>{

	private String name;
	
	private int age;
	
	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;
	}

	
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	@Override
	public int compareTo(Student o) {
		return this.age-o.age;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	
}











 