java Map List转化,通过Map保存数据,通过List排序。取前三名

java  Map   List转化,通过Map保存数据,通过List排序。取前三名

package yo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class a {
	public static void main(String[] args) {
		Map map = new HashMap<>();
		String key;
		Float value;
		key = "张三";
		value = 29.5f;
		map.put(key, value);

		key = "张四";
		value = 19.5f;
		map.put(key, value);

		key = "张五";
		value = 90.5f;
		map.put(key, value);

		key = "张六";
		value = 91.5f;
		map.put(key, value);

		key = "张七";
		value = 94.5f;
		map.put(key, value);

		key = "张八";
		value = 93.5f;
		map.put(key, value);

		key = "张九";
		value = 92.5f;
		map.put(key, value);

		System.out.println(map);

		List> list = new ArrayList<>(map.entrySet());
		Collections.sort(list, Map.Entry.comparingByValue());

		System.out.println("Top 3:");
		for (int i = 0; i < 3; i++) {
			int len = list.size();
			Map.Entry s = list.get(len - i - 1);
			System.out.println(s.getKey() + ":" + s.getValue());
		}

	}
}

你可能感兴趣的:(java,开发语言)