Java 8列表<;V>;进入地图<;K、 V>;
我想使用Java8的streams和lambdas将对象列表转换为映射 这就是我用Java7和更低版本编写它的方式Java 8列表<;V>;进入地图<;K、 V>;,java,lambda,java-8,java-stream,Java,Lambda,Java 8,Java Stream,我想使用Java8的streams和lambdas将对象列表转换为映射 这就是我用Java7和更低版本编写它的方式 private Map<String, Choice> nameMap(List<Choice> choices) { final Map<String, Choice> hashMap = new HashMap<>(); for (final Choice choice : choices) {
private Map<String, Choice> nameMap(List<Choice> choices) {
final Map<String, Choice> hashMap = new HashMap<>();
for (final Choice choice : choices) {
hashMap.put(choice.getName(), choice);
}
return hashMap;
}
私有映射名称映射(列表选项){
final Map hashMap=新hashMap();
for(最终选择:选择){
put(choice.getName(),choice);
}
返回hashMap;
}
我可以使用Java8和番石榴轻松完成这项工作,但我想知道在没有番石榴的情况下如何做到这一点
番石榴:
private Map<String, Choice> nameMap(List<Choice> choices) {
return Maps.uniqueIndex(choices, new Function<Choice, String>() {
@Override
public String apply(final Choice input) {
return input.getName();
}
});
}
私有映射名称映射(列表选项){
返回Maps.uniqueIndex(选项,新函数(){
@凌驾
公共字符串应用(最终选择输入){
返回input.getName();
}
});
}
番石榴配爪哇8兰姆达斯
private Map<String, Choice> nameMap(List<Choice> choices) {
return Maps.uniqueIndex(choices, Choice::getName);
}
私有映射名称映射(列表选项){
返回Maps.uniqueIndex(选项,选项::getName);
}
基于此,它非常简单:
Map<String, Choice> result =
choices.stream().collect(Collectors.toMap(Choice::getName,
Function.identity()));
映射结果=
choices.stream().collect(Collectors.toMap)(Choice::getName,
Function.identity());
如果您的密钥不能保证列表中所有元素的唯一性,则应将其转换为映射
,而不是映射
映射结果=
choices.stream().collect(Collectors.groupingBy(Choice::getName));
使用getName()
作为键,选择本身作为映射的值:
Map<String, Choice> result =
choices.stream().collect(Collectors.toMap(Choice::getName, c -> c));
映射结果=
choices.stream().collect(Collectors.toMap(Choice::getName,c->c));
我使用这种语法
Map<Integer, List<Choice>> choiceMap =
choices.stream().collect(Collectors.groupingBy(choice -> choice.getName()));
Map choiceMap=
choices.stream().collect(Collectors.groupingBy(choice->choice.getName());
我试着这么做,发现使用上面的答案,当使用函数.identity()
作为映射的键时,由于键入问题,我无法使用像this::localMethodName
这样的本地方法来实际工作
Functions.identity()
实际上对本例中的键入做了一些操作,因此该方法只能通过返回Object
并接受Object
的参数来工作
为了解决这个问题,我放弃了Functions.identity()
,改为使用s->s
因此,我的代码,在我的示例中,列出目录中的所有目录,并为每个目录使用目录名作为映射的键,然后使用目录名调用方法并返回项目集合,如下所示:
Map<String, Collection<ItemType>> items = Arrays.stream(itemFilesDir.listFiles(File::isDirectory))
.map(File::getName)
.collect(Collectors.toMap(s->s, this::retrieveBrandItems));
Map items=Arrays.stream(itemFilesDir.listFiles(File::isDirectory))
.map(文件::getName)
.collect(Collectors.toMap(s->s,this::retrieveBrandItems));
Map collect=Arrays.asList(Locale.getavailablecales()).stream().collect(收集器
.toMap(l->l.getDisplayCountry(),l->Collections.singleton(l.getDisplayLanguage());
如果您不介意使用第三方库,AOL的lib(我是贡献者)具有所有类型的扩展,包括和
ListX选项;
Map Map=choices.toMap(c->c.getName(),c->c);
如果您不想使用收集器,这里还有一个
映射结果=
choices.stream().collect(HashMap::new,
(m,c)->m.put(c.getName(),c),
(m,u)->{};
还有一个简单的选择
Map Map=newhashmap();
forEach(e->map.put(e.getName(),e));
以下是解决方案
您可以使用IntStream创建索引流,然后将其转换为映射:
Map<Integer,Item> map =
IntStream.range(0,items.size())
.boxed()
.collect(Collectors.toMap (i -> i, i -> items.get(i)));
Map=
IntStream.range(0,items.size())
.boxed()
.collect(Collectors.toMap(i->i,i->items.get(i));
Map Map=list.stream().collect(Collectors.toMap(Choice::getName,s->s));
甚至为我服务
Map<String,Choice> map= list1.stream().collect(()-> new HashMap<String,Choice>(),
(r,s) -> r.put(s.getString(),s),(r,s) -> r.putAll(s));
Map Map=list1.stream().collect(()->new HashMap(),
(r,s)->r.put(s.getString(),s),(r,s)->r.putAll(s));
例如,如果要将对象字段转换为映射:
示例对象:
class Item{
private String code;
private String name;
public Item(String code, String name) {
this.code = code;
this.name = name;
}
//getters and setters
}
和操作将列表转换为地图:
List<Item> list = new ArrayList<>();
list.add(new Item("code1", "name1"));
list.add(new Item("code2", "name2"));
Map<String,String> map = list.stream()
.collect(Collectors.toMap(Item::getCode, Item::getName));
List List=new ArrayList();
添加(新项目(“代码1”、“名称1”);
添加(新项目(“代码2”、“名称2”);
Map=list.stream()
.collect(Collectors.toMap(Item::getCode,Item::getName));
这可以通过两种方式完成。让person成为我们用来演示它的类
public class Person {
private String name;
private int age;
public String getAge() {
return age;
}
}
让人员成为要转换到地图的人员列表
1.在列表中使用简单的foreach和Lambda表达式
Map<Integer,List<Person>> mapPersons = new HashMap<>();
persons.forEach(p->mapPersons.put(p.getAge(),p));
Map-mappers=newhashmap();
persons.forEach(p->mapperses.put(p.getAge(),p));
2.在给定列表中定义的流上使用收集器
Map<Integer,List<Person>> mapPersons =
persons.stream().collect(Collectors.groupingBy(Person::getAge));
Map-mappers=
persons.stream().collect(收集器.groupingBy(Person::getAge));
可以使用流来实现这一点。为了避免显式使用收集器
,可以静态导入toMap
(根据有效Java第三版的建议)
导入静态java.util.stream.Collectors.toMap;
私有静态映射nameMap(列表选项){
返回choices.stream().collect(toMap(Choice::getName,it->it));
}
我将编写如何使用泛型和控制反转将列表转换为映射。只是通用方法强>
也许我们有整数列表或对象列表。所以问题是:地图的键应该是什么?
创建接口
public interface KeyFinder<K, E> {
K getKey(E e);
}
公共接口密钥查找器{
K getKey(E);
}
现在使用控制反转:
static <K, E> Map<K, E> listToMap(List<E> list, KeyFinder<K, E> finder) {
return list.stream().collect(Collectors.toMap(e -> finder.getKey(e) , e -> e));
}
静态地图列表映射(列表、键盘查找器){
return list.stream().collect(Collectors.toMap(e->finder.getKey(e),e->e));
}
例如,如果我们有book的对象,这个类将为地图选择key
public class BookKeyFinder implements KeyFinder<Long, Book> {
@Override
public Long getKey(Book e) {
return e.getPrice()
}
}
公共类
List<Item> list = new ArrayList<>();
list.add(new Item("code1", "name1"));
list.add(new Item("code2", "name2"));
Map<String,String> map = list.stream()
.collect(Collectors.toMap(Item::getCode, Item::getName));
public class Person {
private String name;
private int age;
public String getAge() {
return age;
}
}
Map<Integer,List<Person>> mapPersons = new HashMap<>();
persons.forEach(p->mapPersons.put(p.getAge(),p));
Map<Integer,List<Person>> mapPersons =
persons.stream().collect(Collectors.groupingBy(Person::getAge));
import static java.util.stream.Collectors.toMap;
private static Map<String, Choice> nameMap(List<Choice> choices) {
return choices.stream().collect(toMap(Choice::getName, it -> it));
}
public interface KeyFinder<K, E> {
K getKey(E e);
}
static <K, E> Map<K, E> listToMap(List<E> list, KeyFinder<K, E> finder) {
return list.stream().collect(Collectors.toMap(e -> finder.getKey(e) , e -> e));
}
public class BookKeyFinder implements KeyFinder<Long, Book> {
@Override
public Long getKey(Book e) {
return e.getPrice()
}
}
public Map<String, Choice> convertListToMap(List<Choice> choices) {
return choices.stream()
.collect(Collectors.toMap(Choice::getName, choice -> choice,
(oldValue, newValue) -> newValue));
}
String array[] = {"ASDFASDFASDF","AA", "BBB", "CCCC", "DD", "EEDDDAD"};
List<String> list = Arrays.asList(array);
Map<Integer, String> map = list.stream()
.collect(Collectors.toMap(s -> s.length(), s -> s, (x, y) -> {
System.out.println("Dublicate key" + x);
return x;
},()-> new TreeMap<>((s1,s2)->s2.compareTo(s1))));
System.out.println(map);
List<V> choices; // your list
Map<K,V> result = choices.stream().collect(Collectors.toMap(choice::getKey(),choice));
//assuming class "V" has a method to get the key, this method must handle case of duplicates too and provide a unique key.
private Map<String, Choice> nameMap(List<Choice> choices) {
return CollectionsKt.associateBy(choices, Choice::getName);
}
Map<String, Choice> result =
choices.stream().collect(Collectors.toMap(c -> c.getName(), c -> c)));
Map<String, Choice> result =
choices.stream().collect(Collectors.toMap(c -> c.getUser().getName(), c -> c)));