Java-(1)笔试必备之容器的使用

一、方法说明

1、java.util.Collection接口

共性的方法:

  public boolean add(E e):  把给定的对象添加到当前集合中 。
  public void clear() :清空集合中所有的元素。
  public boolean remove(E e): 把给定的对象在当前集合中删除。
  public boolean contains(E e): 判断当前集合中是否包含给定的对象。
  public boolean isEmpty(): 判断当前集合是否为空。
  public int size(): 返回集合中元素的个数。
  public Object[] toArray(): 把集合中的元素,存储到数组中。

2、java.util.List接口

(1)java.util.ArrayList

 - public void add(int index, E element): 将指定的元素,添加到该集合中的指定位置上。
 - public E get(int index):返回集合中指定位置的元素。
 - public E remove(int index): 移除列表中指定位置的元素, 返回的是被移除的元素。
 - public E set(int index, E element):用指定元素替换集合中指定位置的元素,返回值的更新前的元素。
- java.utils.Collections是集合工具类,用来对集合进行操作。部分方法如下:
   - public static <T> boolean addAll(Collection<T> c, T... elements):往集合中添加一些元素。
   如:Collections.addAll(list,"a","b","c","d","e");
   
   - public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序。
   - public static <T> void sort(List<T> list,Comparator<? super T> ):将集合中元素按照指定规则排序。
   如:
      Collections.sort(list02, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //按照年龄升序排序
                int result =  o1.getAge()-o2.getAge();
                //如果两个人年龄相同,再使用姓名的第一个字比较
                if(result==0){
                    result =  o1.getName().charAt(0)-o2.getName().charAt(0);
                }
                return  result;
            }

        });

(2)java.util.LinkedList

 - public void addFirst(E e):将指定元素插入此列表的开头。
 - public void addLast(E e):将指定元素添加到此列表的结尾。
 - public void push(E e):将元素推入此列表所表示的堆栈。
 - public E getFirst():返回此列表的第一个元素。
 - public E getLast():返回此列表的最后一个元素。
 - public E removeFirst():移除并返回此列表的第一个元素。
 - public E removeLast():移除并返回此列表的最后一个元素。
 - public E pop():从此列表所表示的堆栈处弹出一个元素。
 - public boolean isEmpty():如果列表不包含元素,则返回true

3、java.util.Map<k,v>集合

- public boolean containsKey(Object key) 判断集合中是否包含指定的键。包含返回true,不包含返回false
- public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
  返回值:
       * key存在,返回对应的value值;  
       * key不存在,返回null
- public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
  返回值:V
       * key存在,v返回被删除的值
       * key不存在,v返回null
- public V put(K key, V value):  把指定的键与指定的值添加到Map集合中。
   返回值:v
       * 存储键值对的时候,key不重复,返回值V是null
       * 存储键值对的时候,key重复,会使用新的value替换map中重复的value,返回被替换的value值
- public Set<K> keySet() 返回此映射中包含的键的 Set 视图。
        如:Set<String> set = map.keySet();
- public Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 Set 视图。
        如:Set<Map.Entry<String, Integer>> set = map.entrySet();

二、程序示例

1、队列Queue

Queue<Integer> queue = new LinkedList<>();
        //推荐,Inserts the specified element into this queue,the tail of this queue
        queue.add(0);

        //Retrieves, but does not remove the head of this queue.it throws an exception if this queue is empty.
        queue.element();

        //同add
        queue.offer(1);

        //Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
        queue.peek();

        //Retrieves and removes the head of this queue, or returns null if this queue is empty.
        queue.poll();

        //Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.
        queue.remove();

2、双端队列Deque

Deque<Integer> deque = new LinkedList<>();
        //同addLast
        deque.add(0);
        //Inserts the specified element at the front of this deque
        deque.addFirst(1);
        //Inserts the specified element at the end of this deque
        deque.addLast(2);
        deque.getFirst();
        deque.getLast();
        deque.remove();
        //Retrieves and removes the first element of this deque. it throws an exception if this deque is empty.
        deque.removeFirst();
        deque.removeLast();

        //同add,offerLast
        deque.offer(3);
        //不推荐使用,Inserts the specified element at the front of this deque
        deque.offerFirst(4);
        deque.offerLast(5);
        //Retrieves, but does not remove the first element of this deque
        deque.peek();
        //同peek
        deque.peekFirst();
        deque.peekLast();
        //Retrieves and removes the head of the queue,returns null if this deque is empty.
        deque.poll();
        deque.pollFirst();
        deque.pollLast();

        //同 removeFirst(),removes and returns the first element of this deque
        deque.pop();
        //同addFirst,头部插入
        deque.push(6);

总结来说,队列推荐使用add, peek, poll, 堆栈推荐使用pop,peek,push

3、列表

A、ArrayList<>()

List<String> list = new ArrayList<>();
        //Appends the specified element to the end of this list
        list.add("a");
        list.add(1, "b");
        //Appends all of the elements in the specified collection to the end of this list
        list.addAll(Arrays.asList("c","d","e"));
        //Returns the element at the specified position in this list.
        list.get(1);
        //Replaces the element at the specified position in this list with the specified element
        list.set(1, "x");
        list.indexOf("a");
        list.lastIndexOf("a");
        list.remove("a");
        list.remove(0);
        list.contains("b");
        //Returns an array containing all of the elements in this list
        list.toArray();

        Set<String> listSet = new HashSet<>(list);

B、LinkedList<>()

LinkedList<String> linkedlist = new LinkedList<>();
        linkedlist.add("a");
        linkedlist.add(1, "b");
        linkedlist.contains("a");
        linkedlist.addAll(Arrays.asList("c", "d", "e"));
        linkedlist.get(0);
        linkedlist.set(3, "f");
        linkedlist.indexOf("a");
        linkedlist.lastIndexOf("e");
        linkedlist.remove("a");
        linkedlist.remove(1);
        linkedlist.toArray();
        /**
         * 还有更多方法,LinkedList实现了List和的Deque接口
         */

4、集合

Set<String> set = new HashSet<>();
        set.add("a");
        set.contains("a");
        set.addAll(Arrays.asList("b","c","d"));
        set.remove("a");
        set.clear();

5、字典

Map<Integer,String> map = new HashMap<>();
        map.put(0, "a");
        map.put(1, "b");
        map.put(2, "c");
        map.putIfAbsent(3, "e");
        map.get(0);
        map.containsKey(0);
        map.containsValue("a");
        map.remove(0);
        map.remove(0, "a");
        map.replace(0, "d");
        map.keySet();

        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        for(Map.Entry<Integer, String> entry:entrySet){
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书生伯言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值