The difference of some collections

本文深入探讨Java集合框架中List、Set及Map接口的不同实现方式,包括ArrayList、LinkedList、HashSet、TreeSet、HashMap、TreeMap等,并对比它们在不同场景下的性能特点。

The content of this chapter is from Think in Java 3th ,bruce ecekl

ArrayList 
由数组实现的 List。允许对元素进行快速随机访问,但是向 List 中间插入与移除元素的速度很慢。ListIterator 只应该用来由后向前遍历 ArrayList,而不是用来插入和移除元素,因为那比 LinkedList 开销要大很多。
LinkedList
对顺序访问进行了优化,向 List 中间插入与删除的开销并不大。随机访问则相对较慢。(使用 ArrayList 代替。)还具有下列方法:addFirst(),addLast(),getFirst(),getLast(),removeFirst(),和 removeLast(),这些方法(没有在任何接口或基类中定义过)使得 LinkedList 可以当作堆栈、队列和双向队列使用。


Set (interface) 存入 Set 的每个元素都必须是唯一的,因为 Set 不保存重复元素,加入 Set 的元素必须定义 equals()方法以确保对象的唯一性。Set 与 Collection 有完全一样的接口。Set 接口不保证维护元素的次序。   

HashSet*:
   为快速查找设计的 Set,存入 HashSet 的对象必须定义hashCode().
TreeSet:
   保持次序的 Set,底层为树结构。使用它可以从 Set中提取有序的序列。
LinkedHashSet:
   具有 HashSet 的查询速度,且内部使用链表维护元素的顺序(插入的次序)。于是在使用迭代器遍历 Set时,结果会按元素插入的次序显示。

HashMap*:
         Map 基于散列表的实现。 (取代了 Hashtable)插入和查询“键值对”的开销是固定的。可以通过构造器设置容量

capacity 和负载因子 loadfactor,以调整容器的性能。

LinkedHashMap:
         类似于 HashMap,但是迭代遍历它时,取得“键值对”的顺序是其插入次序,或者是最近最少使用(LRU)的次序。只比

HashMap 慢一点。而在迭代访问时反而更快,因为它使用链表维护内部次序。

TreeMap:
      基于红黑树数据结构的实现。查看“键”或“键值对”时,它们会被排序(次序由 Comparable或 Comparator 决定,稍后会讨论)。TreeMap的特点在于,你得到的结果是经过排序的。TreeMap 是唯一的带有 subMap()方法的 Map,它可以返回一个子树。

WeakHashMap:
            弱键(weak key)Map,Map 中使用的对象也被允许释放;这是为解决特殊问题设计的。如果没有 map 之外的引用指向某个“键”,则此键”可以被垃圾收集器回收。

一个文件夹里含有6个如下格式内容的大小不一的JSON文件[{ "filename": "analytical_techniques-analytical_techniques_1-analytical_techniques_1.json", "prompt": "The following is a multiple choice question about chemistry.\nPlease answer by responding with the letter of the correct answer.\n\nQuestion: In a Raman spectroscopy experiment, a sample is illuminated with monochromatic light. The following observations are made: 1. Some scattered light has a longer wavelength than the incident light; 2. The intensity of this scattered light decreases when the sample temperature is lowered, 3. The energy difference between the incident and this scattered light matches a molecular vibration energy. Based on these observations, some interpretations are as follows. I. The described scattered light represents Stokes lines. II. The scattered photons have higher energy than the incident photons. III. Lowering the temperature increases the population of ground-state molecules. IV. The scattering process involves energy transfer to the molecule. V. This scattering would be stronger for molecules with more polarizable electron clouds. Which combination of these interpretations is correct?\n\nOptions:\nA. I, II, and III\nB. I, III, and IV\nC. I, IV, and V\nD. II, III, and V\nE. III, IV, and V\n\nYou MUST include the letter(s) of the correct answer (separated by comma if there are many) within the following tags: [ANSWER] and [/ANSWER].\nFor example, '[ANSWER]<answer>[/ANSWER]', where <answer> is comma- or space-separated list of the correct letters. Always answer in exactly this format of comma-separated letters between the two tags, even if you are unsure. We require this because we use automatic parsing.", "targets_": { "I, II, and III": 0, "I, III, and IV": 1, "I, IV, and V": 0, "II, III, and V": 0, "III, IV, and V": 0 }, "type": "multiple_choice" },...]现在要求统计在这些文件中出现了至少5次的"prompt"的内容,并将其与其对应targets_写入新的文件里。如"The following is a multiple choice question about chemistry.\nPlease answer by responding with the letter of the correct answer.\n\nQuestion: In a Raman spectroscopy experiment, a sample is illuminated with monochromatic light. The following observations are made: 1. Some scattered light has a longer wavelength than the incident light; 2. The intensity of this scattered light decreases when the sample temperature is lowered, 3. The energy difference between the incident and this scattered light matches a molecular vibration energy. Based on these observations, some interpretations are as follows. I. The described scattered light represents Stokes lines. II. The scattered photons have higher energy than the incident photons. III. Lowering the temperature increases the population of ground-state molecules. IV. The scattering process involves energy transfer to the molecule. V. This scattering would be stronger for molecules with more polarizable electron clouds. Which combination of these interpretations is correct?\n\nOptions:\nA. I, II, and III\nB. I, III, and IV\nC. I, IV, and V\nD. II, III, and V\nE. III, IV, and V\n\nYou MUST include the letter(s) of the correct answer (separated by comma if there are many) within the following tags: [ANSWER] and [/ANSWER].\nFor example, '[ANSWER]<answer>[/ANSWER]', where <answer> is comma- or space-separated list of the correct letters. Always answer in exactly this format of comma-separated letters between the two tags, even if you are unsure. We require this because we use automatic parsing."出现了五次,就按照[{ "prompt": "The following is a multiple choice question about chemistry.\nPlease answer by responding with the letter of the correct answer.\n\nQuestion: In a Raman spectroscopy experiment, a sample is illuminated with monochromatic light. The following observations are made: 1. Some scattered light has a longer wavelength than the incident light; 2. The intensity of this scattered light decreases when the sample temperature is lowered, 3. The energy difference between the incident and this scattered light matches a molecular vibration energy. Based on these observations, some interpretations are as follows. I. The described scattered light represents Stokes lines. II. The scattered photons have higher energy than the incident photons. III. Lowering the temperature increases the population of ground-state molecules. IV. The scattering process involves energy transfer to the molecule. V. This scattering would be stronger for molecules with more polarizable electron clouds. Which combination of these interpretations is correct?\n\nOptions:\nA. I, II, and III\nB. I, III, and IV\nC. I, IV, and V\nD. II, III, and V\nE. III, IV, and V\n\nYou MUST include the letter(s) of the correct answer (separated by comma if there are many) within the following tags: [ANSWER] and [/ANSWER].\nFor example, '[ANSWER]<answer>[/ANSWER]', where <answer> is comma- or space-separated list of the correct letters. Always answer in exactly this format of comma-separated letters between the two tags, even if you are unsure. We require this because we use automatic parsing.", "targets_": { "I, II, and III": 0, "I, III, and IV": 1, "I, IV, and V": 0, "II, III, and V": 0, "III, IV, and V": 0 }},...]的格式存于output文件里
09-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值