Java学习笔记之集合

以下是博主学java的时候记的一些笔记,分享给大家,如果有错误或者以为的话,可以在下方留言

集合


iterator

iterator()方法返回一个迭代器Iterator.与其他容器主要用于存储数据不同,Iterator主要用于遍历容器. 

iteraror 有三个方法。

hasNext()     判断迭代器中是否还有其他的元素

next()        将迭代器中的下一个元素返回出来

remove()      移除下一个元素(remove方法要和next方法一起使用)

注意: 当遍历Collection时不要使用Collection自带的remove方法删除数据,确实需要删除时,需要使用Iterator提供的remove.

 

ArrayLise

ArrayListList基于数组的实现,它封装了一个动态自增长/允许再分配的Object[]数组

作为数组集合来存储元素

 

Linkedlist

LinkedList是基于双向链表实现的List,虽然可以根据索引来访问集合中的元素,但性能不高(平均时间复杂度为O(N)),但其插入/删除操作非常迅速(尤其是在头尾,平均时间复杂度为O(1));除此之外,LinkedList还实现了Deque接口,因此还可以当成[双端]队列/栈来使用. 

 

:

<span style="font-size:18px;">/**

 * PriorityQueue的练习和使用

 */

package com.jihe;

 

import java.util.PriorityQueue;

import java.util.Queue;

import java.util.Random;

 

public class Dome_5 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Queue<Integer> queue = new PriorityQueue<Integer>();

Random random = new Random();

for(int i=0;i<10;i++){

queue.add(random.nextInt());

}

for(Integer integer:queue){

System.out.println(integer);

}

System.out.println("*********************"+"\n"+"***********************");

while(!queue.isEmpty()){

System.out.println(queue.poll());

}

}

 

}

/**

 * PriorityQueue 类的排列方式是按内部的优先级进行输出或操作的

 */</span>


答案:

<span style="font-size:18px;">-2119804942

-1535077454

-405984777

-619878933

581905698

1724031273

-202363508

924033956

-95536825

1514608436

*********************

***********************

-2119804942

-1535077454

-619878933

-405984777

-202363508

-95536825

581905698

924033956

1514608436

1724031273

 </span>

 

例:

<span style="font-size:18px;">/**

 * collection的使用

 */

package com.jihe;

 

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

import java.util.Random;

 

public class Demo_4 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Collection<Integer> collection = new ArrayList<>();

Random random = new Random();

for(int i=0;i<10;i++){

collection.add(random.nextInt());

}

Iterator<Integer> iterator = collection.iterator();

while(iterator.hasNext()){

Integer iterger = iterator.next();

System.out.println(iterger);

if(iterger<0){

iterator.remove();  

}//当遍历Collection时不要使用Collection自带的remove方法删除数据,确实需要删除时,需要使用Iterator提供的remove.          

}

System.out.println("****************"+"\n"+"*************");

for(Integer iterger:collection){

System.out.println(iterger);

}

}

}

 </span>


答案:

<span style="font-size:18px;"><span style="font-size:18px;">810882037

1318194443

1174286848

-1011163648

1466879012

767720164

2059363899

1944601863

-298313165

-320931497

****************

*************

810882037

1318194443

1174286848

1466879012

767720164

2059363899

1944601863

 </span></span>

集合

 

  集合的特点:

 1.用于存储对象的容器

 2.集合的长度是可变的

 3.集合中不可以存储基本数据类型

