写这个是为了自己复习一下java集合类,对初学者也有帮助哈。当然还有些集合没写到,见谅各位
。
首先我们先写一个苹果类,定义一些参数(其实对象的比较可以在这个类实现Comparable接口从而重写compareTo方法,我这是直接写在Collections.sort里面了,大家可以试一下这个方法。)
package HashMapSort;
public class Apple {
float weight;//重量
int size;//大小
String color;//颜色
public Apple(float weight,int size,String color){
this.weight=weight;
this.size=size;
this.color=color;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
下面就是具体比较操作类了,为了能复习集合,我写的啰嗦了点^_^。package HashMapSort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/**
* HashMap对象排序 实现所有集合复习
* @author Administrator
*
*/
public class Demo1 {
public static void main (String args[]){
HashMap<Integer,Object> appMap =new HashMap<Integer,Object>();
appMap.put(68, new Apple(2.1f,5,"red"));
appMap.put(38, new Apple(2.2f,7,"green"));
appMap.put(27, new Apple(2.0f,9,"blue"));
appMap.put(5, new Apple(2.4f,2,"red"));
appMap.put(4, new Apple(2.5f,7,"red"));
appMap.put(1, new Apple(1.6f,8,"red"));
anKey(appMap);
anValue(appMap);
}
//按照key排序 默认hashcode码排序
public static void anKey(HashMap<Integer,Object> appMap){
System.out.println("key排序前+++++++++++++++++");
checkHashMap(appMap);
System.out.println("key排序后+++++++++++++++++");
List<Integer> appKeyList = new ArrayList<Integer>(appMap.keySet());
//默认升序
System.out.println("排序方法1Collections.sort升序================");
Collections.sort(appKeyList);
for(Integer i:appKeyList){
Apple a= (Apple) appMap.get(i);
System.out.println(i+"---"+a.getWeight()+"|"+a.getSize()+"|"+a.getColor());
}
System.out.println("排序方法1Collections.sort降序================");
Collections.sort(appKeyList, new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
if(o1>o2)return -1;//-1意味o1逻辑上<o2
else if(o1<o2)return 1;//1意味o1逻辑上>o2
else return 0;
}
});
for(Integer i:appKeyList){
Apple a= (Apple) appMap.get(i);
System.out.println(i+"---"+a.getWeight()+"|"+a.getSize()+"|"+a.getColor());
}
System.out.println("排序方法2TreeMap有序直接调用================");
TreeMap<Integer, Object> tree =new TreeMap<Integer, Object>(appMap);
checkHashMap(tree);
}
//按照value排序
public static void anValue(HashMap<Integer,Object> appMap){
System.out.println("value按苹果质量排序前+++++++++++++++++");
checkHashMap(appMap);
System.out.println("value按苹果质量排序后+++++++++++++++++");
List<Entry<Integer,Object>> appValueList =new ArrayList<Entry<Integer,Object>>();
appValueList.addAll(appMap.entrySet());
Collections.sort(appValueList, new Comparator<Entry<Integer,Object>>(){
@Override
public int compare(Entry<Integer, Object> o1,
Entry<Integer, Object> o2) {
Apple o1A= (Apple)o1.getValue();
Apple o2A= (Apple)o2.getValue();
if(o1A.getWeight()<o2A.getWeight())return -1;
else if(o1A.getWeight()>o2A.getWeight())return 1;
else return 0;
}
});
for(Entry<Integer,Object> i:appValueList){
Apple a= (Apple)i.getValue();
System.out.println(i.getKey()+"---"+a.getWeight()+"|"+a.getSize()+"|"+a.getColor());
}
}
//遍历Map
public static void checkHashMap(Map<Integer,Object> appMap){
System.out.println("遍历map方法1=key迭代器================");
Iterator<Integer> it = appMap.keySet().iterator();
while(it.hasNext()){
Integer itt=it.next();
Apple a =(Apple)appMap.get(itt);
System.out.println(itt+"---"+a.getWeight()+"|"+a.getSize()+"|"+a.getColor());
}
System.out.println("遍历map方法2=entry键值对===============");
Set<Entry<Integer, Object>> set =appMap.entrySet();
for(Map.Entry<Integer, Object> entry:set){
Apple a =(Apple) entry.getValue();
System.out.println(entry.getKey()+"---"+a.getWeight()+"|"+a.getSize()+"|"+a.getColor());
}
}
}
当然我们HASHMAP可以定义为<object,integer>这种,大家自己可以去试一下。写的不好的地方,大家见谅