java的集合
2024-09-14 08:49:59
Collection: 1.list ArrayList、Vector、LinkedList ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。 Vector是同步的 2.set 3.queue Map: 1.hashTable 2.hashMap 3.treeTable
java.util是使用工具类,包括常用的集合框架。java.lang是基础类,包括基本Object类、Class类、String类、基本类型的包装类、基本的数学类等等
为何Collection不从Cloneable和Serializable接口继承? Collection是一个抽象表现,具体实现应该决定如何对它进行克隆或序列化
Enumeration不是线程安全的,虽然它比iterator快。Iterator来遍历Set和List集合,而ListIterator只能遍历List。使用并发集合类来避免ConcurrentModificationException,比如使用CopyOnWriteArrayList,而不是ArrayList。Array可以容纳基本类型和对象,而ArrayList只能容纳对象。
1.并发集合类是什么? Java1.5并发包(java.util.concurrent)包含线程安全集合类,允许在迭代时修改集合。迭代器被设计为fail-fast的,会抛出ConcurrentModificationException。一部分类为:CopyOnWriteArrayList、 ConcurrentHashMap、CopyOnWriteArraySet。 2.BlockingQueue是什么? BlockingQueue接口是Java集合框架的一部分,主要用于实现生产者-消费者模式。 BlockingQueue队列是一种支持操作,它等待元素变得可用时来检索,同样等待空间可用时来存储元素。3.Comparable和Comparator接口有何区别? Comparable接口被用来提供对象的自然排序,我们可以使用它来提供基于单个逻辑的排序。Comparator接口被用来提供不同的排序算法,我们可以选择需要使用的Comparator来对给定的对象集合进行排序。4.当一个集合被作为参数传递给一个函数时,如何才可以确保函数不能修改它? 可以使用Collections.unmodifiableCollection(Collection c)方法创建一个只读集合,这将确保改变集合的任何操作都会抛出UnsupportedOperationException。
9.Hashmap如何同步?
当我们需要一个同步的HashMap时,有两种选择:
●使用Collections.synchronizedMap(..)来同步HashMap。
●使用ConcurrentHashMap的
部分算法有排序、搜索、混编、最大最小值。
http://www.importnew.com/15980.html
如何遍历map?
https://www.cnblogs.com/blest-future/p/4628871.html
System.out.println("第一种:通过Map.keySet遍历key和value:"); for (Integer in : map.keySet()) { //map.keySet()返回的是所有key的值 String str = map.get(in);//得到每个key多对用value的值 System.out.println(in + " " + str); } /***********************************************/ System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value:"); Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Integer, String> entry = it.next(); System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } /***********************************************/ System.out.println("第三种:通过Map.entrySet遍历key和value"); for (Map.Entry<Integer, String> entry : map.entrySet()) { //Map.entry<Integer,String> 映射项(键-值对) 有几个方法:用上面的名字entry //entry.getKey() ;entry.getValue(); entry.setValue(); //map.entrySet() 返回此映射中包含的映射关系的 Set视图。 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } /***********************************************/ System.out.println("第四种:通过Map.values()遍历所有的value,但不能遍历key"); for (String v : map.values()) { System.out.println("value= " + v); }
最新文章
- JavaScript之Array类型
- php file_get_contents与curl性能比较
- 渐变背景 css3渐变效果及代码
- maven Spring 4.2+SpringMVC+dubbo解决TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
- 服务器NPC的ID如何分配的
- ios开发——实用技术篇Swift篇&;地址薄、短信、邮件
- 1.2 XmlBeanFactory的实例化
- Python之路,Day10 - 异步IO\数据库\队列\缓存
- 权限设计实现(MVC4+Bootstrap+ PetaPoco+Spring.Net)
- .NET Core使用微软官方类库实现汉字转拼音
- [转载]使用IEDriverServer.exe驱动IE11,实现自动化测试
- ossfs 使用挂在到ecs -centos 6.8
- Python&#160;解决Python安装包时提示Unable&#160;to&#160;find&#160;vcvarsall.bat的问题
- WPF C#仿ios 安卓 红点消息提示
- springsession 实现session 共享
- javascript 面向对象之路.1 - 小蜜蜂
- Scrum_Sprint
- simhash与Google的网页去重
- Android为ViewPager添加切换动画——自己定义ViewPager
- Android输入控件EditText和软键盘监听
热门文章
- delphi 中出现dataset not in edit or insert mode的问题
- SQL Server中的完全连接(full join)
- 深度学习中dropout策略的理解
- yolo算法解析
- codeforces478C
- Codeforces1036G Sources and Sinks 【构造】【状态压缩】
- springMVC整理03--处理数据模型 &; 试图解析器 &; @ResponseBody &; HttpEntity
- spfa优化板子
- bzoj 3673 可持久化并查集 by zky
- ansible 开源批量管理服务器工具