列表与地图界面 (List vs Map interface)
Here, we will see how List differs from Map interface in Java and we will see the points given below,
在这里,我们将看到List与Java中的Map接口有何不同,并且我们将看到以下几点,
列表界面 (List interface)
List is an interface that is defined in java.util package.
List是在java.util包中定义的接口。
List is the data structure in Java.
List是Java中的数据结构。
List object is represented in the form of values.
列表对象以值的形式表示。
The performance of List interface is low as compare to Map interface.
List界面的性能比Map界面低。
The implementation class of List interface is ArrayList, LinkedList, and Vector and Stack, etc.
List接口的实现类是ArrayList,LinkedList和Vector and Stack等。
List is not different from Collection or in other words, there is a relation between List and Collection (i.e. It is a child interface of Collection interface because List implements Collection interface).
List与Collection没有区别,换句话说,List与Collection之间存在关系(即,它是Collection接口的子接口,因为List实现了Collection接口)。
List does not provide uniqueness (i.e. Duplicates are allowed for values or we can insert one object multiple times).
列表不提供唯一性(即值允许重复,或者我们可以多次插入一个对象)。
If we want to represent a group of individual objects where “insertion order is preserved” (i.e. the order of insertion is must be same as the order of retrieval).
如果我们要表示一组单独的对象,其中“插入顺序被保留”(即插入顺序必须与检索顺序相同)。
We should go for List if we want to represent a group of the object as a single entity.
如果我们想将一组对象表示为单个实体,则应使用List。
List is meant for a group of the individual object.
列表用于一组单独的对象。
Example:
例:
Let suppose we have a List with few elements. Here we are adding the elements in the order is [10,20,30,50, null,30] and if we are retrieving the elements so the order of retrieving elements must be the same (i.e. it is needed to be the same insertion and retrieval order of the elements.) so the output will be the same and the order will be like [10,20,30, null,30].
假设我们有一个包含很少元素的列表。 在这里,我们按[10,20,30,50,null,30]的顺序添加元素,如果我们要检索元素,则检索元素的顺序必须相同(即,需要相同的插入以及元素的检索顺序。)因此输出将是相同的,顺序将类似于[10,20,30,null,30]。
// Java program to demonstrate the behavior of List interface
import java.util.*;
class ListInterface {
public static void main(String[] args) {
// Creating an instance
List list = new ArrayList();
// By using add() method to add an elements
list.add(10);
list.add(20);
list.add(30);
list.add(50);
list.add(null);
// if we add again 30 then we will not get any error
// because duplicate element is allowed
list.add(30);
// Display List elements
System.out.println("Retrieval order of the elements in List is :" + list);
}
}
Output
输出量
E:\Programs>javac ListInterface.java
E:\Programs>java ListInterface
Retrieval order of the elements in List is :[10, 20, 30, 50, null, 30]
Now, we will see how Map differs from List interface in Java and we will see the points given below,
现在,我们将看到Map与Java中的List接口有何不同 ,我们将看到以下几点,
地图界面 (Map interface)
The Map is an interface that is defined in java.util package.
Map是在java.util包中定义的接口。
The Map is the data structure in Java.
Map是Java中的数据结构。
The Map is based on Hashing and The Map object is represented in the form of key-value pairs and the key-value pairs are called entry.
Map基于散列,并且Map对象以键值对的形式表示,而键值对称为entry。
The performance of Map interface is high as compare to Set interface.
Map界面的性能比Set界面高。
In the case of Map interface, there is no collision concept if we know keys.
在Map接口的情况下,如果我们知道按键,则不会有冲突概念。
The implementation class of Map interface is HashMap, LinkedHashMap, and ConcurrentHashMap, etc.
Map接口的实现类是HashMap,LinkedHashMap和ConcurrentHashMap等。
The Map is different from Collection or in other words, there is no relation between Map and Collection (i.e. It is not a child interface of Collection interface because Map does not implement Collection interface).
Map与Collection不同,换句话说,Map与Collection之间没有关系(即,它不是Collection接口的子接口,因为Map未实现Collection接口。
The Map does not provide uniqueness fully (i.e. Duplicates are not allowed for Keys and Duplicates are allowed for values).
映射不能完全提供唯一性(即,键不允许重复,值不允许重复)。
We should go for Map if we want to represent a group of the object as key-value pairs.
如果我们想将一组对象表示为键值对,则应该使用Map。
The Map is meant for a group of key-value pairs.
该映射用于一组键值对。
Example:
例:
Let suppose we have a Map with few elements. Here we are adding the elements in the order is {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000,null=null, Django=null, null=7000} and if we are retrieving the elements so the order of retrieving elements can be different (i.e. insertion order is not preserved and it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like {Ruby=4000, C=2000, Django=null, Python=1000, C++=3000, null=7000, Java=1000}
假设我们有一个包含很少元素的Map。 在这里,我们按照{Java = 1000,C = 2000,C ++ = 3000,Ruby = 4000,Python = 1000,null = null,Django = null,null = 7000}的顺序添加元素,如果我们要检索元素因此检索元素的顺序可以不同(即不保留插入顺序,也不必与元素的插入和检索顺序相同。)因此输出将有所不同,顺序将类似于{Ruby = 4000 ,C = 2000,Django = null,Python = 1000,C ++ = 3000,null = 7000,Java = 1000}
// Java program to demonstrate the behavior of Map
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;
class MapClass {
public static void main(String[] args) {
// Creating an instance of HashMap
Map map = new HashMap();
//By using put() method to add some values in Map
map.put("Java", 1000);
map.put("C", 2000);
map.put("C++", 3000);
map.put("Ruby", 4000);
map.put("Python", 1000);
map.put("null", null);
map.put("Django", null);
/* Here we will not get any error but one null is accepted for keys*/
map.put("null", 7000);
// Display retrieval order of Map
System.out.println("Current Map list is :" + map);
// by using values() to find values of Map
Collection values = map.values();
// Display Values of Map
System.out.println("Current Map Key values is :" + values);
}
}
Output
输出量
E:\Programs>javac MapClass.java
E:\Programs>java MapClass
Current Map list is :{Ruby=4000, C=2000, Django=null,
Python=1000, C++=3000, null=7000, Java=1000}
Current Map Key values is :[4000, 2000, null, 1000, 3000, 7000, 1000]
翻译自: https://www.includehelp.com/java/differences-between-list-and-map-interface-in-java.aspx