JAVA SE 8 的流库-1.9收集到映射表中

1.9 收集到映射表中

Collectors.toMap方法有两个函数引元,他们用来产生映射表的键和值。

Map<Integer,String> idToName = people().collect(
			    Collectors.toMap(Person::getId, Person::getName));

通常情况下,值应该是实际的元素,因此第二个函数可以使用Function.identity()

java.util.function.Function是函数式接口,它的特点是有且只有一个抽象方法。
对于函数式接口,形如这样的表达式(x -> x + 1)能够实例化一个对象。
并且该表达式正是那个唯一的抽象方法的实现。(没有完全弄懂,留个?)
Map<Integer, Person> idToPerson = people().collect(
				Collectors.toMap(Person::getId, Function.identity()));

如果多个元素映射同一个键,那么就会存在冲突,收集器将会抛出一个IllegalStateException对象,可以提供第三个引元来覆盖这种行为。

Map<String,String> languageNames = locales.collect(
				Collectors.toMap(Locale::getDisplayLanguage, 
				l->l.getDisplayLanguage(),
				(existingValue,newValue)->existingValue));

如果一个键对应多个值时,需要一个Map<String,<Set < String > >。将已有集和新集做并操作。

Map<String,Set<String>> countryLanguageSets = locales.collect(
				Collectors.toMap(Locale::getDisplayCountry, 
				l->Collections.singleton(l.getDisplayLanguage()),
				(a,b)->{//union of a and b 
					Set<String> union = new HashSet<>(a);
					union.addAll(b);
					return union;
				}));

如果想要得到TreeMap,可以将构造器作为第四个引元来提供,需要提供一种合并函数。

idToPerson = people().collect(
				Collectors.toMap(Person::getId, Function.identity(),(
						existingValue,newValue)->{throw new IllegalStateException();//????
						},TreeMap::new));
package JavaSE8的流库;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectingIntoMaps {

	public static class Person//静态类
	{
		private int id;
		private String name;
		
		public Person(int id, String name)
		{
			this.id=id;
			this.name=name;
		}
		
		public int getId()
		{
			return id;
		}
		
		public String getName()
		{
			return name;
		}
		
		public String toString()
		{
			return getClass().getName()+"[id="+id+",name="+name+"]";
		}
	}
	
	public static Stream<Person> people()
	{
		return Stream.of(new Person(1001,"Peter"), 
				new Person(1002,"poul"), new Person(1003,"Mary"));
	}
	
	public static void main(String[] args)throws IOException {
		// TODO 自动生成的方法存根

		Map<Integer,String> idToName = people().collect(
				Collectors.toMap(Person::getId, Person::getName));
		System.out.println("idToName:"+idToName);
		
		Map<Integer, Person> idToPerson = people().collect(
				Collectors.toMap(Person::getId, Function.identity()));
		System.out.println("idToPerson:"+idToPerson.getClass().
				getName()+idToPerson);
		
		idToPerson = people().collect(
				Collectors.toMap(Person::getId, Function.identity(),(
						existingValue,newValue)->{throw new IllegalStateException();//????
						},TreeMap::new));
		System.out.println("idToPerson:"+idToPerson.getClass().
				getName()+idToPerson);
		
		Stream<Locale> locales = Stream.of(Locale.getAvailableLocales());
		Map<String,String> languageNames = locales.collect(
				Collectors.toMap(Locale::getDisplayLanguage, 
				l->l.getDisplayLanguage(),
				(existingValue,newValue)->existingValue));
		System.out.println("languageNames:"+languageNames);
		
		locales = Stream.of(Locale.getAvailableLocales());
		Map<String,Set<String>> countryLanguageSets = locales.collect(
				Collectors.toMap(Locale::getDisplayCountry, 
				l->Collections.singleton(l.getDisplayLanguage()),
				(a,b)->{//union of a and b 
					Set<String> union = new HashSet<>(a);
					union.addAll(b);
					return union;
				}));
		System.out.println("countryLanguageSets:"+countryLanguageSets);
	}

}

输出结果:
在这里插入图片描述
(如有问题,请评论!Thanks♪(・ω・)ノ)

猜你喜欢

转载自blog.csdn.net/z036548/article/details/84189210