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.mzph.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…