双列集合及collections

2022-07-27,

双列集合

1.概念:

Map集合是双列集合,是以键值对的形式存储数据的,关于键值对可以简单的理解为数学学的映射关系。

Map集合中key值不能重复的,同时,key所对应的值至多为一个。
TreeMap是基于key来进行自然排序的。

2.Map集合中常用的子类有两个:HashMap、TreeMap

  • put(Object key,Object value)

  • putAll(Map map)

  • remove(Object key)

  • remove(Object key,Object value)

  • get(Object key)

*注意: Map集合本身并没有提供可以直接遍历的形式,将Map集合转换为Collection集合后,利用Collection中提供的迭代器进行遍历

  • values() keySet() entrySet()

  • size()

package cn.yunhe.map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MapDemo {

	public static void main(String[] args) {
		orderMethod();
	}
	
	/**
	 * TreeMap是基于key来进行自然排序的
	 */
	public static void orderMethod() {
		Map map = new TreeMap();
		map.put("name", "小张");
		map.put("age", 17);
		map.put("country","China");
		System.out.println(map);
		
		Map map2 = new TreeMap(new CompareByPriceAndBrand());
		map2.put(new Item("卫龙辣条","weilong",9.9), "自定义类型1");
		map2.put(new Item("大刀肉","sanzhisongshu",12.3), "自定义类型2");
		map2.put(new Item("大盘鸡","laolang",9.9), "自定义类型3");
		System.out.println(map2);
	}
	
	/**
	 * entrySet() 将整个映射作为一个整体进行使用,返回值类型Set<Map.Entry>
	 * Map.Entry是Map集合中的一个内部接口
	 */
	public static void ergodicMethod3() {
		Map map = new HashMap();
		map.put("name", "小张");
		map.put("age", 17);
		map.put("country","China");
		Set set = map.entrySet(); 
		//迭代器中存储的整个键值对关系---Entry
		Iterator it = set.iterator();
		while(it.hasNext()) {
			Map.Entry entry = (Map.Entry)it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			System.out.println(key+":"+value);
		}
	}
	
	/**
	 * keySet() 将所有的key封装成一个Set集合进行遍历
	 * Map集合中要求key是不能重复的,而Set集合本身就是不重复的
	 */
	public static void ergodicMethod2() {
		Map map = new HashMap();
		map.put("name", "小张");
		map.put("age", 17);
		map.put("country","China");
		Set set = map.keySet();
		Iterator it = set.iterator();
		while(it.hasNext()) {
			Object key = it.next();
			Object value = map.get(key);
			System.out.println(key+"--"+value);
		}
	}
	
	/**
	 * values 以Collection的形式进行遍历
	 */
	public static void ergodicMethod() {
		Map map = new HashMap();
		map.put("name", "小张");
		map.put("age", 17);
		map.put("country","China");
		//将所有的值以Collection的单列集合形式进行存储
		Collection coll = map.values();
		Iterator it = coll.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
	}
	
	/**
	 * 获取方法
	 */
	public static void getMethod() {
		Map map = new HashMap();
		map.put("name", "小张");
		map.put("age", 17);
		map.put("country","China");
		System.out.println(map.get("age"));
	}
	
	/**
	 * 移除方法
	 */
	public static void removeMethod() {
		Map map = new HashMap();
		map.put("name", "小张");
		map.put("age", 17);
		map.put("country","China");
		System.out.println("移除前:"+map);
		//移除
		map.remove("age");
		System.out.println("移除后:"+map);
		map.remove("name","小黄");
		System.out.println("再一次移除后:"+map);
		//clear()
		map.clear();
		System.out.println("清空后的集合:"+map);
		System.out.println("集合的大小:"+map.size());
	}
	
	/**
	 * 添加方法
	 */
	public static void putMethod() {
		Map map = new HashMap();
		map.put("name", "小张");
		map.put("age", 17);
		map.put("country","China");
		//当key相同时,值会进行覆盖
		map.put("age", 19);
		//null能否作为key--可以
		map.put(null, null);
		System.out.println("map:"+map);
		//存自定义类型
		Map map2 = new HashMap();
		Item item = new Item("卫龙辣条","卫龙",9.9);
		map2.put(item, 1);
		map2.put(2, new Item("蛋黄酥","达利园",12.2));
		System.out.println("map2:"+map2);
		
		map.putAll(map2);
		System.out.println("整合后的集合:"+map);
	}
}



package cn.yunhe.map;

import java.util.Comparator;

/**
 * 按照商品的价格及商家名称进行排序
 */
public class CompareByPriceAndBrand implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		Item item1 = (Item)o1;
		Item item2 = (Item)o2;
		//先比较价格
		double temp = item1.getPrice() - item2.getPrice();
		//判断价格是佛相等,如果相等就根据商家名字进行排序
		if(temp == 0) {
			return item1.getBrand().compareTo(item2.getBrand());
		}
		return (int)temp;
	}

}


package cn.yunhe.map;

public class Item {

	private String itemName;
	
	private String brand;
	
	private double price;

	public Item(String itemName, String brand, double price) {
		super();
		this.itemName = itemName;
		this.brand = brand;
		this.price = price;
	};
	
	public Item() {}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Item [itemName=" + itemName + ", brand=" + brand + ", price=" + price + "]";
	}
	
}

3. Collections

  • Collections是针对Collection集合提供的一个工具类
  • sort(List list) 对list集合进行排序
  • set集合不重复、TreeSet是默认就有自然排序的
package cn.yunhe.coll;

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



public class CollectionsDemo {

	public static void main(String[] args) {
		compareList();
	}
	
	public static void compareList() {
		List list = new ArrayList();
		list.add("baidu.com");
		list.add("aliyun.cn");
		list.add("taobao.com");
		list.add("yunhe.cn");
		System.out.println("未排序的集合:"+list);
		//对集合进行排序
		Collections.sort(list);
		System.out.println("排序后的集合:"+list);
	}
}

本文地址:https://blog.csdn.net/qq_52232797/article/details/109823531

《双列集合及collections.doc》

下载本文的Word格式文档,以方便收藏与打印。