Harbin KK's growth path-collection framework

Understanding collections and common methods

Recognition set

The Java collection framework mainly includes two types of containers, one is a collection, which stores a collection of elements, and the other is a map, which stores key/value pair mappings. The Collection interface has three subtypes, List, Set, and Queue, followed by some abstract classes, and finally concrete implementation classes. Commonly used are ArrayList , LinkedList HashSet , LinkedHashSet, HashMap , LinkedHashMap and so on.

	以下是集合框架体系图

Collective framework system

ArrayList

Common method
Common method

How to traverse and output the elements in an ArrayList collection is a very important and frequently used method


ArrayList alist = new ArrayList();
alist.add(31);
alist.add("rapper虫儿飞")
alist.add("哦哈")
//循环遍历
for(int i=0;i<alist.size();i++){
    
    
	System.out.print(alist.get(i)+"\t");
}
System.out.println("--------------------------");

//增强for 循环
for (Object o:alist){
    
    
	System.out.print(o)
}
System.out.println("--------------------------");

//迭代器
Iterator it=alist.iterator();
while(it.hasNext()){
    
    
	System.out.print(it.next()+"\t");
}

LinkedList

存储原理是一个链表,在元素的前后分别有一个前置结点和后置结点,用于连接集合中的上一个元素和下一个元素,依次构成一条链式数据的集合。

LinkedList相比较于ArrayList特有的方法如下
void addFirst(Object o)  给指定元素插入当前集合头部
void addLastt(Object o)  给指定元素插入当前集合尾部
···  getFirst()  获取当前集合的第一个元素
···  getLast()  获取当前集合的最后一个元素
···  removeFirst() 移除并返回当前集合的第一个元素
···  removeLast() 移除并返回当前集合的最后一个元素

The method of traversing the output LinkedList is the same as that of ArrayList

Set

set接口的特点:存储一组唯一(不允许出现重复的元素),无序(没有index下标的)的对象,HashSet是Set接口常用的实现类。
HashSet set=new HashSet();

set.add("ni");
set.add(22);
//没有修改数据的方法

//删除数据
set.remove(22);
//只能获取所有元素--只能用增强for循环和迭代器Iterator输出
for (Object o:set){
    
    
	System.out.println(o)
}

Iterator it =set.iterator();
while(it.hasNext()){
    
    
	System.out.println(it.next());
}

JDK9 version adds new features

This new feature can add multiple elements to the collection at once, which facilitates the usual operation of adding elements.

/*JDK9的新特性:
        List接口,Set接口,Map接口:里边增加了一个静态的方法of,可以给集合一次性添加多个元素
        static <E> List<E> of?(E... elements)
        使用前提:
            当集合中存储的元素的个数已经确定了,不在改变时使用
     注意:
        1.of方法只适用于List接口,Set接口,Map接口,不适用于接接口的实现类
        2.of方法的返回值是一个不能改变的集合,集合不能再使用add,put方法添加元素,会抛出异常
        3.Set接口和Map接口在调用of方法的时候,不能有重复的元素,否则会抛出异常
*/




  public static void main(String[] args) {
    
    
        List<String> list = List.of("a", "b", "a", "c", "d");
        System.out.println(list);//[a, b, a, c, d]
        //list.add("w");//UnsupportedOperationException:不支持操作异常
         //Set<String> set = Set.of("a", "b", "a", "c", "d");//IllegalArgumentException:非法参数异常,有重复的元素
        Set<String> set = Set.of("a", "b", "c", "d");
        System.out.println(set);
        //set.add("w");//UnsupportedOperationException:不支持操作异常
         //Map<String, Integer> map = Map.of("张三", 18, "李四", 19, "王五", 20,"张三",19);IllegalArgumentException:非法参数异常,有重复的元素
        Map<String, Integer> map = Map.of("张三", 18, "李四", 19, "王五", 20);
        System.out.println(map);//{王五=20, 李四=19, 张三=18}
        //map.put("赵四",30);//UnsupportedOperationException:不支持操作异常
    }

Thanks for reading evbd! ! ! Hope that helps.

Guess you like

Origin blog.csdn.net/serenty111/article/details/109403349