Java 学习排序

package com.src.hero;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
/**
 * @author Hero
 * 2012-5-18 下午05:21:07
 * java 简单的排序使用练习
 */
public class CollectionMy {

	/**
	 * @author Hero 2012-5-18 下午02:10:10
	 * @param args
	 * HashSet不保存重复值;
	 */
	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	private void Hashsetdeal(){
		Collection mycoll = new HashSet();
		mycoll.add("中国");
		mycoll.add("日本");
		mycoll.add("美国");
		mycoll.add("中国");
		mycoll.add("美国");
		System.out.println("--------------------------HashSet");
		for (Iterator it = mycoll.iterator(); it.hasNext();) {
			System.out.println(it.next().toString());
		}


	
	}
	/**
	 * @author Hero
	 * 2012-5-18 下午05:20:41
	 * 保存重复值
	 */
	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	private void arrayList(){
		Collection mylist = new ArrayList();
		mylist.add("中国");
		mylist.add("日本");
		mylist.add("美国");
		mylist.add("中国");
		mylist.add("美国");
		System.out.println("--------------------------ArrayList");
		for (Iterator it = mylist.iterator(); it.hasNext();) {
			System.out.println(it.next().toString());
		}
	}
	
	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	private void treeSet(){
		Person person1 = new Person(20);
		Person person2 = new Person(30);
		Person person3 = new Person(25);

		TreeSet tree2 = new TreeSet(new Mycoprate());
		tree2.add(person1);
		tree2.add(person2);
		tree2.add(person3);
		System.out.println("--------------------------TreeSet");
		for (Iterator it = tree2.iterator(); it.hasNext();) {
			Person person = (Person) it.next();
			System.out.println(person.getScore());
		}
	}
    /**
     * list排序通过重写Comparator方法
     */
	private void deal() {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < 3; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			switch (i) {
			case 0:
				map.put("name", "zhangsan");
				map.put("age", 20);
				break;
			case 1:
				map.put("name", "lisi");
				map.put("age", 30);
				break;
			case 2:
				map.put("name", "wangwu");
				map.put("age", 25);
				break;
			default:
				break;
			}
			data.add(map);
		}

		Comparator<Map<String, Object>> agesort = new MycoprateList();
		Collections.sort(data, agesort);
		System.out.println("--------------------------List");
		for (int i = 0; i < data.size(); i++) {
			System.out.println(data.get(i));
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		CollectionMy coll = new CollectionMy();
		coll.Hashsetdeal();
		coll.arrayList();
		coll.treeSet();
		coll.deal();
	


	}

	class MycoprateList implements Comparator<Map<String, Object>> {

		@Override
		public int compare(Map<String, Object> o1, Map<String, Object> o2) {
			// TODO Auto-generated method stub
			return ((Integer) o1.get("age")).compareTo((Integer) o2.get("age"));
		}
	}
	
	class Person {

		private int score;

		public Person(int s) {
			this.score = s;
		}

		public int getScore() {
			return score;
		}

		public void setScore(int score) {
			this.score = score;
		}

		@Override
		public String toString() {
			return "Person [score=" + score + "]";
		}

	}
	
	class Mycoprate implements Comparator {

		@Override
		public int compare(Object o1, Object o2) {
			// TODO Auto-generated method stub
			Person a = (Person) o1;
			Person b = (Person) o2;
			return ((Integer) a.getScore()).compareTo((Integer) b.getScore());
		}

	}
	//--------------------------HashSet
	//日本
	//美国
	//中国
	//--------------------------ArrayList
	//中国
	//日本
	//中国
	//美国
	//--------------------------TreeSet
	//20
	//25
	//30
	//--------------------------List
	//{age=20, name=zhangsan}
	//{age=25, name=wangwu}
	//{age=30, name=lisi}

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值