/**

 * 使用技巧

    在各种Lists中,最好的做法是以ArrayList作为缺省选择。当插入、删除频繁时,使用LinkedList();

    在各种Sets中,HashSet通常优于TreeSet(插入、查找)。只有当需要产生一个经过排序的序列,才用TreeSet。TreeSet存在的唯一理由:能够维护其内元素的排序状态。

 *  在各种Maps中,HashMap用于快速查找。

 *  当元素个数固定,用Array,因为Array效率是最高的。

结论:

    最常用的是ArrayList,HashSet,HashMap,Array。而且,我们也会发现一个规律,用TreeXXX都是排序的。

 * 注意:

 * Collection没有get()方法来取得某个元素。只能通过iterator()遍历元素。

 */

 * 集合容器因为内部的数据结构不同,有多种容器。

 * 框架的顶层Collection接口:

 * Collection的常见方法:

 *

 * 1.添加

 *  boolean add(Object obj);

 *  boolean addAll(Collection coll);

 * 2.删除

 *  boolean remove(Object obj);

 *  boolean removeAll(Collection coll);

 *  void clear();

 * 3.判断

 *  boolean contains(Object obj);

 *  boolean containsAll(Collection coll);

 *  boolean isEmpty():判断集合中是否有元素

 * 4.获取

 *  int size();

 *  Iterator iterator():迭代器

 *  

 * Collection

 *   |--List : 有序(存入和取出的顺序一致) 元素都有索引(角标) 元素可以重复

 *   |--Set  : 元素不能重复  无序

 *  

 * List的常见方法,有一个共性的特点就是可以操纵角标

 * 1.添加

 *   void add(index,element);

 *   void add(index,collection);

 * 2.删除

 *   Object remove(index);

 * 3.修改

 *   Object set(index);

 * 4.获取

 *   Object get(index);

 *   

 *  LinkedList实现了List接口,允许null元素。

 *  此外LinkedList提供额外的get,remove,insert方法在 LinkedList的首部或尾部。

 *  ArrayList实现了可变大小的数组。它允许所有元素,包括null。ArrayList没有同步。

 *  

 *  

 * Set:元素不可以重复  无序

 *  |--HashSet:内部数据结构是哈希表,是不同步的

 *  |      如何保证该集合的元素唯一?

 *  |      通过对象的hashCode和equals方法来完成对象唯一性的

 *  |      如果对象的hashCode值不同,那么就不需要判断对象的equal方法是否为true

 *  |      如果对象的hashCode值相同,那么要再次判断对象的equal方法是否为true

 *  |      如果为true,视为相同元素,不存,如果为false,那么视为不同元素,就进行存储

 *  |  

 *  |      记住:如果元素要存储到HashSet集合中,必须覆盖hashCode方法和equal方法

 *  |

 *  |--TreeSet:可以对Set集合中的元素进行排序,是不同步的

 *         判断元素唯一性的方式:就是根据比较方法的返回结果是否是0

 *     TreeSet对元素进行排序的方式一

 *         让“元素”自身具备比较功能,要实现Comparable接口。覆盖compareTo方法

 *     TreeSet对元素进行排序的方式二

 *         让“集合”自身具备比较功能,定义一个类实现Compatarator接口,覆盖compare方法

 *     将该类对象作为参数传递给TreeSet集合的构造函数

 *              

 *     Set子接口:无序,不允许重复。

 *     List子接口:有序,可以有重复元素。

 *  具体区别是:

 *     Set:检索元素效率低下,删除和插入效率高,插入和删除不会引起元素位置改变。

 *          <对应类有 HashSet,TreeSet>

 *     List:和数组类似,List可以动态增长,查找元素效率高,插入删除元素效率低,因为会引起其他元素位置改变。

 *          <相应类有 ArrayList,LinkedList,Vector>

 *            

 *     普遍的都是,set查询慢,插入快,list查询快,插入慢。

 *      

 *      

 *  Map主要用于存储健值对,根据键得到值,因此不允许键重复,但允许值重复。

 *   |--HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null;

 *   |  HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。

 *   |  如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力

 *   |  

 *   |--Hashtable与HashMap类似,不同的是:它不允许记录的键或者值为空;

 *      它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢

 *      TreeMap能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器,

 *      当用Iterator 遍历TreeMap时,得到的记录是排过序的。

 *      

 *      

 *      

 * public static void main(String[] args) {

 *

 *

 * Map<String, String> map = new HashMap<String, String>();

 * map.put("1", "value1");

 * map.put("2", "value2");

 * map.put("3", "value3");

 *

 * //第一种:普遍使用,二次取值

 * System.out.println("通过Map.keySet遍历key和value:");

 * for (String key : map.keySet()) {

 * System.out.println("key= "+ key + " and value= " + map.get(key));

 * }

 *

 * //第二种

 * System.out.println("通过Map.entrySet使用iterator遍历key和value:");

 * Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();

 * while (it.hasNext()) {

 * Map.Entry<String, String> entry = it.next();

 * System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());

 * }

 *

 * //第三种:推荐,尤其是容量大时

 * System.out.println("通过Map.entrySet遍历key和value");

 * for (Map.Entry<String, String> entry : map.entrySet()) {

 * 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);

 * }

 * }

 */

 

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值