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}
}
Java 学习排序
最新推荐文章于 2024-07-11 16:03:52 发布