10-10
27
list,set,map,数组间的相互转换
作者:Java伴侣 日期:2010-10-27
1.list转set
Java代码
Set set = new HashSet( new ArrayList());
Java代码
Set set = new HashSet( new ArrayList());
Set set = new HashSet(new ArrayList());
2.set转list
Java代码
List list = new ArrayList( new HashSet());
Java代码
List list = new ArrayList( new HashSet());
List list = new ArrayList(new HashSet());
3.数组转为list
Java代码
List stooges = Arrays.asList( "Larry" , "Moe" , "Curly" );
Java代码
List stooges = Arrays.asList( "Larry" , "Moe" , "Curly" );
List stooges = Arrays.asList("Larry", "Moe", "Curly");
此时stooges中有有三个元素。
4.数组转为set
int[] a = { 1, 2, 3 };
Java代码
Set set = new HashSet(Arrays.asList(a));
Java代码
Set set = new HashSet(Arrays.asList(a));
Set set = new HashSet(Arrays.asList(a));
5.map的相关操作。
Java代码
Map map = new HashMap();
map.put( "1" , "a" );
map.put( '2' , 'b' );
map.put( '3' , 'c' );
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
Java代码
Map map = new HashMap();
map.put( "1" , "a" );
map.put( '2' , 'b' );
map.put( '3' , 'c' );
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
Map map = new HashMap();
map.put("1", "a");
map.put('2', 'b');
map.put('3', 'c');
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
6.list转数组
Java代码
List list = Arrays.asList( "a" , "b" );
System.out.println(list);
String[] arr = (String[])list.toArray( new String[list.size()]);
System.out.println(Arrays.toString(arr));
Java代码
List list = Arrays.asList( "a" , "b" );
System.out.println(list);
String[] arr = (String[])list.toArray( new String[list.size()]);
System.out.println(Arrays.toString(arr));
Java代码
Set set = new HashSet( new ArrayList());
Java代码
Set set = new HashSet( new ArrayList());
Set set = new HashSet(new ArrayList());
2.set转list
Java代码
List list = new ArrayList( new HashSet());
Java代码
List list = new ArrayList( new HashSet());
List list = new ArrayList(new HashSet());
3.数组转为list
Java代码
List stooges = Arrays.asList( "Larry" , "Moe" , "Curly" );
Java代码
List stooges = Arrays.asList( "Larry" , "Moe" , "Curly" );
List stooges = Arrays.asList("Larry", "Moe", "Curly");
此时stooges中有有三个元素。
4.数组转为set
int[] a = { 1, 2, 3 };
Java代码
Set set = new HashSet(Arrays.asList(a));
Java代码
Set set = new HashSet(Arrays.asList(a));
Set set = new HashSet(Arrays.asList(a));
5.map的相关操作。
Java代码
Map map = new HashMap();
map.put( "1" , "a" );
map.put( '2' , 'b' );
map.put( '3' , 'c' );
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
Java代码
Map map = new HashMap();
map.put( "1" , "a" );
map.put( '2' , 'b' );
map.put( '3' , 'c' );
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
Map map = new HashMap();
map.put("1", "a");
map.put('2', 'b');
map.put('3', 'c');
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
6.list转数组
Java代码
List list = Arrays.asList( "a" , "b" );
System.out.println(list);
String[] arr = (String[])list.toArray( new String[list.size()]);
System.out.println(Arrays.toString(arr));
Java代码
List list = Arrays.asList( "a" , "b" );
System.out.println(list);
String[] arr = (String[])list.toArray( new String[list.size()]);
System.out.println(Arrays.toString(arr));
评论: 0 | 引用: 0 | 查看次数: 421
发表评论