Java array list map between the introduction of the three

  • 2020-04-01 02:22:17
  • OfStack

The relationship between the three is transformed, and a picture is clearly presented.
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201310/201310291116471.gif? 201392911179 ">  
The code:

Maputils is an apache collection package.
 
package util; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.apache.commons.collections.MapUtils; 

public class Convert { 
public static void main(String[] args) { 
//1. Convert the array to list and map
String[] arr = {"123","456","789","123"}; 
List<String> list = Arrays.asList(arr); 
//The array is converted to map. When the array is one-dimensional, the odd number is key, the even number is value, the odd number of elements, and the last one is discarded
//. Two dimensional arrays as two one-dimensional arrays
Map map = MapUtils.putAll(new HashMap(), arr); 

String[][] ss = {{"a","b","e"},{"c","d","f"}}; 
MapUtils.debugPrint(System.out,arr,map); 
MapUtils.putAll(map, arr); 
MapUtils.debugPrint(System.out,arr,map); 
MapUtils.putAll(map, ss); 
MapUtils.verbosePrint(System.out,arr,MapUtils.invertMap(map)); 

//List is converted to arr
List<String> ls = new ArrayList<String>(); 
ls.add("wch"); 
ls.add("name"); 
String[] as = (String[]) ls.toArray(); 

} 
} 

Related articles: