java collection-basic

Note: This picture only shows some collection classes that are most commonly used in work, excluding all collection classes.
As you can see from the above collection framework diagram, the Java collection framework mainly includes two types of containers, one is a collection, which stores a collection of elements, which is a single-column collection, and the other is a map, which stores keys/ Value pair mapping is a combination of two columns.
The Collection interface has 3 subtypes, List, Set, and Queue, followed by some abstract classes, and finally concrete implementation classes. Commonly used are ArrayList, LinkedList, HashSet, LinkedHashSet, HashMap, LinkedHashMap, etc.

The difference between Set and List
1. The Set interface instance stores unordered, non-repetitive data. The List interface instance stores ordered, repeatable elements.
• 2. Set retrieval efficiency is low, deletion and insertion efficiency is high, insertion and deletion will not cause element position change <implementation class has HashSet, TreeSet>.
• 3. List is similar to array, which can grow dynamically, and automatically grow the length of List according to the length of the actual stored data. Finding elements is efficient, inserting and deleting are inefficient, because it will cause other element positions to change <implementation classes are ArrayList, LinkedList, Vector>.

Ways to traverse the collection:

Traverse the ArrayList:

public class TestCollection {
    
    

	List<String> list=new ArrayList<String>();
	list.add("Hello");
    list.add("World");
    list.add("HAHAHAHA");
    //第一种遍历方法使用 For-Each 遍历 List
    for (String str : list) {
    
                //也可以改写 for(int i=0;i<list.size();i++) 这种形式
       System.out.println(str);
    }

    //第二种遍历,把链表变为数组相关的内容进行遍历
    String[] strArray=new String[list.size()];
    list.toArray(strArray);
    for(int i=0;i<strArray.length;i++) //这里也可以改写为  for(String str:strArray) 这种形式
    {
    
    
       System.out.println(strArray[i]);
    }
    
   //第三种遍历 使用迭代器进行相关遍历
    Iterator<String> ite=list.iterator();
    while(ite.hasNext())//判断下一个元素之后有值
    {
    
    
        System.out.println(ite.next());
    }
 }
}

Traverse the Map:

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

Welcome to leave a message to discuss!

Guess you like

Origin blog.csdn.net/weixin_42409759/article/details/107300605