【问题标题】:Java - How to Loop and Print Values From 2 HashMaps on the Same Line?Java - 如何在同一行上循环和打印来自 2 个 HashMap 的值?
【发布时间】:2017-02-19 11:58:27
【问题描述】:

我发现了相关问题,但我不确定如何使用 for 循环来做到这一点。我有两个由用户输入填充的 HashMap。两张地图都有匹配的键。我正在尝试提出一个最终报告,该报告将提取每个地图的匹配键和值,所有这些都打印在同一行上。截至目前,我的代码正在打印第一个 HashmMap(Products),然后是第二个 MashMap(Price) 的值。

电流输出

Priority Product  Price

1 apple  
2 banana   
3 orange  
$ 43.81   
$ 69.64   
$ 96.35  

我考虑过创建第三个哈希图并将所有值合并为一个,但我不确定这是否真的有效。

期望的输出

Priority Product Price

1 apple  $ 43.81  
2 banana $ 69.64   
3 orange $ 96.35  

我的代码

import java.util.*; 


public class Testing 
{

    String products;
    int priority;
    double price; 


    Scanner keyboard = new Scanner(System.in); //Scanner 

    HashMap<Integer, String> pMap = new HashMap<Integer, String>(); // Product HashMap

    HashMap<Integer, Double> priceMap = new HashMap<Integer, Double>(); //Price HashMap

    Random r = new Random(); //Random number


    public void testingItems()
    {


        for ( int count = 0; count < 3; count++)
            {
                //Ask for user input to Add product name and product priority 

                    System.out.println("\nEnter a Product"); 
                    products = keyboard.nextLine();


                    System.out.println("\nEnter a Priority");
                    priority = keyboard.nextInt();

                    price = (r.nextDouble() * 100); 
                    //System.out.print(price);


                    keyboard.nextLine(); 


                //*** HASHMAPS *** 

                    //Add product and priority to HashMap 



                    pMap.put(priority, products);

                    priceMap.put(priority, price); 



                    System.out.println("Product: "+ products + "\t\t" + "Priority: " +  priority + "\n\n" + "Price:" + " $ "+ price);
            }


                    System.out.println("\nPriority  " + "  Product  " + "  Price");
                    System.out.println("----------------------------------------");

                    for (Integer key: pMap.keySet())
                    {                                                                                   
                        System.out.println(key + "\t" + pMap.get(key));
                    }
                        for (Integer key2: priceMap.keySet()){
                            System.out.println("$ " + priceMap.get(key2));
                        }

            }
    }

【问题讨论】:

  • 您能否完善您的问题,我认为您是在尝试迭代 2 [任意] 地图并将它们各自的值打印到同一行?
  • 是的,我就是这个意思。我在遍历两个地图时遇到了麻烦,同时确保输出值会打印在同一行。
  • HashMap 不保留插入顺序,这意味着不能保证您的两个关键循环将以相同的顺序运行。如果您想坚持使用 2 张地图,请考虑改用 LinkedHashMap

标签: java for-loop hashmap


【解决方案1】:

为了您自己的理智,您可能想引入一个具有优先级、名称和价格的Product 类。但是要按要求回答问题:

pMap.forEach((p, n) -> System.out.format("%d %s $%2f\n", p, n, priceMap.get(p)));

如果您创建了一个Product 类(推荐),其自然顺序 (implements Comparable&lt;Product&gt;) 由priority 定义,您可以将它们存储在TreeSet&lt;Product&gt; 中。然后将按优先顺序对集合进行迭代。

【讨论】:

  • 谢谢!!这是一个非常酷的单行解决方案。我喜欢这个,因为它还可以帮助我解决我正在处理的另一个格式问题。是的,我肯定会添加一个 Product 类。 :)
【解决方案2】:

如果您不想要换行符,请使用 print 而不是 println(因为您希望将文本保留在同一行,所以您不需要。请记住以 println() 或 \n 结尾获取下一行的下一条记录)。

【讨论】:

    【解决方案3】:

    您可以同时遍历两个映射并根据其优先级索引访问元素。

    for (int priority : pMap.keySet()) {
         System.out.print(priority);   //prints the priority
         System.out.print(pMap.get(priority));  // prints the product for the priority
         System.out.print(priceMap.get(priority));  //prints the price 
         System.out.println();  // changes the line
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多