刷题常用之集合集合工具类详解

Posted !0 !

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷题常用之集合集合工具类详解相关的知识,希望对你有一定的参考价值。

目录

一、前言

集合在刷题中还是非常重要的,集合能够实现各种数据结构,比如:链表,栈,队列,树,哈希表等。能使我们写题的效率大大提高。下面我将介绍一下集合中常用的方法,因为这篇博客不是针对的基础知识,所以看这篇博客之前应该具备一些基本的集合知识,这样能更有利于利用集合中的方法。这篇博客可以当作手册一样,在做题时遇到不会的可以随时查询。

二、Collection接口常用方法

1、向集合中添加元素:boolean add(E e)

Collection c1 = new ArrayList();
c1.add("hello1");
c1.add("hello2");
c1.add("hello3");

2、返回集合中元素的个数:int size()

System.out.println(c1.size());//3;

3、判断当前集合是否为空:boolean isEmpty()

System.out.println(c1.isEmpty());//false;

4、返回遍历这个集合的迭代器对象:Iterator< E > iterator()

/*
*	boolean hasNext();	//返回当前迭代器中是否还有下一个对象 
*	Object next();		//获取迭代器中的下一个对象
*/
Iterator it = c1.iterator();
while(it.hasNext()) {	
    System.out.println(it.next() + " ");	//hello1 hello2 hello3;
}

5、把一个指定集合中的所有数据,添加到当前集合中:boolean addAll(Collection<? extends E> c)

Collection c2 = new ArrayList();
c2.add("hello4");
c2.add("hello5");
c1.addAll(c2);
Iterator it = c1.iterator();
while(it.hasNext()) {
    System.out.println(it.next() + " ");	//hello1 hello2 hello3 hello4 hello5;
}

6、判断当前集合中是否包含给定的对象:boolean contains(Object o)

System.out.println(c1.contains("hello1"));//true
System.out.println(c1.contains("hello7"));//false

7、判断当前集合中是否包含给定的集合的所有元素:boolean containsAll(Collection<?> c)

System.out.println(c1.containsAll(c2));//true

8、把给定的对象,在当前集合中删除:boolean remove(Object o)

c1.remove("hello7");//false;
c1.remove("hello4");//true;并删除"hello4"
Iterator it = c1.iterator();
while(it.hasNext()) {
    System.out.println(it.next() + " ");	//hello1 hello2 hello3 hello5;
}

9、把给定的集合中的所有元素,在当前集合中删除(删除两个集合的交集):boolean removeAll(Collection<?> c)

c1.remove(c2);//true;并删除"hello5";相当于删除两个集合的交集
Iterator it = c1.iterator();
while(it.hasNext()) {
    System.out.println(it.next() + " ");	//hello1 hello2 hello3;
}

10、判断俩个集合中是否有相同的元素,如果有当前集合只保留相同元素,如果没有当前集合元素清空(求两个集合的交集):boolean retainAll(Collection<?> c)

