List、Map、Set的区别与联系
一、结构特点

  1. ListSet是单列数据,Map是键值对;<br /> List中存储的数据是有顺序的,并且值允许重复;<br /> Set中存储的数据是无顺序的,并且不允许重复,但元素在集合中的位置是由元素的hashcode决定,即位置是固定的<br /> Map中存储的数据是无序的,它的键是不允许重复的,但是值是允许重复的;

ArrayList使用动态数组存储元素,便于储存和获取元素

import java.util.ArrayList

ArrayList<Integer> myList = new ArrayList<Integer>();

// Create an ArrayList with a capacity of 12:
ArrayList<String> months = new ArrayList<String>(12);
ArrayList<String> months = new ArrayList<String>(12);
months.add("January");
System.out.println(months): // Prints: [January]


ArrayList<String> months = new ArrayList<String>(12);
months.add("January");
months.add("February");
months.add("June");
// Change the value at index 2:
months.add(2, "March");
System.out.println(months); // Prints: [January, February, March, June]
ArrayList<String> months = new ArrayList<String>(12);
months.add("January");
months.add("February");
months.add("March");
System.out.println(months.get(1)); // Prints: February
ArrayList<String> months = new ArrayList<String>(12);
months.add("January");
months.add("February");
months.add("March");
months.remove(1);
System.out.println(months); // Prints: [January, March]
-----------------------------------------------------------------
ArrayList<String> months = new ArrayList<String>(12);
months.add("January");
months.add("February");
months.add("March");
months.remove("February");
System.out.println(months); // Prints: [January, March]

LinkedList便于操作元素

LinkedList<String> days = new LinkedList<String>();
LinkedList<String> days = new LinkedList<String>();
days.add("Monday");
days.add("Tuesday");
System.out.println(days); // Prints: [Monday, Tuesday]

LinkedList<String> days = new LinkedList<String>();
days.add("Monday");
days.add("Tuesday");
days.add(0, "Sunday");
System.out.println(days); // Prints: [Sunday, Monday, Tuesday]
LinkedList<String> days = new LinkedList<String>();
days.add("Monday");
days.add("Tuesday");
days.add(0, "Sunday");  //sunday加到第一个位置
System.out.println(days.get(1)); // Prints: Monday

.getFirst() 
.getLast()
LinkedList<String> days = new LinkedList<String>();
days.add("Monday");
days.add("Tuesday");
days.add(0, "Sunday"); 
days.remove(1);
System.out.println(days); // Prints: [Sunday, Tuesday]

LinkedList<String> days = new LinkedList<String>();
days.add("Monday");
days.add("Tuesday");
days.add(0, "Sunday");
days.remove("Monday");
System.out.println(day); // Prints: [Sunday, Tuesday]

LinkedList<String> days = new LinkedList<String>();
days.add("Monday");
days.add("Tuesday");
days.add(0, "Sunday");
days.remove();    //移除第一个元素
System.out.println(days); // Prints: [Monday, Tuesday]

HashMap具有键值对

import java.util.HashMap;

// Declare a HashMap with String keys and Integer values:
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();

// Add key-value pairs to HashMap:
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Output a HashMap:
System.out.println(teaSteepingTemp); // Prints: {Oolong=185, Rooibos=212}
int oolongTemp = teaSteepingTemp.get("Oolong");
System.out.println(oolongTemp);  // Prints: 185
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Remove an item:
teaSteepingTemp.remove("Oolong");
System.out.println(teaSteepingTemp); // Prints: {Rooibos=212}

