Java中将ArrayList转换为HashMap
在Java中,List是一个子接口类,它也可以表示集合。它是一个有序集合,包含可以排序的重复值对象。Java ArrayList是一个集合框架,因为它位于java.util包中,并提供Java中的动态数组元素。
对于ArrayList,我们不必指定列表的大小。另一方面,HashMap<K, V>是一个Java集合,可以在java.util包中找到。它包含数据值作为(键, 值)对。基本上,有两种不同的方法可以使用Java将ArrayList转换为HashMap:
使用ArrayList迭代
使用ArrayList迭代和LinkedHashMap
这里有一些关于这个过程的一般示例:
Input As A List : [1="1", 2="2", 3="3"]
Output As A Map : {1=1, 2=2, 3=3}
Input As A List : [1="Java", 2="for", 3="JavaScript"]
Output As A Map : {1=Java, 2=for, 3=JavaScript}
Input : Mercedes, Audi, BMW, Harley Davidson, Triumph
{Car=[Mercedes, Audi, BMW], Bike=[Harley Davidson, Triumph]}
将ArrayList转换为HashMap的算法
在这个可能的算法中,我们将向您展示如何对数组节点进行转换处理,使其成为一组哈希映射。使用此算法,我们将构建一些Java语法来广泛了解问题陈述。
步骤1 - 开始该过程。
步骤2 - 声明和导入一些Java包。
步骤3 - 创建一个公共列表。
步骤4 - 声明一些键值对。
步骤5 - 为引用值创建一个构造函数。
步骤6 - 分配已声明键的值。
步骤7 - 返回一些私有变量ID。
步骤8 - 声明一个公共主类和方法。
步骤9 - 声明参数字符串。
步骤10 - 创建一个ArrayList。
步骤11 - 使用数据元素填充列表值。
步骤12 - 创建和声明一个映射值。
步骤13 - 声明对象方法。
步骤14 - 创建对象映射值。
步骤15 - 为每个数据元素声明并将其添加到映射中。
步骤16 - 打印映射值并终止该过程。
将ArrayList转换为HashMap的语法
ArrayList<Product> productList = new ArrayList<Product>();
productList = getProducts();
Map<String, Product> urMap =
yourList.stream().collect(Collectors.toMap(Product::getField1,
Function.identity()));
HashMap<String, Product> productMap = new HashMap<String, Product>();
for (Product product : productList) {
productMap.put(product.getProductCode(), product);
}
for (Product p: productList) { s.put(p.getName(), p); }
for(Product p : productList){
s.put(p.getProductCode() , p);
}
在上面这个可能的语法中,我们尝试向您展示如何对数组节点进行转换处理,使其成为一组哈希映射。通过这些语法,我们将构建一些Java代码,以高效的方式解决问题陈述。
遵循的方法
方法1 - 使用convertArrayListToHashMap()、list、Collectors.toMap()、Collectors.groupingBy()和直接迭代方法的Java程序,该程序将ArrayList转换为HashMap
方法2 - 使用Java 8 Stream和ASCII字符表方法的将列表转换为HashMap的Java程序
方法1
使用ConvertArrayListToHashMap()、List、Collectors.toMap()、Collectors.groupingBy()和直接迭代方法将ArrayList转换为HashMap。
使用ConvertArrayListToHashMap()方法
在这个可能的方法中,我们将应用convertArrayListToHashMap()方法来执行将ArrayList转换为一组HashMap的转换。
class Color{
private String name;
private String code;
public Color(String name, String code){
this.name = name;
this.code = code;
}
@Override
public String toString() {
return name + "=" + code;
}
//Declare the getters and setters method
}
示例
//Java program by using of convertArrayListToHashMap() method directly which converts ArrayList to Hashmap
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ARBRDD{
public static void main(String[] args){
ArrayList<String> languageList
= new ArrayList<>(Arrays.asList("INDIA", "BANGLADESH", "Mitali","Bandhan", "Maitree"));
System.out.println("----------#---ArrayList Is Here---#------------");
for (String language : languageList){
System.out.println(language);
}
System.out.println("-----------$---HashMap Is Here---$-------------");
HashMap<String, Integer> languageMap =
convertArrayListToHashMap(languageList);
for (Map.Entry<String, Integer> entry : languageMap.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
private static HashMap<String, Integer>
convertArrayListToHashMap(ArrayList<String> arrayList){
HashMap<String, Integer> hashMap = new HashMap<>();
for (String str : arrayList) {
hashMap.put(str, str.length());
}
return hashMap;
}
}
输出
----------#---ArrayList Is Here---#------------ INDIA BANGLADESH Mitali Bandhan Maitree -----------$---HashMap Is Here---$------------- Maitree : 7 Mitali : 6 BANGLADESH : 10 INDIA : 5 Bandhan : 7
使用ArrayList迭代和LinkedHashMap方法
在这个可能的方法中,我们将应用迭代方法和哈希方法来执行将ArrayList转换为一组HashMap的转换。
示例
// Java program by using convertArrayListToHashMap() method directly converts ArrayList to HashMap with ArrayList Iteration and LinkedHashMap
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.*;
public class ARBRDD{
public static void main(String[] args){
ArrayList<String> languageList
= new ArrayList<>(Arrays.asList(
"KOLKATA", "ESPLANADE", "RAJABAZAR", "SALTLAKE", "DUMDUM"));
System.out.println("----------#---ArrayList Is Here---#------------");
for (String language : languageList) {
System.out.println(language);
}
System.out.println("-----------&---HashMap is here---&-------------");
HashMap<String, Integer> languageMap
= convertArrayListToHashMap(languageList);
for (Map.Entry<String, Integer> entry :
languageMap.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
private static HashMap<String, Integer>
convertArrayListToHashMap(ArrayList<String>
arrayList){
LinkedHashMap<String, Integer> linkedHashMap
= new LinkedHashMap<>();
for (String str : arrayList) {
linkedHashMap.put(str, str.length());
}
return linkedHashMap;
}
}
输出
----------#---ArrayList Is Here---#------------ KOLKATA ESPLANADE RAJABAZAR SALTLAKE DUMDUM -----------&---HashMap is here---&------------- KOLKATA : 7 ESPLANADE : 9 RAJABAZAR : 9 SALTLAKE : 8 DUMDUM : 6
使用List方法
在这个可能的方法中,我们将应用Java Lists方法来执行将ArrayList转换为一组HashMap的转换。
示例
// Java program by using convertArrayListToHashMap() method directly converts ArrayList to HashMap with ArrayList Iteration and LinkedHashMap by object of list
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
class Student {
private Integer id;
private String name;
public Student(Integer id, String name){
this.id = id;
this.name = name;
}
public Integer getId(){
return id;
}
public String getName(){
return name;
}
}
public class ARBRDD {
public static void main(String[] args){
List<Student>
lt = new ArrayList<Student>();
lt.add(new Student(071001, "ARB"));
lt.add(new Student(161097, "RDD"));
lt.add(new Student(100522, "ARBRDD"));
Map<Integer, String> map = new HashMap<>();
for (Student stu : lt) {
map.put(stu.getId(), stu.getName());
}
System.out.println("Map Is Here: " + map);
}
}
输出
Map Is Here: {29185=ARB, 161097=RDD, 100522=ARBRDD}
使用Collectors.toMap()方法
在这个可能的方法中,我们将应用Collectors.toMap()方法来执行将ArrayList转换为一组HashMap的转换。
示例
//Java program for list convert in HashMap with the help of java.util.LinkedHashMap and Collectors.toMap() method
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
class Student {
private Integer id;
private String name;
public Student(Integer id, String name){
this.id = id;
this.name = name;
}
public Integer getId(){
return id;
}
public String getName(){
return name;
}
}
public class ARBRDD {
public static void main(String[] args){
List<Student> lt = new ArrayList<>();
lt.add(new Student(071001, "ARB"));
lt.add(new Student(161097, "RDD"));
lt.add(new Student(100522, "ARBRDD"));
LinkedHashMap<Integer, String>
map = lt.stream()
.collect(
Collectors
.toMap(
Student::getId,
Student::getName,
(x, y)
-> x + ", " + y,
LinkedHashMap::new));
map.forEach(
(x, y) -> System.out.println(x + "=" + y));
}
}
输出
29185=ARB 161097=RDD 100522=ARBRDD
使用Collectors.groupingBy()方法
在这个可能的方法中,我们将应用Collectors.groupingBy()方法来执行将ArrayList转换为一组HashMap的转换。
示例
//Java program for list convert in HashMap with the help of Collectors.groupingBy() method
import java.util.*;
import java.util.stream.Collectors;
import java.util.HashMap;
class Student {
private Integer id;
private String name;
public Student(Integer id, String name){
this.id = id;
this.name = name;
}
public Integer getId(){
return id;
}
public String getName(){
return name;
}
}
public class ARBRDD {
public static void main(String[] args){
List<Student> lt = new ArrayList<Student>();
lt.add(new Student(071001, "ARB"));
lt.add(new Student(161097, "RDD"));
lt.add(new Student(100522, "ARBRDD"));
lt.add(new Student(180423, "RDDARB"));
Map<Integer, List<String> >
multimap = lt
.stream()
.collect(
Collectors
.groupingBy(
Student::getId,
Collectors
.mapping(
Student::getName,
Collectors
.toList())));
System.out.println("////MultiMap Is Here//// " + multimap);
}
}
输出
////MultiMap Is Here//// {29185=[ARB], 180423=[RDDARB], 100522=[ARBRDD], 161097=[RDD]}
方法2
使用Java 8 Stream和ASCII字符表方法。
使用Java 8 Stream方法
在这个可能的方法中,我们将应用Java 8 Stream方法来执行将ArrayList转换为一组HashMap的转换。
colors.stream().collect(Collectors.toMap(x -> x.getName(),x -> x.getCode())); colors.stream().collect(Collectors.toMap(x -> x.getName(),x -> x.getCode(),(oldValue, newValue) -> newValue)); colors.stream().collect(Collectors.toMap(x -> x.getName(), x -> x.getCode(), (oldValue, newValue) -> newValue, TreeMap::new));
示例
//Java program for list convert in HashMap with the help of Java 8 Stream methods
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.stream.Collectors;
class Color{
private String name;
private String code;
public Color(String name, String code){
this.name = name;
this.code = code;
}
@Override
public String toString() {
return name + "=" + code;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
public class ARBRDD{
public static void main(String[] args){
// input list of `Color` objects
List<Color> colors = new ArrayList<>();
colors.add(new Color("RED COLOUR", "#FF0000"));
colors.add(new Color("BLUE COLOUR", "#0000FF"));
colors.add(new Color("GREEN COLOUR", "#008000"));
Map<String, String> map = colors.stream()
.collect(Collectors.toMap(Color::getName, Color::getCode));
System.out.println("List Value Is Here: " + colors);
System.out.println("Map Value Is Here : " + map);
}
}
输出
List Value Is Here: [RED COLOUR=#FF0000, BLUE COLOUR=#0000FF, GREEN COLOUR=#008000]
Map Value Is Here : {GREEN COLOUR=#008000, BLUE COLOUR=#0000FF, RED COLOUR=#FF0000}
使用ASCII字符表方法
在这个可能的方法中,我们将应用ASCII字符表方法来执行将ArrayList转换为一组HashMap的转换。
示例
//Java program for list convert in HashMap with the help of ASCII characters table methods
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ARBRDD{
public static void main(String[] args){
List<String> chars = Arrays.asList("A", "R", "B", "U");
Map<String, Integer> asciiMap = new HashMap<>();
for (String s : chars){
if (asciiMap.put(s, s.hashCode()) != null){
throw new IllegalStateException("Duplicate key");
}
}
System.out.println(asciiMap);
}
}
输出
{A=65, R=82, B=66, U=85}
结论
在Java环境中,我们当然可以将ArrayList转换为HashMap。但是像数组一样,HashMap不遵循元素的顺序。为了创建和构建一个保持顺序的HashMap,还有一个名为LinkedHashMap的函数,它可以帮助程序员维护HashMap。在今天的文章中,我们学习了在Java环境中将特定的ArrayList转换为HashMap集合的各种方法。
使用上述语法和算法,我们构建了一些Java代码来高效地解释问题陈述。
另请阅读: Java面试问题及答案
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP