Java 中将 ArrayList 转换为 LinkedHashMap
在 Java 中,LinkedHashMap 类 是一种哈希映射,它允许用户维护其中元素的有序记录。此功能还提供了快速插入、搜索和删除元素的方法。当我们需要将一个数组列表 转换为 LinkedHashMap 时,我们需要为此设置一个键值,它会反映为数组列表的索引。在迭代和数据排序方面,数组列表和 LinkedHashMap 本质上是相同的。
以下是一个通用示例以及此过程的可能伪代码:
Input ArrayList Is Here: { 5, 16, 7, 1997, 2001 }
Output LinkedHashMap Will Be:
Index | Value
1 | 5
2 | 16
3 | 7
4 | 1997
5 | 2001
while (i < l1.size()) {l.put(i + 1, l1.get(i));i++;}
Here, "l1" is a particular ArrayList and "l" is represented as the
LinkedHashMap.
Java 中将 ArrayList 转换为 LinkedHashMap 的算法
在此可能的算法中,我们将向您展示如何对数组节点执行转换过程以将其转换为一组 LinkedHashMap。通过使用此算法,我们将构建一些Java 语法 来全面了解问题陈述。
步骤 1 - 开始此过程。
步骤 2 - 声明并导入一些 Java 包。
步骤 3 - 创建一个公共列表。
步骤 4 - 声明一些键作为值。
步骤 5 - 为引用值创建一个构造函数。
步骤 6 - 在列表中分配已声明键的值。
步骤 7 - 返回一些私有变量 ID。
步骤 8 - 声明一个主要的公共类和方法。
步骤 9 - 声明参数字符串。
步骤 10 - 创建一个数组列表。
步骤 11 - 使用一些数据元素填充列表值。
步骤 12 - 创建并声明一个 LinkedHashMap 值。
步骤 13 - 声明对象方法。
步骤 14 - 为 LinkedHashMap 值创建对象。
步骤 15 - 声明每个数据元素到 LinkedHashMap 中。
步骤 16 - 打印 LinkedHashMap 作为值并终止此过程。
Java 中将 ArrayList 转换为 LinkedHashMap 的语法
public class Book {
private Integer bookId;
private String title;
private String author;
//getters, setters, constructors, equals and hashcode omitted
}
HashMap<String,HashMap<String,String>> newMap = new HashMap();
for(HashMap<String,String> record : dataList){
String key = record.get("rollno").toString();
newMap.put(key,record);
}
HashMap result = (HashMap<String, HashMap<String, String>>)
listOfHashMaps.stream()
.collect(Collectors.toMap(e-> e.get("roll"),e->e));
list.stream()
.collect(toMap(e->e.get("rollno"), Function.identity()));
在此可能的语法中,我们试图向您展示如何对数组节点执行转换过程以将其转换为一组 LinkedHashMap。通过这些语法,我们将努力构建一些 Java 代码以有效地解决问题陈述。
遵循的方法
方法 1 - 使用 .entrySet()、.toString、流、remove(Object key)、remove(Object key,Object value) 方法将 ArrayList 转换为 LinkedHashMap 的 Java 程序
方法 2 - 使用 put()、java.util.stream.Collectors 和 this() 方法将 ArrayList 转换为 LinkedHashMap 的 Java 程序
方法 1
使用 .entrySet()、.toString、流、Remove(Object key)、Remove(Object key,Object Value) 方法
使用 .entrySet() 方法
在此可能的方法中,我们将应用.entrySet() 方法 来执行将数组列表转换为一组 LinkedHashMap 的转换。
List<Student> aListStudent = new ArrayList<Student>();
aListStudent.add(new Student(1, "RUDRA"));
aListStudent.add(new Student(2, "ABONI"));
aListStudent.add(new Student(3, "AHONA"));
aListStudent.add(new Student(4, "ANANDI"));
System.out.println("ArrayList contains are here: " + aListStudent);
Map<Integer, Student> mapStudents =
aListStudent.stream().collect( Collectors.toMap(Student::getId,
student->student));
System.out.println("Map contains are: " + mapStudents);
示例
//Java program to convert an ArrayList to a LinkedHashMap by using the .entrySet() method
import java.util.*;
import java.io.*;
public class ARBRDD{
public static void main(String[] args){
LinkedHashMap<Integer, Integer> l = new LinkedHashMap<>();
ArrayList<Integer> l1 = new ArrayList<>();
l1.add(12);
l1.add(16);
l1.add(7);
l1.add(1997);
l1.add(2001);
int i = 0;
while (i < l1.size()){
l.put(i + 1, l1.get(i));
i++;
}
System.out.println("Key Present In The List | Value Of The Element Is");
for (Map.Entry<Integer, Integer> it :l.entrySet()) {
System.out.println(" " + it.getKey() + " | " + it.getValue());
}
}
}
输出
Key Present In The List | Value Of The Element Is 1 | 12 2 | 16 3 | 7 4 | 1997 5 | 2001
使用 .toString 和流方法
在此可能的方法中,我们将应用 .toString() 和Java 流 方法来执行将数组列表转换为一组 LinkedHashMap 的转换。
示例
// Java program to convert ArrayList to LinkedHashMap by using .toString and streams method
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
class integer{
private Integer id;
private String name;
public integer(Integer id, String name){
this.id = id;
this.name = name;
}
public Integer getId() { return this.id; }
public String toString(){
return "[<" + this.id + "=><" + this.name + ">]";
}
}
public class ARBRDD{
public static void main(String[] args){
List<integer> List = new ArrayList<integer>();
List.add(new integer(1, "ARB"));
List.add(new integer(2, "RDD"));
List.add(new integer(3, "DHAKA"));
List.add(new integer(4, "KOLKATA"));
System.out.println("ArrayList contains are here: " + List);
Map<Integer, integer> HashMap =
List.stream().collect(Collectors.toMap(
integer::getId, integer -> integer));
System.out.println("Map contains from the ArrayList are: " +
HashMap);
}
}
输出
ArrayList contains are here:
[[<1=><ARB>], [<2=><RDD>], [<3=><DHAKA>], [<4=><KOLKATA>]]
Map contains from the ArrayList are:
{1=[<1=><ARB>], 2=[<2=><RDD>], 3=[<3=><DHAKA>], 4=[<4=><KOLKATA>]}
使用 Remove(Object key)、Remove(Object key,Object Value) 方法
在此可能的方法中,我们将应用 remove(Object key) 和 remove(Object key,Object value) 方法来执行将数组列表转换为一组 LinkedHashMap 的转换。
示例
//Java program to convert an ArrayList to a LinkedHashMap and remove the elements from the list by using remove(Object key), remove(Object key,Object value)
import java.util.*;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
public class ARBRDD{
public static void main(String[] args) {
Map<String, Integer> travelFareMap = new LinkedHashMap<String,
Integer>();
travelFareMap.put("BUS", 1200);
travelFareMap.put("CAR", 2000);
travelFareMap.put("TRAIN", 3500);
travelFareMap.put("AIR PLANE", 14000);
System.out.println(travelFareMap);
Integer fareCar = travelFareMap.remove("CAR");
System.out.println("*************************************");
System.out.println("Vehicle CAR with fare has been "+fareCar+" removed from the HashMap");
System.out.println(travelFareMap);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
boolean isCarRemoved = travelFareMap.remove("TRAIN",3500);
System.out.println("Did CAR removed from the LinkedHashMap: "+isCarRemoved);
System.out.println(travelFareMap);
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
boolean isFlightRemoved = travelFareMap.remove("AIR PLANE",14000);
System.out.println("Did AIR PLANE removed from the LinkedHashMap: "+isFlightRemoved);
System.out.println(travelFareMap);
System.out.println("##################END####################");
}
}
输出
{BUS=1200, CAR=2000, TRAIN=3500, AIR PLANE=14000}
*************************************
Vehicle CAR with fare has been 2000 removed from the HashMap
{BUS=1200, TRAIN=3500, AIR PLANE=14000}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Did CAR removed from the LinkedHashMap: true
{BUS=1200, AIR PLANE=14000}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Did AIR PLANE removed from the LinkedHashMap: true
{BUS=1200}
##################END####################
方法 2
使用 put()、Java.util.stream.Collectors 和 This() 方法。
使用 put() 方法和定义 IllegalArgumentsException
在此可能的方法中,我们将应用put() 方法 来执行将数组列表转换为一组 LinkedHashMap 的转换,并且在这里我们还定义了 IllegalArgumentsException 的作用。
private static class CompArrayList extends ArrayList<LinkedHashMap<String,
Comparable>> implements Comparable{
@Override
public int compareTo(Object o){
return 0;
}
}
ArrayList<LinkedHashMap<String, Comparable>> y = new
ArrayList<LinkedHashMap<String, Comparable>>();
LinkedHashMap<String, Comparable> x = new LinkedHashMap<String, Comparable>();
示例
//Java program to convert two arrays containing keys and values into a Linked Hash Map by using put() method and define IllegalArgumentsException
import java.util.*;
import java.io.*;
import java.util.LinkedHashMap;
public class ARBRDD{
public static HashMap map(Integer[] keys,
String[] values){
int keysSize = keys.length;
int valuesSize = values.length;
if (keysSize != valuesSize) {
throw new IllegalArgumentException( "The Number Of Keys Doesn't Match The Number Of Values.");
}
if (keysSize == 0) {
return new HashMap();
}
HashMap<Integer, String> map
= new HashMap<Integer, String>();
for (int i = 0; i < keysSize; i++) {
map.put(keys[i], values[i]);
}
return map;
}
public static void main(String[] args){
Integer[] keys = { 1, 2, 3, 4, 5 };
String[] values
= { "Welcome", "To", "Kolkata", "From", "Dhaka" };
Map m = map(keys, values);
System.out.println(m);
}
}
输出
{1=Welcome, 2=To, 3=Kolkata, 4=From, 5=Dhaka}
使用 Java.util.stream.Collectors 方法
在此可能的方法中,我们将应用 java.util.stream.Collectors 方法来执行将数组列表转换为一组 LinkedHashMap 的转换。
示例
//Java program to convert an ArrayList to a LinkedHashMap by using java.util.stream.Collectors
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class ARBRDD{
public static void main(String[] args){
List<HashMap<String, String>> input = new ArrayList<>();
HashMap<String, String> subinput1 = new HashMap<>();
subinput1.put("name", "name1");
subinput1.put("rollno", "rollno1");
input.add(subinput1);
HashMap<String, String> subinput2 = new HashMap<>();
subinput2.put("name", "name2");
subinput2.put("rollno", "rollno2");
input.add(subinput2);
HashMap<String, HashMap<String, String>> result = (HashMap<String, HashMap<String, String>>) input.stream()
.collect(Collectors.toMap(v -> (String) v.get("rollno"), e ->
e));
System.out.println(result);
}
}
输出
{rollno2={name=name2, rollno=rollno2}, rollno1={name=name1, rollno=rollno1}}
使用 This() 方法
在此可能的方法中,我们将应用 this() 方法来执行将数组列表转换为一组 LinkedHashMap 的转换。
示例
//Java program to convert an ArrayList to a LinkedHashMap by using THIS methed
import java.util.*;
import java.util.LinkedHashMap;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity){
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class ARBRDD {
public static void main(String[] args) {
Map<Integer,Book> map=new LinkedHashMap<Integer,Book>();
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB Publications",8);
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill Noida",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
map.put(2,b2);
map.put(1,b1);
map.put(3,b3);
for(Map.Entry<Integer, Book> entry:map.entrySet()){
int key=entry.getKey();
Book b=entry.getValue();
System.out.println(key+" Details:");
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}
输出
2 Details: 102 Data Communications & Networking Forouzan Mc Graw Hill Noida 4 1 Details: 101 Let us C Yashwant Kanetkar BPB Publications 8 3 Details: 103 Operating System Galvin Wiley 6
结论
在 Java 环境中,LinkedHashMap 类是将链接列表和哈希表结合在 Map 接口上的一个实现过程,并具有适当的迭代过程。此过程继承 HashMap 类并将其转换为 Map 接口。在本文中,我们学习了如何在 Java 环境中将特定的数组列表转换为 LinkedHashMap 集的各种方法。通过使用上述语法和算法,我们构建了一些 Java 代码以有效地解释问题陈述。
另请参阅: Java 面试问题及答案
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP