📜  JavaList、Set 和 Map 的区别

📅  最后修改于: 2021-09-11 03:53:31             🧑  作者: Mango

Java中的List接口是Java集合接口的一个子接口。它包含基于索引的方法来插入、更新、删除和搜索元素。它也可以有重复的元素。我们还可以将空元素存储在列表中。 List 保留插入顺序,它允许元素的位置访问和插入。它在Java.util 包中找到。让我们考虑一个示例,以便更好地理解您将看到如何使用Java的列表接口添加元素

例子:

Java
// Java Program to illustrate the
// addition of elements in a List
  
import java.util.*;
public class GFG {
  
    public static void main(String args[])
    {
  
        // Creating a List
        List al = new ArrayList<>();
  
        // Adding elements in the List
        al.add("mango");
        al.add("orange");
        al.add("Grapes");
  
        // Iterating the List
        // element using for-each loop
        for (String fruit : al)
            System.out.println(fruit);
    }
}


Java
// A Java program to demonstrate a Set.
// Here, you will see how you can add 
// Elements using Set.
  
import java.util.*;
  
public class SetExample {
    
    public static void main(String[] args)
    {
        // Set demonstration using HashSet
        Set Set = new HashSet();
          
        // Adding Elements  
        Set.add("one");
        Set.add("two");
        Set.add("three");
        Set.add("four");
        Set.add("five");
          
        // Set follows unordered way. 
        System.out.println(Set);
    }
}


Java
// A sample prrogram to demonstrate Map.
  
// Here, you will see how you
// can add elements using Map
import java.util.*;
  
class MapExample {
  
    public static void main(String args[])
    {
  
        // Creating object for Map.
        Map map
            = new HashMap();
  
        // Adding Elements using Map.
        map.put(100, "Amit");
        map.put(101, "Vijay");
        map.put(102, "Rahul");
  
        // Elements can traverse in any order
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + " "
                               + m.getValue());
        }
    }
}


输出 :

mango
orange
Grapes

Set遵循无序方式,它位于Java.util包中,并扩展了Java的集合接口。重复项将在 Set 中被忽略,并且不会在最终输出中打印。让我们考虑一个示例,以便更好地理解您将看到如何使用Javaset 接口添加元素。我们来看一下。

例子:

Java

// A Java program to demonstrate a Set.
// Here, you will see how you can add 
// Elements using Set.
  
import java.util.*;
  
public class SetExample {
    
    public static void main(String[] args)
    {
        // Set demonstration using HashSet
        Set Set = new HashSet();
          
        // Adding Elements  
        Set.add("one");
        Set.add("two");
        Set.add("three");
        Set.add("four");
        Set.add("five");
          
        // Set follows unordered way. 
        System.out.println(Set);
    }
}

输出 :

[four, one, two, three, five]

Java Map 接口, Java.util.Map 表示键和值之间的映射。更具体地说, Java Map 可以存储键和值对。每个键都链接到一个特定的值。存储在 Map 中后,您可以稍后仅使用键查找值。让我们考虑一个示例,以便更好地理解您将看到如何使用Java的 Map 接口添加元素。我们来看一下。

例子:

Java

// A sample prrogram to demonstrate Map.
  
// Here, you will see how you
// can add elements using Map
import java.util.*;
  
class MapExample {
  
    public static void main(String args[])
    {
  
        // Creating object for Map.
        Map map
            = new HashMap();
  
        // Adding Elements using Map.
        map.put(100, "Amit");
        map.put(101, "Vijay");
        map.put(102, "Rahul");
  
        // Elements can traverse in any order
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + " "
                               + m.getValue());
        }
    }
}

输出 :

100 Amit
101 Vijay
102 Rahul

JavaList、 Set和 Map 的区别

List

Set

Map

The list interface allows duplicate elements

Set does not allow duplicate elements.

The map does not allow duplicate elements

The list maintains insertion order.

Set do not maintain any insertion order. 

The map also does not maintain any insertion order. 

We can add any number of null values.

But in set almost only one null value.

The map allows a single null key at most and any number of null values.

List implementation classes are Array List, LinkedList.

Set implementation classes are HashSet, LinkedHashSet, and TreeSet. 

Map implementation classes are HashMap, HashTable, TreeMap, ConcurrentHashMap, and LinkedHashMap.

The list provides get() method to get the element at a specified index.

Set does not provide get method to get the elements at a specified index

The map does not  provide get method to get the elements at a specified index

If you need to access the elements frequently by using the index then we can use the list

If you want to create a collection of unique elements then we can use set

If you want to store the data in the form of key/value pair then we can use the map.

To traverse the list elements by using Listlterator.

Iterator can be used traverse the set elements

Through keyset, value, and entry set.