Java 中的关联数组
- 在 Java 中使用关联数组
- Java 中关联数组的摘要
- 在 Java 中实现关联数组
- 在 Java 中创建关联数组
- 在 Java 中将元素添加到关联数组
- 遍历 Java 中关联数组的元素
- 
          
            在 Java 8 中使用 forEach()方法遍历关联数组的元素
 
关联数组是一种将元素集存储在键和值对中的数组类型。它是键和值的集合,其中键是唯一的并且与一个值相关联。
如果我们必须访问关联数组中的元素,我们必须调用数组的名称并传递键我们要访问的值。
在 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 轻松实现它。