For Java beginners: List, Map, Iterator / Array ... How to convert?

When programming in Java, both training and non-training I think List, Map, etc. often appear (sometimes Set, Iterator, etc.).

However, what is converted after a long time Because it should be "that? How do you convert this?"

1. Quick reference table for the time being

Static methods for those with class names, instance methods for those with only method names

\ Conversion destination
Conversion source|List, Set, etc.(Collection)
Map Iterator Stream Array
List/Set etc.
constructor - iterator() stream()
Map entrySet(), keySet(), values() constructor (Via Set) (Via Set)
Stream Collect() Collect() iterator() Intermediate operation in general
Array (List only)
- (Via List) Steam.of()

2. Explain only the important points

・ Collection (List / Set, etc.) → Collection (List / Set, etc.)

Java's built-in Collection classes (ArrayList, HashSet, ArrayDeque, etc.) Some constructors put a Collection in it. Map is not a Collection, but you can do the same with Map → Map.

 //Prepare Set in advance
 Set<String> ESet = Set.of("list","set","deque");
 //Set to List!
 List<String> EList = new ArrayList(Eset);

・ Map → Collection, Iterator

If you want only the key list, use keySet. If you want only the contents, use values (). entrySet () if you want to use both in the for statement

 //Prepare Map in advance
 Map<String,Collection<String>> EMap = new HashMap<>();

 //Map to Set!
 Set<String> ESet = EMap.keySet();
 //Set to Iterator!

 //Map → Iterator is also possible by connecting

・ Conversion from List to array

 List<String> EList = List.of("list","set","deque");
 String[] EArray = EList.toArray(new String[0]);

3. For those who have a little time: I want to convert to a special collection ... Convert using Stream

From Stream, you can make it a Collection by using collect (). (It is also possible to use other than Collection such as array and Map) In particular, you can easily create almost any Collection by using Collectors.toCollection.

・ Conversion using the built-in Collector

 List<String> EList = List.of("list","set","deque");
 //Convert to Set
 Set<String> ESet =;

Note: ** Collectors.toSet () and Collectors.toList () are not guaranteed to be modifiable or impossible! **
(Example: toList is currently an ArrayList, but it may suddenly become an immutable List)

 List<Integer> EList = List.of(1,2,3);
 //Convert the element of the list to the key and the string to the Map to set the value
 Map<Integer,String> EMap =, String::valueOf));

-Conversion using Collectors.toCollection

 List<Integer> EList = List.of(1,2,3);
 //If you want to specify the implementation class of List, use toCollection
 LinkedList<Integer> EList =;

Recommended Posts

For Java beginners: List, Map, Iterator / Array ... How to convert?
[Java] [For beginners] How to insert elements directly in a 2D array
[Java] How to use Map
[Java] How to use Map
How to use Java Map
[Java] Convert ArrayList to array
How to initialize Java array
[For beginners] How to operate Stream API after Java 8
How to convert a file to a byte array in Java
How to loop Java Map (for Each / extended for statement)
[Java] How to use List [ArrayList]
How to turn Iterator Dojo (Java)
[Java] Conversion from array to List
How to make a Java array
[Java] Convert array to ArrayList * Caution
[Java] Get List / Map elements with Iterator
[Java] How to add data to List (add, addAll)
[Ruby] How to use slice for beginners
[For beginners] How to debug in Eclipse
Array / list / map
[Java] How to search for a value in an array (or list) with the contains method
[Java] How to turn a two-dimensional array with an extended for statement
[Java] [ibatis] How to get records of 1-to-N relationship with List <Map <>>
[Java] How to test for null with JUnit
[Java] From two Lists to one array list
How to use an array for HashMap keys
How to create pagination for a "kaminari" array
[For beginners] How to implement the delete function
[Java] (for MacOS) How to set the classpath
[For super beginners] How to use autofocus: true
[Introduction to Java] Basics of java arithmetic (for beginners)
[Java] How to operate List using Stream API
[Java] How to make multiple for loops single
[Java] How to convert one element of a String type array to an Int type
[For beginners] Introduction to Java Basic knowledge of Java language ③ Array, selection structure, iteration structure
How to use Map
About Java Array List
How to use Map
How to implement login request processing (Rails / for beginners)
Sample code to convert List to List <String> in Java Stream
java: How to write a generic type list [Note]
I learned stream (I want to convert List to Map <Integer, List>)
[Spring Boot] How to create a project (for beginners)
[For beginners] Minimum sample to display RecyclerView in Java
Java development for beginners to start from 1-Vol.1-eclipse setup
Introduction to Java for beginners Basic knowledge of Java language ①
How to convert A to a and a to A using AND and OR in Java
List of frequently used Java instructions (for beginners and beginners)
How to execute WebCamCapture sample of NyARToolkit for Java
How to use GitHub for super beginners (team development)
How to lower java version
Convert Java Powerpoint to XPS
How to use SAS tokens for Azure Event hubs (Java)
How to uninstall Java 8 (Mac)
[Java] I want to convert a byte array to a hexadecimal number
How to create a lightweight container image for Java apps
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
How to use java Optional
How to minimize Java images
Convert a Java byte array to a string in hexadecimal notation