Java中List和Map接口之間的區別

列表與地圖界面 (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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542095.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542095.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542095.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python—高階函數

什么是內置高階函數: 一個函數可以作為參數傳給另外一個函數,或者一個函數的返回值為另外一個函數(若返回值為該函數本身,則為遞歸),滿足其一則為高階函數。 Python中內置了幾個常用的高階函數,…

功能區不顯示工具條_【新老客戶必知】軟件支持超高清屏顯示器了

隨著計算機硬件的不斷更新換代顯示設備的不斷更新從原來的分辨率640 X 480啥原來分辨這么低呀?還記得DOS嗎?或者Win95,win98嗎當時顯示器分辨率能調到800X 600很好了2000年左右隨著純平顯示器的推出也有了高清顯示器的概念那么一般我們說的高清顯示器分辨…

nginx1.10.2源碼安裝配置參數參考

[rootlocalhost nginx-1.10.2]# ./configure --help--help print this message--prefixPATH set installation prefix #Nginx安裝的根路徑,默認為 /usr/local/nginx。--sbin-pathPATH set nginx binary pathname #指定nginx二進制文件的路徑,默認為PATH/sbin/n…

c ++查找字符串_C ++朋友功能| 查找輸出程序| 套裝1

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;class Sample {int A, B;friend void fun();};void fun(){Sample S;S.A 10;S.B 20;cout << S.A << " " << S.B << endl;}int main(){fun();retu…

Spring定時器的運用

為什么80%的碼農都做不了架構師&#xff1f;>>> 一、spring4定時器任務配置如下&#xff1a; <bean id"jsapiTask" class"chan.ye.dai.wexin.JsapiTicketTimeTask" /><bean id"jobDetail"class"org.springframework.s…

python—裝飾器

裝飾器概念&#xff1a; 把一個函數當作參數傳遞給一個函數&#xff0c;返回一個替代版的函數 本質上就是一個返回函數的函數 在不改變原函數的基礎上&#xff0c;給函數增加功能 python 中裝飾器做的事情&#xff01;它們封裝一個函數&#xff0c;并且用這樣或者那樣的方式來修…

ad18原理圖器件批量修改_Altium Designer 15原理圖設計基礎

Altium Designer 15成為越來越多電子設計開發工程師EDA電路設計軟件的首選&#xff0c;在學校學習Altium Designer的也越來較多&#xff0c;像單片機開發學習一樣&#xff0c;EDA設計只要學會了&#xff0c;再學其他的設計軟件就容易多了。上一節分享了《Altium Designer 15集成…

c++freopen函數_使用示例的C語言中的freopen()函數

cfreopen函數C語言中的freopen()函數 (freopen() function in C) Prototype: 原型&#xff1a; FILE* freopen(const char *str, const char *mode, FILE *stream);Parameters: 參數&#xff1a; const char *str, const char *mode, FILE *streamReturn type: FILE* 返回類型…

python—文件

1 . 文件的基本操作&#xff1a; 文件讀取三部曲&#xff1a; 打開操作關閉&#xff08;如果不關閉會占用文件描述符&#xff09; 打開文件&#xff1a; f open(/tmp/passwdd,w)操作文件&#xff1a; 1 . 讀操作&#xff1a; f.read()content f.read()print(content) 2 …

基本概念學習(7000)--P2P對等網絡

對等網絡&#xff0c;即對等計算機網絡&#xff0c;是一種在對等者&#xff08;Peer&#xff09;之間分配任務和工作負載的分布式應用架構[1] &#xff0c;是對等計算模型在應用層形成的一種組網或網絡形式。“Peer”在英語里有“對等者、伙伴、對端”的意義。因此&#xff0c;…

c語言for循環++_C ++程序使用循環查找數字的冪

c語言for循環Here, we are going to calculate the value of Nth power of a number without using pow function. 在這里&#xff0c;我們將不使用pow函數來計算數字的N 次冪的值 。 The idea is using loop. We will be multiplying a number (initially with value 1) by t…

廈門one_理想ONE真是“500萬內最好的車”?

提起羅永浩&#xff0c;不少人還停留在“砸冰箱、造手機”等早期事件。隨著網絡直播的興起&#xff0c;羅永浩轉戰直播帶貨行業&#xff0c;但老羅畢竟是老羅&#xff0c;雷人語錄一點沒比以前少。前一段時間&#xff0c;羅永浩在微博中稱&#xff1a;“理想ONE是你能在這個價位…

Data Collection

眾所周知&#xff0c;計算機領域論文是要以實驗為基礎的&#xff0c;而實驗的原料就是數據。不管是在圖像&#xff0c;文字或者語音領域&#xff0c;開源的數據都十分寶貴和重要。這里主要收集各領域的一些常用的公開數據集。 計算機視覺&#xff1a; 【ImageNet】 【Caltech P…

python—os模塊、時間模塊

os模塊 作用&#xff1a;os模塊是python標準庫中的一個用于訪問操作系統功能的模塊&#xff0c; os模塊提供了其他操作系統接口&#xff0c;可以實現跨平臺訪問。 使用&#xff1a; 1 . 返回操作系統類型 &#xff1a;os.name 值為&#xff1a;posix 是linux操作系統 值為&…

kotlin鍵值對數組_Kotlin程序檢查數組是否包含給定值

kotlin鍵值對數組Given an array and an element, we have to check whether array contains the given element or not. 給定一個數組和一個元素&#xff0c;我們必須檢查數組是否包含給定的元素。 Example: 例&#xff1a; Input:arr [34, 56, 7, 8, 21, 0, -6]element to…

enter sleep mode黑屏怎么解決_【linux】 不要再暴力關機了,講講我最近遇到的問題和完美解決方案...

歡迎關注我的個人公眾號&#xff1a;AI蝸牛車前言結束了每天的緊張的工作&#xff0c;這兩天真的有些肝。這兩天打打字&#xff0c;突然感覺手指頭疼起來了&#xff0c;想意識到成天打了十多個小時的鍵盤&#xff0c; 手指頭都疲勞了 之后這兩天基本上除了基本的吃睡&#xff…

重復T次的LIS的dp Codeforces Round #323 (Div. 2) D

http://codeforces.com/contest/583/problem/D 原題&#xff1a;You are given an array of positive integers a1,?a2,?...,?an??T of length n??T. We know that for any i?>?n it is true that ai??ai?-?n. Find the length of the longest non-decreasing …

微擎pc 導入前綴_段覆蓋前綴| 8086微處理器

微擎pc 導入前綴As we already know that the effective address is calculated by appending the segment registers value and adding up the value of the respective offset. But what if we want to choose some other offset than the assigned one. 眾所周知&#xff0…

python—面向對象

面向過程 面向對象&#xff1a; 面向過程&#xff1a;—側重于怎么做&#xff1f; 1.把完成某一個需求的 所有步驟 從頭到尾 逐步實現 2.根據開發要求&#xff0c;將某些功能獨立的代碼封裝成一個又一個函數 3.最后完成的代碼&#xff0c;就是順序的調用不同的函數 特點&#…

5中bug vue_蘋果官網出BUG!這些都只要一兩百元

近日&#xff0c;有網友在網上反饋稱&#xff0c;他發現蘋果官網商城出現了BUG&#xff01;眾多上千元的產品&#xff0c;BUG價只需一兩百元。比如Shure MOTIV MV88 Digital立體聲電容式麥克風配件。正常售價1288元&#xff0c;而BUG后的價格是235元。UBTECH Jimu Astrobot Cos…