Collection c3 = new ArrayList();
c3.add("hello1");
c3.add("hello2");
c3.add("hello3");
Collection c4 = new ArrayList();
c4.add("hello1");
c4.add("hello4");
c4.add("hello5");
System.out.println(c3.retainAll(c4));//true;
Iterator it = c3.iterator();
while(it.hasNext()) {
    System.out.println(it.next());//hello1;

11、清空集合中所有的元素:void clear()

c3.clear();
System.out.println(c3.isEmpty());//true;

12、把集合中的元素,存储到数组中:Object[] toArray()

Collection c5 = new ArrayList();
c5.add("hello1");
c5.add("hello2");
c5.add("hello3");
Object[] a = c5.toArray();
for(Object i : a)
    System.out.print(i + " ");//hello1 hello2 hello3; 

13、把集合中的元素,存储到数组中,并指定数组的类型< T > T[] toArray(T[] a)

Object[] a1 = new Object[c5.size()];
c5.toArray(a1);
for(Object i : a1)
	System.out.println(i + " ");//hello1 hello2 hello3; 

三、List接口中常用方法(List可以调用Collection接口的方法)

1、将指定的元素,添加到该集合中的指定位置上:void add(int index, E element);

List list = new ArrayList();
//往尾部添加指定元素
list.add("hello1");
list.add("hello2");
list.add("hello3");
System.out.println(list);//[hello1, hello2, hello3];

2、移除列表中指定位置的元素, 并返回被移除的元素。:E remove(int index);

List list = new ArrayList();
//往尾部添加指定元素
list.add("hello1");
list.add("hello2");
list.add("hello3");
System.out.println(list.remove(2));//hello3;

3、返回集合中指定位置的元素:E get(int index);

List list = new ArrayList();
list.add("hello1");
list.add("hello2");
list.add("hello3");
System.out.println(list.get(1));//hello2;下标从0开始

4、用指定元素替换集合中指定位置的元素,并返回被替代的旧元素:E set(int index, E element);

List list = new ArrayList();
//往尾部添加指定元素
list.add("hello1");
list.add("hello2");
list.add("hello3");
list.set(2, "hello4");
System.out.println(list);//[hello1, hello2, hello4];

5、从指定位置开始,把另一个集合的所有元素添加进来 :boolean addAll(int index, Collection<? extends E> c);

List list = new ArrayList();
list.add("hello1");
list.add("hello2");
list.add("hello3");
List list1 = new ArrayList();
list.add("hello4");
list.add("hello5");
list.addAll(2, list1);
System.out.println(list);//[hello1, hello2, hello3, hello4, hello5];

6、查收指定元素在集合中的所有,从前往后查到的第一个元素(List集合可以重复存放数据):int indexOf(Object o);

List list = new ArrayList();
list.add("hello1");
list.add("hello2");
list.add("hello3");
System.out.println(list.indexOf("hello1"));//0;

7、查收指定元素在集合中的所有,从后往前查到的第一个元素(List集合可以重复存放数据):int lastIndexOf(Object o);

List list = new ArrayList();
list.add("hello1");
list.add("hello2");
list.add("hello3");
list.add("hello3");
System.out.println(list.lastIndexOf("hello3"));//3;

8、根据指定开始和结束位置,截取出集合中的一部分数据:List< E > subList(int fromIndex, int toIndex);

List list = new ArrayList();
list.add("hello1");
list.add("hello2");
list.add("hello3");
list.add("hello3");
List list1 = list.subList(1, 3);
System.out.println(list1);//[hello2, hello3];

四、List接口实现类 LinkedList

LinkedList不仅可以调用List接口的方法,还自己定义了一些方法。LinkedList最重要的作用是现实了栈和队列,还有双端队列,所以用来做数据结构的题还是非常方便的。

1、Queue

QueueDeque说明
add(e)addLast(e)向队尾插入元素,失败则抛出异常
offer(e)offerLast(e)向队尾插入元素,失败则返回false
remove()removeFirst()获取并删除队首元素,失败则抛出异常
poll()pollFirst()获取并删除队首元素,失败则返回null
element()getFirst()获取但不删除队首元素,失败则抛出异常
peek()peekFirst()获取但不删除队首元素,失败则返回null
Deque<Integer> queue = new LinkedList<>();
queue.add(1);
queue.addLast(2);
queue.offer(3);
queue.offerLast(4);
queue.offerLast(5);
for(Integer i : queue)
    System.out.print(i + " ");	//1 2 3 4 5;
System.out.println();
System.out.println(queue.remove());//1;
System.out.println(queue.removeFirst());//2;
System.out.println(queue.poll());//3;
System.out.println(queue.pollFirst());//4;
System.out.println(queue.element());//5;
System.out.println(queue.getFirst());//5;
System.out.println(queue.peek());//5;
System.out.println(queue.peekFirst());//5;

2、Stack

StackDeque说明
push(e)addFirst(e)向栈顶插入元素,失败则抛出异常
offerFirst(e)向栈顶插入元素,失败则返回false
pop()removrFirst()获取并删除栈顶元素,失败则抛出异常
pollFirst()获取并删除栈顶元素,失败则返回null
peek()peekFirst()获取但不删除栈顶元素,失败则返回null
Deque<Integer> stack = new LinkedList<>();
stack.push(1);
stack.addFirst(2);
stack.offerFirst(3);
stack.offerFirst(4);
for(Integer i : stack)
    System.out.print(i + " ");//4 3 2 1;
System.out.println(stack.pop());//4
System.out.println(stack.removeFirst());//3
System.out.println(stack.pollFirst());//2
System.out.println(stack.peek());//1
System.out.println(stack.peekFirst());//1

3、优先队列priorityQueue

priorityQueue是Queue接口的实现,可以对其中元素进行排序,可以放基本的包装类型或自定义的类,对于基本类型的包装类,优先队列中元素的默认排列顺序是升序,但是对于自定义类来说,需要自定义比较类PriorityQueue对元素采用的是堆排序,头是按指定排序方式的最小元素。堆排序只能保证根是最大(最小),整个堆并不是有序的。
方法iterator()中提供的迭代器可能只是对整个数组的依次遍历。也就只能保证数组的第一个元素是最小的 PriorityQueue的iterator()不保证以任何特定顺序遍历队列元素。若想按特定顺序遍历,先将队列转成数组,然后排序遍历。Arrays.sort(pq.toArray())

peek()//返回队首元素
poll()//返回队首元素,队首元素出队列
add()//添加元素
size()//返回队列元素个数
isEmpty()//判断队列是否为空,为空返回true,不空返回false

五、Set

Set一般是用来存储不重复,无序的数据,用的方法也基本上是Collection接口的方法,平时一般都是用的HashMap,所以这里就不多作介绍了。

六、Map

HashMap在刷题中还是经常能够用到的,他的特点是可以存储不重复的K-V键值对。LinkedHashMap使用则没那么广泛,常用的是实现LRU算法。

1、把key-value存到当前Map集合中:V put(K key, V value)

Map map = new HashMap();
map.put(1, "tom");

2、把指定map中的所有key-value,存到当前Map集合中:void putAll(Map<? extends K,? extends V> m)

Map map1 = new HashMap();
map1.put(2, "jack");
map1.put(3, "lucy");
map1.put(4, "mary");
map.putAll(map1);

3、当前Map集合中是否包含指定的key值:boolean containsKey(Object key)

System.out.println(map.containsKey(3));//true;

4、当前Map集合中是否包含指定的value值:boolean containsValue(Object value)

System.out.println(map.containsValue("jack"));//true;

5、返回当前Map集合中的元素个数(一对key-value,算一个元素数据):int size()

6、清空当前Map集合中的所有数据:void clear()

7、判断当前Map集合是否为空 :boolean isEmpty()

map.clear();
System.out.println(map.size());//0;	
System.out.println(map.isEmpty());//true;

8、在当前Map集合中,通过指定的key值,获取对应的value:V get(Object key)

Map map2 = new HashMap();
map2.put(1, "tom");
map2.put(2, "jack");
map2.put(3, "lucy");
map2.put(4, "mary");
System.out.println(map2.get(3));//lucy;

9、在当前Map集合中,移除指定key及其对应的value:V remove(Object key)

System.out.println(map2.remove(3));//lucy;

10、返回Map集合中所有的key值 :Set< K > keySet()

Set keys &#

以上是关于刷题常用之集合集合工具类详解的主要内容,如果未能解决你的问题,请参考以下文章

刷题常用之包装类(Integer常用方法属性以及与String转换进制转换)

Java常用类之集合工具类Collections

集合详解之 Map

集合详解之HashMap

集合详解之 Collection

Java集合之Collections 剖析