📜  Java 8 之前如何将 ArrayList 转换为 HashMap?

📅  最后修改于: 2022-05-13 01:55:49.892000             🧑  作者: Mango

Java 8 之前如何将 ArrayList 转换为 HashMap?

ArrayList 是一个可调整大小的数组。它位于Java包Java.util 下。它在Java中提供动态数组。 ArrayList 在需要对数组进行大量更改但也比标准数组慢的程序中很有用。 ArrayList 中的元素可以在需要时添加和删除。

HashMap 是一种以“键/值”对形式存储项目的数据结构。它们可以通过另一种类型的索引(如字符串)进行访问。 HashMap 表示为 HashMap。这与 HashTable 非常相似,但它允许使用空值和空键。它不会按照它们存储到 HashMap 中的相同顺序返回键和值。它也在Java包Java.util 下。

现在为了将 ArrayList 转换为 HashMap,两个用例也被认为是为了说明覆盖概念

  1. 初始 ArrayList 没有重复项
  2. 初始 ArrayList 具有重复值。

案例 1:在Java 8 之前,将 ArrayList 转换为 HashMap 的最理想方法是使用增强的 for 循环迭代给定的 ArrayList,并将 String 作为键及其长度作为值插入 HashMap。在这里,ArrayList 的每个元素都可以转换成一个键值对并存储在 HashMap 中。此方法还处理 ArrayList 中的任何重复项,因为插入的条目会在重复键的情况下覆盖值。在这种情况下,不会抛出任何错误或异常。

示例:初始 ArrayList 没有重复项

Java
// Program to convert ArrayList
// to Hashmap before Java 8
 
// Name of ArrayList chosen randomly: vehicleList
 
// Importing java generic libraries
import java.util.*;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating ArrayList
        ArrayList vehicleList
            = new ArrayList<>(Arrays.asList(
                "Car", "Bike", "Bus", "Cycle", "Rickshaw"));
 
        // Display message to enter ArrayList
        System.out.println("ArrayList: \n");
 
        // Printing ArrayList
        for (String vehicle : vehicleList) {
            System.out.println(vehicle);
        }
 
        // Display message to separate above
        // list from hashmap for readability
        System.out.println("\nHashMap: \n");
 
        // Creating Hashmap
        HashMap vehicleMap
            = convertArrayListToHashMap(vehicleList);
 
        for (Map.Entry entry :
             vehicleMap.entrySet()) {
            System.out.println(entry.getKey() + " : "
                               + entry.getValue());
        }
    }
 
    // Converting ArrayList to HashMap
    private static HashMap
    convertArrayListToHashMap(ArrayList arrayList)
    {
        HashMap hashMap = new HashMap<>();
 
        // For-each loop for iteration
        for (String str : arrayList) {
            hashMap.put(str, str.length());
        }
 
        // Returning converted Hashmap
        return hashMap;
    }
}


Java
// Program to convert ArrayList
// to Hashmap before Java 8
 
// Importing java generic libraries
import java.util.*;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating ArrayList
        ArrayList vehicleList = new ArrayList<>(
 
            // Presence of Duplicate Elements
            Arrays.asList("Car", "Bike", "Bus", "Cycle",
                          "Rickshaw", "Car", "Bike"));
 
        System.out.println("ArrayList: \n");
 
        // Printing ArrayList
        for (String vehicle : vehicleList) {
            System.out.println(vehicle);
        }
 
        // Display message just after ArratList is printed
        // for better readability
        System.out.println("\nHashMap: \n");
 
        // Creating HashMap
        HashMap vehicleMap
            = convertArrayListToHashMap(vehicleList);
 
        // Printing above HashMap
        for (Map.Entry entry :
             vehicleMap.entrySet()) {
            System.out.println(entry.getKey() + " : "
                               + entry.getValue());
        }
    }
 
    // Converting ArrayList to HashMap
    private static HashMap
    convertArrayListToHashMap(ArrayList arrayList)
    {
        HashMap hashMap = new HashMap<>();
 
        // For-each loop for iteration
        for (String str : arrayList) {
            hashMap.put(str, str.length());
        }
 
        // Returning converted HashMap
        return hashMap;
    }
}



输出
ArrayList: 

Car
Bike
Bus
Cycle
Rickshaw

HashMap: 

Bus : 3
Rickshaw : 8
Car : 3
Bike : 4
Cycle : 5

情况 2:初始 ArrayList 具有重复值,由于 HashMap 中的重复键,它会覆盖该值。

Java

// Program to convert ArrayList
// to Hashmap before Java 8
 
// Importing java generic libraries
import java.util.*;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating ArrayList
        ArrayList vehicleList = new ArrayList<>(
 
            // Presence of Duplicate Elements
            Arrays.asList("Car", "Bike", "Bus", "Cycle",
                          "Rickshaw", "Car", "Bike"));
 
        System.out.println("ArrayList: \n");
 
        // Printing ArrayList
        for (String vehicle : vehicleList) {
            System.out.println(vehicle);
        }
 
        // Display message just after ArratList is printed
        // for better readability
        System.out.println("\nHashMap: \n");
 
        // Creating HashMap
        HashMap vehicleMap
            = convertArrayListToHashMap(vehicleList);
 
        // Printing above HashMap
        for (Map.Entry entry :
             vehicleMap.entrySet()) {
            System.out.println(entry.getKey() + " : "
                               + entry.getValue());
        }
    }
 
    // Converting ArrayList to HashMap
    private static HashMap
    convertArrayListToHashMap(ArrayList arrayList)
    {
        HashMap hashMap = new HashMap<>();
 
        // For-each loop for iteration
        for (String str : arrayList) {
            hashMap.put(str, str.length());
        }
 
        // Returning converted HashMap
        return hashMap;
    }
}


输出
ArrayList: 

Car
Bike
Bus
Cycle
Rickshaw
Car
Bike

HashMap: 

Bus : 3
Rickshaw : 8
Car : 3
Bike : 4
Cycle : 5