首页javamapJava Collection - 如何使用集合跟踪插入的顺序

Java Collection - 如何使用集合跟踪插入的顺序

我们想知道如何使用集合跟踪插入的顺序。
import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    Map<String, Integer> map = new LinkedHashMap<>();

    map.put("Zero", 0);
    map.put("One", 1);
    map.put("Two", 2);
    map.put("Three", 3);
    map.put("Four", 4);

    for (Map.Entry<String, Integer> entry : map.entrySet()) {
      System.out.println(entry.getKey() + " => " + entry.getValue());
    }
  }
}