您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java中List使用stream流轉(zhuǎn)成map的方法有哪些的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Java中List使用stream流轉(zhuǎn)成map的方法有哪些文章都會(huì)有所收獲,下面我們一起來看看吧。
public class Person { private String name; private String address; public Person(String name, String address) { this.name = name; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
List<Person> list = new ArrayList<>(); Person person1 = new Person("熊大","森林第一個(gè)小屋"); Person person2 = new Person("熊二","森林第二個(gè)小屋"); Person person3 = new Person("光頭強(qiáng)","森林第三個(gè)小屋"); Person person4 = new Person("熊大","森林第四個(gè)小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map<String,Person> map = list.stream().collect(Collectors.toMap(Person::getName,each->each,(value1, value2) -> value1)); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“光頭強(qiáng)”:{“address”:“森林第三個(gè)小屋”,“name”:“光頭強(qiáng)”},“熊大”:{“address”:“森林第一個(gè)小屋”,“name”:“熊大”},“熊二”:{“address”:“森林第二個(gè)小屋”,“name”:“熊二”}}
List<Person> list = new ArrayList<>(); Person person1 = new Person("熊大","森林第一個(gè)小屋"); Person person2 = new Person("熊二","森林第二個(gè)小屋"); Person person3 = new Person("光頭強(qiáng)","森林第三個(gè)小屋"); Person person4 = new Person("熊大","森林第四個(gè)小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map<String,String> map = list.stream().collect(Collectors.toMap(Person::getName,Person::getAddress,(value1, value2) -> value1)); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“光頭強(qiáng)”:“森林第三個(gè)小屋”,“熊大”:“森林第一個(gè)小屋”,“熊二”:“森林第二個(gè)小屋”}
List<Person> list = new ArrayList<>(); Person person1 = new Person("熊大","森林第一個(gè)小屋"); Person person2 = new Person("熊二","森林第二個(gè)小屋"); Person person3 = new Person("光頭強(qiáng)","森林第三個(gè)小屋"); Person person4 = new Person("熊大","森林第四個(gè)小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map<String, List<Person>> map = list.stream().collect(Collectors.groupingBy(Person::getName)); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“光頭強(qiáng)”:[{“address”:“森林第三個(gè)小屋”,“name”:“光頭強(qiáng)”}],“熊大”:[{“address”:“森林第一個(gè)小屋”,“name”:“熊大”},{“address”:“森林第四個(gè)小屋”,“name”:“熊大”}],“熊二”:[{“address”:“森林第二個(gè)小屋”,“name”:“熊二”}]}
List<Person> list = new ArrayList<>(); Person person1 = new Person("熊大","森林第一個(gè)小屋"); Person person2 = new Person("熊二","森林第二個(gè)小屋"); Person person3 = new Person("光頭強(qiáng)","森林第三個(gè)小屋"); Person person4 = new Person("熊大","森林第四個(gè)小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map<String,List<Person>> map = list.stream().collect(Collectors.toMap(Person::getName,each->Collections.singletonList(each),(value1, value2) -> { List<Person> union = new ArrayList<>(value1); union.addAll(value2); return union; })); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“光頭強(qiáng)”:[{“address”:“森林第三個(gè)小屋”,“name”:“光頭強(qiáng)”}],“熊大”:[{“address”:“森林第一個(gè)小屋”,“name”:“熊大”},{“address”:“森林第四個(gè)小屋”,“name”:“熊大”}],“熊二”:[{“address”:“森林第二個(gè)小屋”,“name”:“熊二”}]}
List<Person> list = new ArrayList<>(); Person person1 = new Person("熊大","森林第一個(gè)小屋"); Person person2 = new Person("熊二","森林第二個(gè)小屋"); Person person3 = new Person("光頭強(qiáng)","森林第三個(gè)小屋"); Person person4 = new Person("熊大","森林第四個(gè)小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map<String,List<String>> map = list.stream().collect(Collectors.toMap(Person::getName,each->Collections.singletonList(each.getAddress()),(value1, value2) -> { List<String> union = new ArrayList<>(value1); union.addAll(value2); return union; })); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“光頭強(qiáng)”:[“森林第三個(gè)小屋”],“熊大”:[“森林第一個(gè)小屋”,“森林第四個(gè)小屋”],“熊二”:[“森林第二個(gè)小屋”]}
List<Map<String,Object>> list = new ArrayList<>(); Person person1 = new Person("熊大","森林第一個(gè)小屋"); Person person2 = new Person("熊二","森林第二個(gè)小屋"); Person person3 = new Person("光頭強(qiáng)","森林第三個(gè)小屋"); Map<String,Object> map1 = new HashMap<>(); map1.put("id","1"); map1.put("person",person1); Map<String,Object> map2 = new HashMap<>(); map2.put("id","2"); map2.put("person",person2); Map<String,Object> map3 = new HashMap<>(); map3.put("id","3"); map3.put("person",person3); list.add(map1); list.add(map2); list.add(map3); Map<String,Map<String,Object>> map = list.stream().collect(Collectors.toMap(each->Objects.toString(each.get("id"),""),each->each,(key1,key2)->key1)); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“1”:{“person”:{“address”:“森林第一個(gè)小屋”,“name”:“熊大”},“id”:“1”},“2”:{“person”:{“address”:“森林第二個(gè)小屋”,“name”:“熊二”},“id”:“2”},“3”:{“person”:{“address”:“森林第三個(gè)小屋”,“name”:“光頭強(qiáng)”},“id”:“3”}}
List<Map<String,Object>> list = new ArrayList<>(); Person person1 = new Person("熊大","森林第一個(gè)小屋"); Person person2 = new Person("熊二","森林第二個(gè)小屋"); Person person3 = new Person("光頭強(qiáng)","森林第三個(gè)小屋"); Map<String,Object> map1 = new HashMap<>(); map1.put("id","1"); map1.put("person",person1); Map<String,Object> map2 = new HashMap<>(); map2.put("id","2"); map2.put("person",person2); Map<String,Object> map3 = new HashMap<>(); map3.put("id","3"); map3.put("person",person3); list.add(map1); list.add(map2); list.add(map3); Map<String,Object> map = list.stream().collect(Collectors.toMap(each->Objects.toString(each.get("id"),""),each->each.get("person"),(key1,key2)->key1)); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“1”:{“address”:“森林第一個(gè)小屋”,“name”:“熊大”},“2”:{“address”:“森林第二個(gè)小屋”,“name”:“熊二”},“3”:{“address”:“森林第三個(gè)小屋”,“name”:“光頭強(qiáng)”}}
List<Map<String,String>> list = new ArrayList<>(); Map<String,String> map1 = new HashMap<>(); map1.put("id","1"); map1.put("name","熊大"); map1.put("address","森林第一個(gè)小屋"); Map<String,String> map2 = new HashMap<>(); map2.put("id","2"); map2.put("name","熊二"); map2.put("address","森林第二個(gè)小屋"); Map<String,String> map3 = new HashMap<>(); map3.put("id","3"); map3.put("name","光頭強(qiáng)"); map3.put("address","森林第三個(gè)小屋"); list.add(map1); list.add(map2); list.add(map3); Map<String,Map<String,String>> map = list.stream().collect(Collectors.toMap(each->each.get("id"),each->each,(key1,key2)->key1)); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“1”:{“address”:“森林第一個(gè)小屋”,“name”:“熊大”,“id”:“1”},“2”:{“address”:“森林第二個(gè)小屋”,“name”:“熊二”,“id”:“2”},“3”:{“address”:“森林第三個(gè)小屋”,“name”:“光頭強(qiáng)”,“id”:“3”}}
List<Map<String,String>> list = new ArrayList<>(); Map<String,String> map1 = new HashMap<>(); map1.put("id","1"); map1.put("name","熊大"); map1.put("address","森林第一個(gè)小屋"); Map<String,String> map2 = new HashMap<>(); map2.put("id","2"); map2.put("name","熊二"); map2.put("address","森林第二個(gè)小屋"); Map<String,String> map3 = new HashMap<>(); map3.put("id","3"); map3.put("name","光頭強(qiáng)"); map3.put("address","森林第三個(gè)小屋"); list.add(map1); list.add(map2); list.add(map3); Map<String,String> map = list.stream().collect(Collectors.toMap(each->each.get("id"),each->each.get("name"),(key1,key2)->key1)); System.out.println(JSON.toJSONString(map));
控制臺(tái)打印日志:
{“1”:“熊大”,“2”:“熊二”,“3”:“光頭強(qiáng)”}
關(guān)于“Java中List使用stream流轉(zhuǎn)成map的方法有哪些”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Java中List使用stream流轉(zhuǎn)成map的方法有哪些”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。