利用HASHMAP对苹果称重

本文旨在通过实例讲解Java集合类的应用,包括HashMap对象的排序、遍历等操作,覆盖了从基本概念到具体实现的全过程,适合初学者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写这个是为了自己复习一下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>这种,大家自己可以去试一下。写的不好的地方,大家见谅




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值