java集合的互转
List<-->数组、List<-->Set、数组<-->Set、Map将键转化为Set、Map将值转化为Set、Map将值转化为List等集合常
public class Test {
public static void main(String[] args) {
//List-->数组
List<String> list = new ArrayList<String>();
list.add("Tom");
list.add("Java");
list.add("Jack");
Object[] objects = list.toArray();//返回Object数组
System.out.println("objects:"+Arrays.toString(objects));
String[] strings1 = new String[list.size()];
list.toArray(strings1);//将转化后的数组放入已经创建好的对象中
System.out.println("strings1:"+Arrays.toString(strings1));
String[] strings2 = list.toArray(new String[0]);//将转化后的数组赋给新对象
System.out.println("strings2:"+Arrays.toString(strings2));
//数组-->List
String[] ss = {"Jack","Kitter"};
List<String> list1 = Arrays.asList(ss);
List<String> list2 = Arrays.asList("Tom","Jack");
System.out.println(list1);
System.out.println(list2);
//Set --> List
List<String> list3 = new ArrayList<String>(new HashSet<String>());
//List --> Set
Set<String> set = new HashSet<String>(new ArrayList<String>());
//数组-->Set
String[] strs = {"AA","BB"};
Set<String> set2 = new HashSet<String>(Arrays.asList(strs));
System.out.println(set2);
//Set-->数组
Set<String> set3 = new HashSet<String>(Arrays.asList("PP","OO"));
String[] strSet = new String[set3.size()];
set3.toArray(strSet);
System.out.println(Arrays.toString(strSet));
//Map操作
Map<String, String> map = new HashMap<String, String>();
map.put("YYY", "UUU");
map.put("RRR", "TTT");
// 将键转化为Set
Set<String> mapKeySet = map.keySet();
// 将值转化为Set
Set<String> mapValuesSet = new HashSet<String>(map.values());
// 将值转化为List
List<String> mapValuesList = new ArrayList<String>(map.values());
}
}
java集合的互转的更多相关文章
- java json数据转List对象的集合-----阿里巴巴插件---及原生json---JSON 与 对象 、集合 之间的转换 JSON字符串和java对象的互转【json-lib】
List<RunfastFullLess> list=(List<RunfastFullLess>)JSONArray.parseObject(activity.getFull ...
- java 集合与数组的互转方法,与源码分析
前言 java数组与集合需要互相转换的场景非常多,但是运用不好还是容易抛出UnSupportedOperationException.下面讲解一下互转的方法,以及结合源码分异常产生的原因 集合转数组 ...
- JAVA和.NET互调用
通过接口实现JAVA和.NET互调用-JNInterface 使用C#编程多年,也十分感激微软在语言架构.语法糖.编辑器等方面给自己带来的便利.但因为最近工作中有接触到JAVA,渐渐地发现的确像大家说 ...
- S2:java集合框架
Java集合就是一个容器.面向对象语言对事物的体现都是以对象的形式存在,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式.集合只用于存储对象,集合长度是可变的,集合可以 ...
- Java集合框架总结2_Map
1. Map接口概述 Map与Collection并列存在.用于保存具有映射关系的数据:key-value: Map中的key和value都可以是任何应用类型的数据: Map中的key用Set来存放, ...
- JAVA 集合 List 分组的两种方法
CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...
- Java集合原理分析和知识点大杂烩(多图初学者必备!!)
一.数据结构 数据结构就是计算机存储.组织数据的方式. 在计算机科学中,算法的时间复杂度是一个函数,它定性描述了该算法的运行时间,常用O符号来表述. 时间复杂度是同一问题可用不同算法解决, ...
- Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结
2017年的秋招彻底结束了,感觉Java上面的最常见的集合相关的问题就是hash--系列和一些常用并发集合和队列,堆等结合算法一起考察,不完全统计,本人经历:先后百度.唯品会.58同城.新浪微博.趣分 ...
- Scala集合和Java集合对应转换关系
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 用Scala编码的时候,经常会遇到scala集合和Java集合互相转换的case,特意mark一 ...
随机推荐
- 利用text插件和css插件优化web应用
JavaScript的模块化开发到如今,已经相当成熟了,当然,一个应用包含的不仅仅有js,还有html模板和css文件. 那么,如何将html和css也一起打包,来减少没必要的HTTP请求数呢? 本文 ...
- ios5 xcode 4.2 中 release显示编译警告或错误的解决方法
转自:http://lizi464789754.blog.163.com/blog/static/1689370852011924113245778/ 由于 iOS5 xcode4.2 引入了ARC ...
- javascript 实现htmlEncode htmlDecode
屌屌的写法..function htmlEncode(value){ //create a in-memory div, set it's inner text(which jQuery automa ...
- Boxes in a Line
Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...
- Java Bean validation specification...
http://www.ibm.com/developerworks/cn/java/j-lo-beanvalid/index.html
- Mysql 死锁相关操作
该随笔随时记录日常工作中遇到的关于mysql的死锁相关问题 1)查看mysql当前的处理线程(connection) mysql> show processlist; 2)杀掉对应的connec ...
- hdu 4665 搜索
思路:直接搜索 #include<iostream> #include<cstdio> #include<algorithm> #include<cstrin ...
- merge into update
如果要DML实现真正意义上的并发,在开始执行需要并发语句前,需要执行开启session并发 ALTER SESSION ENABLE PARALLEL DML; 在执行完语句后,需要执行关闭ses ...
- php 学习笔记
//本文转自:http://www.cnblogs.com/ronghua/p/6002995.html //语法错误(syntax error)在语法分析阶段,源代码并未被执行,故不会有任何输出. ...
- jQuery中的经典动画
show()方法和hide()方法是jQuery中的基本动画方法,hide()方法等于将css()方法设置display属性为none.如何让元素动起来呢,我们可以在show和hide里加入slow, ...