Home  >  Article  >  Java  >  Detailed explanation of list, set, map traversal and enhanced for loop code examples in Java

Detailed explanation of list, set, map traversal and enhanced for loop code examples in Java

黄舟
黄舟Original
2017-03-08 10:46:591639browse

This article mainly introduces the detailed explanation of list, set, map traversal and enhanced for loop in Java. Friends who need it can refer to the following

Detailed explanation of list, set, map in Java Traversal and enhanced for loop

The Java collection class can be divided into three blocks, namely List, Set extended from the Collection interface, and Map type collections stored in the form of key-value pairs.

Regarding the enhanced for loop, it should be noted that the array subscript value cannot be accessed using the enhanced for loop, and the related methods of Iterator are also used internally for traversing the collection. If you only do simple traversal reading, the enhanced for loop will indeed reduce the amount of code.

Concept of collection:

1. Function: used to store objects
2. Equivalent to a container, which contains a group of objects, among which Each object appears as an element of the collection
3.Java's container has collection classes and arrays, the difference is

The difference and its commonly used implementation classes

List interface:

The ordered elements of the list can be repeated

Implementation class:ArrayList:Dynamic array list

LinkedList: Doubly linked list

Set interface:

The set is unordered and the elements cannot be repeated

Implementation class: HashSet: Hash set

TreeSet: Internal sorting of tree set

Map interface:

Store data in the form of key-value pairs - keys are not allowed to be repeated

Implementation class: HashSet: Hash set

TreeSet: Inside the tree set Sorting

The collection classes appearing in JDK1.0 are all thread-safe, but the efficiency is low

The collection classes appearing in JDK1.2 are not thread-safe, but the efficiency is low High

The code example is as follows:

import java.util.ArrayList; 
import java.util.HashSet; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Set; 
 
public class ListAndSet{ 
 
  public static void main(String[] args) { 
    setTest(); 
    listTest();
  } 
  // 遍历Set集合
  private static void setTest() { 
    Set<string> set = new HashSet<string>(); 
    set.add("A"); 
    set.add("B"); 
    set.add("C");  
    set.add("D"); 
    set.add("E"); 
 
    //set集合遍历方法1:使用iterator 
    Iterator<string> it = set.iterator(); 
    while (it.hasNext()) { 
      String value = it.next(); 
      System.out.println(value); 
    } 
 
    //set集合遍历方法2:使用增强for循环。 
    for(String s: set){ 
      System.out.println(s); 
    } 
  } 
 
  // 遍历list集合 
  private static void listTest() { 
    List<string> list = new ArrayList<string>(); 
    list.add("111"); 
    list.add("222"); 
    list.add("333"); 
    list.add("444"); 
    list.add("555"); 
 
    // 遍历方式1:使用iterator 
    Iterator<string> it = list.iterator(); 
    while (it.hasNext()) { 
      String value = it.next(); 
      System.out.println(value); 
    } 
 
    // 遍历方法2:使用传统for循环进行遍历。 
    for (int i = 0, size = list.size(); i < size; i++) { 
      String value = list.get(i); 
      System.out.println(value); 
    } 
 
    // 遍历方法3:使用增强for循环进行遍历。 
    for (String value : list) { 
      System.out.println(value); 
    } 
  } 
} 
 
//关于Map类型集合的遍历,keySet()与entrySet()方法
//增强For循环 
public class Map{ 
 
  public static void main(String[] args) { 
    // 创建一个HashMap对象,并加入了一些键值对。 
    Map<string, string=""> maps = new HashMap<string, string="">(); 
    maps.put("111", "111"); 
    maps.put("222", "222"); 
    maps.put("333", "333"); 
    maps.put("444", "444"); 
    maps.put("555", "555"); 
 
    // 传统的遍历map集合的方法1; keySet() 
    //traditionalMethod1(maps); 
    // 传统的遍历map集合的方法2; entrySet() 
    //traditionalMethod2(maps); 
    // 使用增强For循环来遍历map集合方法1; keySet() 
    //strongForMethod1(maps); 
    // 使用增强For循环来遍历map集合方法2; entrySet() 
    strongForMethod2(maps); 
  } 
 
  private static void strongForMethod2(Map<string, string=""> maps) { 
    Set<entry<string, string="">> set = maps.entrySet(); 
    for (Entry<string, string=""> entry : set) { 
      String key = entry.getKey(); 
      String value = entry.getValue(); 
      System.out.println(key + " : " + value); 
    } 
  } 
 
  private static void strongForMethod1(Map<string, string=""> maps) { 
    Set<string> set = maps.keySet(); 
    for (String s : set) { 
      String key = s; 
      String value = maps.get(s); 
      System.out.println(key + " : " + value); 
    } 
  } 
 
  // 使用entrySet()方法,获取maps集合中的每一个键值对, 
  private static void traditionalMethod2(Map<string, string=""> maps) { 
    Set<map.entry<string, string="">> sets = maps.entrySet(); 
    // 取得迭代器遍历出对应的值。 
    Iterator<entry<string, string="">> it = sets.iterator(); 
    while (it.hasNext()) { 
      Map.Entry<string, string=""> entry = (Entry<string, string="">) it.next(); 
      String key = entry.getKey(); 
      String value = entry.getValue(); 
      System.out.println(key + " : " + value); 
    } 
  } 
 
  // 使用keySet()方法,获取maps集合中的所有键,遍历键取得所对应的值。 
  private static void traditionalMethod1(Map<string, string=""> maps) { 
    Set<string> sets = maps.keySet(); 
    // 取得迭代器遍历出对应的值。 
    Iterator<string> it = sets.iterator(); 
    while (it.hasNext()) { 
      String key = it.next(); 
      String value = maps.get(key); 
      System.out.println(key + " : " + value); 
    } 
  } 
}


The above is the detailed content of Detailed explanation of list, set, map traversal and enhanced for loop code examples in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn