迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中的关联数组

作者:迹忆客 最近更新:2023/09/11 浏览次数:

关联数组是一种将元素集存储在对中的数组类型。它是键和值的集合,其中键是唯一的并且与一个值相关联。

如果我们必须访问关联数组中的元素,我们必须调用数组的名称并传递键我们要访问的值


在 Java 中使用关联数组

例如,我们有一个名为标记的数组,用于存储卷号和学生的分数。

因此,如果我们必须访问特定学生的标记,那么我们可以像这样调用标记 105,其中标记是数组的名称,105 是学生的卷号,而不是索引号如果我们使用 Java 语言,则不可能在数组中。

因此关联数组不支持 Java,但我们可以使用 HashMap 轻松实现。Java 不支持关联数组,但可以使用 Map 来实现。


Java 中关联数组的摘要

HashMap<String, String> hashmap = new HashMap<>();
        // method to add the key,value pair in hashmap
        hashmap.put("Key1", "Value1");
        hashmap.put("Key2", "Value2");
        hashmap.put("Key3", "Value3");
		// and many more...
        // get the value 1 and 2
        System.out.println(hashmap.get("Key1"));
        System.out.println(hashmap.get("Key2"));
		// and many more...

在 Java 中实现关联数组

为了在 Java 中实现关联数组,我们使用了 Map 接口的实现类 HashMap。让我们一步一步来理解。

首先,导入并初始化 HashMap,即使用以下语句创建一个 HashMap 实例。

import java.util.HashMap;
HashMap<String, String> hashmap = new HashMap<>();

然后,使用 put() 方法,将键值添加到 HashMap

hashmap.put("Key1", "Value1");

使用 entrySet() 方法将 HashMap 转换为 Set 以删除重复键。

Set<Map.Entry<String ,String> > set = map.entrySet();

将 Set 转换为我们想要的数组 ArrayList

List<Map.Entry<String ,String>> list=new ArrayList<>(set);

在 Java 中创建关联数组

在本例中,我们使用 HashMap 类在 Java 中实现关联数组。

看,它包含键值对格式的数据,我们使用 getKey() 方法访问键和 getValue() 方法访问值。

import java.io.*;
import java.util.*;
public class SimpleTesting {
    public static void main(String[] args) {
        HashMap<String, String> hashmap = new HashMap<>();
        hashmap.put("Virat", "Batsman");
        hashmap.put("Bumrah", "Bowler");
        hashmap.put("Jadeja", "All-rounder");
        hashmap.put("Pant", "Wicket-Keeper");

        Set<Map.Entry<String, String>> s = hashmap.entrySet();
        List<Map.Entry<String, String>> array = new ArrayList<>(s);
        for (int i = 0; i < array.size(); i++) {
            System.out.println(array.get(i).getKey() + " is " + array.get(i).getValue());
        }
    }
}

输出:

Pant is Wicket-Keeper
Jadeja is All-rounder
Bumrah is Bowler
Virat is Batsman

正如我们已经讨论过的,该键应该是唯一的。如果我们在关联数组中插入相同的键,它将丢弃键值对之一。

我们在下面的代码中插入了两个相同的键 Virat。请参见下面的示例。

import java.io.*;
import java.util.*;
public class SimpleTesting {
    public static void main(String[] args) {
        HashMap<String, String> hashmap = new HashMap<>();
        hashmap.put("Virat", "Batsman");
        hashmap.put("Bumrah", "Bowler");
        hashmap.put("Jadeja", "All-rounder");
        hashmap.put("Pant", "Wicket-Keeper");
        hashmap.put("Virat", "Captain");

        Set<Map.Entry<String, String>> s = hashmap.entrySet();
        List<Map.Entry<String, String>> array = new ArrayList<>(s);
        for (int i = 0; i < array.size(); i++) {
            System.out.println(array.get(i).getKey() + " is " + array.get(i).getValue());
        }
    }
}

输出:

Pant is Wicket-Keeper
Jadeja is All-rounder
Bumrah is Bowler
Virat is Captain

在 Java 中将元素添加到关联数组

我们可以使用 put() 方法将元素添加到地图中的数组中。类似地,我们可以使用 remove() 方法从数组中删除一个元素。

我们可以使用 size() 方法找出数组的大小。

import java.util.HashMap;
public class SimpleTesting {
    public static void main(String[] args) {
        HashMap<String, String> fruits = new HashMap<String, String>();
        fruits.put("Apple", "Red");
        fruits.put("Banana", "Yellow");
        fruits.put("Guava", "Green");
        fruits.put("Blackberries", "Purple");
        
        System.out.println("The Size of fruits Map is : " + fruits.size());
        // Remove Banana from the HashMap
        fruits.remove("Banana");
        // To find out the size of the Hashmap
        System.out.println("The Size of fruits Map is : " + fruits.size());
        // Check whether the key is present in the Hashmap or not
        String fruit_key = "Apple";
        if (fruits.containsKey(fruit_key)) {
            System.out.println("The colour of " + fruit_key + " is: " + fruits.get(fruit_key));
        } else {
            System.out.println("There is no entry for the fruit of " + fruit_key);
        }
    }
}

输出:

The Size of fruits Map is : 4
The Size of fruits Map is : 3
The colour of Apple is: Red

遍历 Java 中关联数组的元素

我们可以使用 for-each 循环来遍历关联数组。由于 HashMap 属于 java.util 包,我们可以使用 foreach 循环来迭代它的元素。

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class SimpleTesting {
    public static void main(String[] args) {

        HashMap<String, String> fruits = new HashMap<String, String>();
        fruits.put("Apple", "Red");
        fruits.put("Banana", "Yellow");
        fruits.put("Guava", "Green");
        fruits.put("Blackberries", "Purple");
        System.out.println("The Size of fruits Map is : " + fruits.size());
        for (Map.Entry element : fruits.entrySet()) {
            String key = (String) element.getKey();
            System.out.println(key + " : " + element.getValue());
        }
    }
}

输出:

The Size of fruits Map is : 4
Guava : Green
Apple : Red
Blackberries : Purple
Banana : Yellow

在 Java 8 中使用 forEach() 方法遍历关联数组的元素

如果你使用的是 Java 8 或更高版本,则可以使用 forEach() 方法来遍历数组元素。forEach() 方法需要一个 lambda 表达式作为参数。

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class SimpleTesting {
    public static void main(String[] args) {
        HashMap<String, String> fruits = new HashMap<String, String>();
        fruits.put("Apple", "Red");
        fruits.put("Banana", "Yellow");
        fruits.put("Guava", "Green");
        fruits.put("Blackberries", "Purple");

        System.out.println("The Size of fruits Map is : " + fruits.size());
        fruits.forEach((k, v) -> System.out.println(k + " : " + v));
    }
}

输出:

The Size of fruits Map is : 4
Guava : Green
Apple : Red
Blackberries : Purple
Banana : Yellow

本教程研究了 Java 在技术上不支持关联数组,但我们可以使用 HashMap 轻松实现它。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便