📜  如何在Java中以相反的顺序迭代 LinkedHashMap?

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

如何在Java中以相反的顺序迭代 LinkedHashMap?

LinkedHashMap 用于维护插入其中的元素的顺序。它提供了可以按插入顺序访问元素的位置。 LinkedHashMap 包含基于键的值。它实现了 Map 接口并扩展了 HashMap 类。它只包含唯一的元素或映射。

Linkedhashmap 的语法

public class LinkedHashMap extends HashMap implements Map

其中K是键, V是值。

我们可以将键和值作为我们自己的数据类型,如字符串、浮点数、整数等。我们可以通过首先反转元素来反转链接哈希图中的元素。反转后,我们可以迭代它。

方法一:(使用reverse()方法)

此方法用于反转 LinkedHashMap 中的元素或映射顺序。

句法:

public static void reverse(List myList)

参数: myList 是提供给 Collections.reverse(myList) 方法的 List。

返回:它不返回任何内容,而是在内部修改列表。

异常:如果指定的 List 或其列表迭代器不支持 set 操作,则抛出UnsupportedOperationException

脚步:

  • 导入必要的包
  • 创建一个带有键和值的 LinkedHashMap
  • 反转 LinkedHashMap
  • 迭代它。

示例 1:

学生详细信息显示升序和降序,以整数为键,值为字符串。

Java
// Java program to iterate 
// LinkedHashMap in reverse order
  
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
  
public class Main {
  
    public static void main(String[] args)
    {
        System.out.println("Student Details:");
        
        // creating HashMap object of type 
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        // adding key-value pairs to HashMap object
        lhm.put(1, "Sravan Kumar");
        lhm.put(2, "Ishitha");
        lhm.put(3, "Harsha");
        lhm.put(4, "Vamsi");
        lhm.put(5, "Jyothika");
  
        // Insertion Order iterating
        System.out.println(
            "Insertion Order of LinkedHashMap:"
            + " iterating \n");
  
        // getting keySet() into Set
        Set set = lhm.keySet();
  
        // get Iterator from key set
        Iterator itr = set.iterator();
  
        // iterating as per Insertion Order
        while (itr.hasNext()) {
            Integer key = itr.next();
            System.out.println("Key : " + key + "\t\t"
                               + "Value : " + lhm.get(key));
        }
  
        // Reverse of Insertion Order iterating
        System.out.println("\n\nReverse of Insertion Order:"
                           + " iterating \n");
  
        // convert to ArrayList of key set
        List alKeys
            = new ArrayList(lhm.keySet());
  
        // reverse order of keys
        Collections.reverse(alKeys);
  
        // iterate LHM using reverse order of keys
        for (Integer strKey : alKeys) {
            System.out.println("Key : " + strKey + "\t\t"
                               + "Value : "
                               + lhm.get(strKey));
        }
    }
}


Java
// Java program to iterate 
// LinkedHashMap in reverse order
  
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
  
public class Main {
  
    public static void main(String[] args)
    {
  
        // creating HashMap object of type 
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        System.out.println("Staff DataBase");
  
        // adding key-value pairs to HashMap object
        lhm.put("CSE", "Subba Rao");
        lhm.put("IT", "Maruti");
        lhm.put("Civil", "Sundari Devi");
  
        // Insertion Order iterating
        System.out.println(
            "Insertion Order of LinkedHashMap:"
            + " iterating \n");
  
        // getting keySet() into Set
        Set set = lhm.keySet();
  
        // get Iterator from key set
        Iterator itr = set.iterator();
  
        // iterating as per Insertion Order
        while (itr.hasNext()) {
            String key = itr.next();
            System.out.println("Key : " + key + "\t\t"
                               + "Value : " + lhm.get(key));
        }
  
        // Reverse of Insertion Order iterating
        System.out.println("\n\nReverse of Insertion Order:"
                           + " iterating \n");
  
        // convert to ArrayList of key set
        List alKeys
            = new ArrayList(lhm.keySet());
  
        // reverse order of keys
        Collections.reverse(alKeys);
  
        // iterate LHM using reverse order of keys
        for (String strKey : alKeys) {
            System.out.println("Key : " + strKey + "\t\t"
                               + "Value : "
                               + lhm.get(strKey));
        }
    }
}


Java
// Java program to iterate 
// LinkedHashMap in reverse order
  
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
  
public class LinkedHashMapIterateReverseExample {
  
    public static void main(String[] args)
    {
  
        // create an instance of linked hashmap
        LinkedHashMap lhmap
            = new LinkedHashMap();
  
        // Add mappings
        lhmap.put("one", "Geeks");
        lhmap.put("two", "For");
        lhmap.put("three", "Geeks");
  
        // get all keys from the LinkedHashMap
        Set setKeys = lhmap.keySet();
  
        // convert set to list
        List listKeys
            = new ArrayList(setKeys);
  
        // get a ListIterator for the ArrayList and
        // position it at the end to iterate backwards
        ListIterator iterator
            = listKeys.listIterator(listKeys.size());
  
        // Iterate in reverse order using the hasPrevious
        // and previous methods
        while (iterator.hasPrevious()) {
  
            String key = iterator.previous();
  
            // get value mapped to the key
            System.out.println(key + " " + lhmap.get(key));
        }
    }
}


Java
// Java program to iterate 
// LinkedHashMap in reverse order
  
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Set;
  
public class LinkedHashMapIterateReverseExample {
  