------------------------------------------
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Remove all items:
teaSteepingTemp.clear();
System.out.println(teaSteepingTemp); // Prints: {}
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Output the size of a HashMap:
System.out.println(teaSteepingTemp.size()); // Prints: 2
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Iterate through the HashMap:
for (String key : teaSteepingTemp.keySet()) {
  System.out.println("Brew " + key + " tea at " + teaSteepingTemp.get(key) + "°F");
}
/*
Prints:
Brew Oolong tea at 185°F
Brew Rooibos tea at 212°F
*/
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Check for keys:
System.out.println(teaSteepingTemp.containsKey("Oolong")); // Prints: true
System.out.println(teaSteepingTemp.containsKey("Green")); // Prints: false
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Replace a value:    
teaSteepingTemp.replace("Rooibos", 245);
System.out.println(teaSteepingTemp.get("Rooibos")); // Prints: 245
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Output only the keys:
System.out.println(teaSteepingTemp.keySet()); // Prints: [Oolong, Rooibos]
HashMap<String, Integer> teaSteepingTemp = new HashMap<>();
teaSteepingTemp.put("Oolong", 185);
teaSteepingTemp.put("Rooibos", 212);

// Output only the values:
System.out.println(teaSteepingTemp.values()); // Prints: [185, 212]

Set

HashSet

HashSet使用HashMap实例实现。
HashSet中存储的元素的顺序是随机的。
HashSet能够存储空值。
HashSet的.add()、.remove()和.contains()方法有O(1)运行时。
HashSets通常比其他两个选项运行得更快。

TreeSet

TreeSet是使用TreeMap实例实现的。
元素的顺序是使用比较器确定的(例如,数字或字母顺序)。
TreeSet不能存储空值。
TreeSet的运行时间较慢,.add()、.remove()和.contains()方法的运行时间为O(log n)。

LinkedHashSet

LinkedHashSet是使用双链表和哈希表实现的。
元素的存储顺序由添加元素的顺序决定。
LinkedHashSet能够存储空值。
LinkedHashSet的.add()、.remove()和.contains()方法的运行时间为O(1),但运行时间比HashSet略慢。

// Import Set class:
import java.util.Set;

// Import HashSet class:
import java.util.HashSet;

// Import TreeSet class:
import java.util.TreeSet;

// Import LinkedHashSet class:
import java.util.LinkedHashSet;

// Import all classes:
import java.util.*;


// Create a HashSet that will contain String values:
Set<String> colors = new HashSet<String>();

// Create a TreeSet that will contain Integer values:
TreeSet<Integer> myNumbers = new TreeSet<Integer>();
Set<String> colors = new HashSet<String>();

// Add items to Set:
colors.add("red");
colors.add("blue");
colors.add("blue"); // Duplicate value

System.out.println(colors); // Prints: [red, blue]
Set<String> colors = new HashSet<String>();
colors.add("red");
colors.add("blue");

// Remove an item:
colors.remove("red");
System.out.println(colors); // Prints: [blue]
Set<String> colors = new HashSet<String>();
colors.add("red");
colors.add("blue");

// Check for items:
System.out.println(colors.contains("red")); // Prints: true
System.out.println(colors.contains("green")); // Prints: false
Set<String> colors = new HashSet<String>();
colors.add("red");
colors.add("blue");

// Find size of Set
System.out.println(colors.size()); // Prints: 2
Set<String> colors = new HashSet<String>();
colors.add("red");
colors.add("blue");

// Iterate through the Set:
for (String item: colors) {
  System.out.println(item);
}
/*
Prints:
red
blue
*/
Set<String> colors = new HashSet<String>();
colors.add("red");
colors.add("orange");

Set<String> primary = new HashSet<String>();
primary.add("red");
primary.add("yellow");

// Modify colors to store a union between colors set and primary set:
colors.addAll(primary);
System.out.println(colors); // Prints: [red, orange, yellow]
Set<String> colors = new HashSet<String>();
colors.add("red");
colors.add("orange");

Set<String> primary = new HashSet<String>();
primary.add("red");
primary.add("yellow");

// Modify colors to store the intersection between colors set and primary set:
colors.retainAll(primary);
System.out.println(colors); // Prints: [red]
Set<String> colors = new HashSet<String>();
colors.add("red");
colors.add("orange");

Set<String> primary = new HashSet<String>();
primary.add("red");
primary.add("yellow");

// Modify colors to store the complement between colors set and primary set:
colors.removeAll(primary);
System.out.println(colors); // Prints: [orange]