将一个java示例转换map
为list
map map = new hashmap<>();
// convert all map keys to a list
list result = new arraylist(map.keyset());
// convert all map values to a list
list result2 = new arraylist(map.values());
// java 8, convert all map keys to a list
list result3 = map.keyset().stream()
.collect(collectors.tolist());
// java 8, convert all map values to a list
list result4 = map.values().stream()
.collect(collectors.tolist());
// java 8, seem a bit long, but you can enjoy the stream features like filter and etc.
list result5 = map.values().stream()
.filter(x -> !"apple".equalsignorecase(x))
.collect(collectors.tolist());
// java 8, split a map into 2 list, it works!
// refer example 3 below
~~~~~~~~~~~~~~~~~~~~~~~~~~
分类:
1.映射到list
为了简单map
的list
转换,只需使用下面的代码:
convertmaptolist.java
package com.mkyong;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
public class convertmaptolist {
public static void main(string[] args) {
map map = new hashmap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
system.out.println("\n1. export map key to list...");
list result = new arraylist(map.keyset());
result.foreach(system.out::println);
system.out.println("\n2. export map value to list...");
list result2 = new arraylist(map.values());
result2.foreach(system.out::println);
}
}
output
1. export map key to list...
50
20
40
10
30
2. export map value to list...
dragonfruit
orange
watermelon
apple
banana
2.java 8 - 映射到list
对于java 8,您可以将其map
转换为流,将其处理并将其返回list
convertmaptolist.java
package com.mkyong;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.stream.collectors;
public class convertmaptolist {
public static void main(string[] args) {
map map = new hashmap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
system.out.println("\n1. export map key to list...");
list result = map.keyset().stream()
.collect(collectors.tolist());
result.foreach(system.out::println);
system.out.println("\n2. export map value to list...");
list result2 = map.values().stream()
.collect(collectors.tolist());
result2.foreach(system.out::println);
system.out.println("\n3. export map value to list..., say no to banana");
list result3 = map.keyset().stream()
.filter(x -> !"banana".equalsignorecase(x))
.collect(collectors.tolist());
result3.foreach(system.out::println);
}
}
output
1. export map key to list...
50
20
40
10
30
2. export map value to list...
dragonfruit
orange
watermelon
apple
banana
3. export map value to list..., say no to banana
dragonfruit
orange
watermelon
apple
3.java 8 - 将map转换成list
这个例子有点极端,map.entryset()
用来转换map
成list
convertmaptolist.java
package com.mkyong;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.stream.collectors;
//https://www.mkyong.com/java8/java-8-how-to-sort-a-map/
public class convertmaptolist {
public static void main(string[] args) {
map map = new hashmap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
// split a map into 2 list
list resultsortedkey = new arraylist<>();
list resultvalues = map.entryset().stream()
//sort a map by key and stored in resultsortedkey
.sorted(map.entry.comparingbykey().reversed())
.peek(e -> resultsortedkey.add(e.getkey()))
.map(x -> x.getvalue())
// filter banana and return it to resultvalues
.filter(x -> !"banana".equalsignorecase(x))
.collect(collectors.tolist());
resultsortedkey.foreach(system.out::println);
resultvalues.foreach(system.out::println);
}
}
output
//resultsortedkey
50
40
30
20
10
//resultvalues
dragonfruit
watermelon
orange
apple