    public static void main(String[] args)
    {
  
        // create an instance of linked hashmap
        LinkedHashMap lhmap
            = new LinkedHashMap();
  
        // Add mappings
        lhmap.put("one", "Geeks");
        lhmap.put("two", "For");
        lhmap.put("three", "Geeks");
  
        // get all keys from the LinkedHashMap
        Set setKeys = lhmap.keySet();
  
        // convert set to LinkedList
        LinkedList listKeys
            = new LinkedList(setKeys);
  
        // get descending iterator
        Iterator iterator
            = listKeys.descendingIterator();
  
        // iterate the keys and get the values from the
        // map
        while (iterator.hasNext()) {
  
            String key = iterator.next();
  
            // get the value
            System.out.println(key + " " + lhmap.get(key));
        }
    }
}


输出
Student Details:
Insertion Order of LinkedHashMap: iterating 

Key : 1        Value : Sravan Kumar
Key : 2        Value : Ishitha
Key : 3        Value : Harsha
Key : 4        Value : Vamsi
Key : 5        Value : Jyothika


Reverse of Insertion Order: iterating 

Key : 5        Value : Jyothika
Key : 4        Value : Vamsi
Key : 3        Value : Harsha
Key : 2        Value : Ishitha
Key : 1        Value : Sravan Kumar

示例 2:键和值都是字符串类型。

Java

// Java program to iterate 
// LinkedHashMap in reverse order
  
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
  
public class Main {
  
    public static void main(String[] args)
    {
  
        // creating HashMap object of type 
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        System.out.println("Staff DataBase");
  
        // adding key-value pairs to HashMap object
        lhm.put("CSE", "Subba Rao");
        lhm.put("IT", "Maruti");
        lhm.put("Civil", "Sundari Devi");
  
        // Insertion Order iterating
        System.out.println(
            "Insertion Order of LinkedHashMap:"
            + " iterating \n");
  
        // getting keySet() into Set
        Set set = lhm.keySet();
  
        // get Iterator from key set
        Iterator itr = set.iterator();
  
        // iterating as per Insertion Order
        while (itr.hasNext()) {
            String key = itr.next();
            System.out.println("Key : " + key + "\t\t"
                               + "Value : " + lhm.get(key));
        }
  
        // Reverse of Insertion Order iterating
        System.out.println("\n\nReverse of Insertion Order:"
                           + " iterating \n");
  
        // convert to ArrayList of key set
        List alKeys
            = new ArrayList(lhm.keySet());
  
        // reverse order of keys
        Collections.reverse(alKeys);
  
        // iterate LHM using reverse order of keys
        for (String strKey : alKeys) {
            System.out.println("Key : " + strKey + "\t\t"
                               + "Value : "
                               + lhm.get(strKey));
        }
    }
}
输出
Staff DataBase
Insertion Order of LinkedHashMap: iterating 

Key : CSE        Value : Subba Rao
Key : IT        Value : Maruti
Key : Civil        Value : Sundari Devi


Reverse of Insertion Order: iterating 

Key : Civil        Value : Sundari Devi
Key : IT        Value : Maruti
Key : CSE        Value : Subba Rao

方法二:(使用listIterator方法)

句法:

ListIterator listIterator(int index)

参数:此方法只有一个参数,即 index – 要从列表迭代器(通过调用 next)返回的第一个元素的索引。

返回:此方法返回列表中元素的列表迭代器(以适当的顺序),从列表中的指定位置开始。

Java

// Java program to iterate 
// LinkedHashMap in reverse order
  
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
  
public class LinkedHashMapIterateReverseExample {
  
    public static void main(String[] args)
    {
  
        // create an instance of linked hashmap
        LinkedHashMap lhmap
            = new LinkedHashMap();
  
        // Add mappings
        lhmap.put("one", "Geeks");
        lhmap.put("two", "For");
        lhmap.put("three", "Geeks");
  
        // get all keys from the LinkedHashMap
        Set setKeys = lhmap.keySet();
  
        // convert set to list
        List listKeys
            = new ArrayList(setKeys);
  
        // get a ListIterator for the ArrayList and
        // position it at the end to iterate backwards
        ListIterator iterator
            = listKeys.listIterator(listKeys.size());
  
        // Iterate in reverse order using the hasPrevious
        // and previous methods
        while (iterator.hasPrevious()) {
  
            String key = iterator.previous();
  
            // get value mapped to the key
            System.out.println(key + " " + lhmap.get(key));
        }
    }
}
输出
three Geeks
two For
one Geeks

方法三:(使用descendingIterator方法)

句法:

public Iterator descendingIterator()

返回值:此方法以相反的顺序返回此 LinkedList 中元素的迭代器

Java

// Java program to iterate 
// LinkedHashMap in reverse order
  
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Set;
  
public class LinkedHashMapIterateReverseExample {
  
    public static void main(String[] args)
    {
  
        // create an instance of linked hashmap
        LinkedHashMap lhmap
            = new LinkedHashMap();
  
        // Add mappings
        lhmap.put("one", "Geeks");
        lhmap.put("two", "For");
        lhmap.put("three", "Geeks");
  
        // get all keys from the LinkedHashMap
        Set setKeys = lhmap.keySet();
  
        // convert set to LinkedList
        LinkedList listKeys
            = new LinkedList(setKeys);
  
        // get descending iterator
        Iterator iterator
            = listKeys.descendingIterator();
  
        // iterate the keys and get the values from the
        // map
        while (iterator.hasNext()) {
  
            String key = iterator.next();
  
            // get the value
            System.out.println(key + " " + lhmap.get(key));
        }
    }
}
输出
three Geeks
two For
one Geeks