-3

I need to get unique key and that key related object variable in HashMap.

I have a list of items

public class Java8Examples3 {

    public static void main(String[] args) {

        // 3 apple, 2 banana, others 1
        List<Item> items = Arrays.asList(new Item("KPIT", 10, new BigDecimal("9.99")),
                new Item("CTS", 20, new BigDecimal("19.99")), new Item("WIPRO", 10, new BigDecimal("29.99")),
                new Item("INFOSYS", 10, new BigDecimal("29.99")), new Item("ESTUATE", 20, new BigDecimal("9.99")),
                new Item("KPIT", 10, new BigDecimal("9.999")), new Item("CTS", 10, new BigDecimal("19.99")),
                new Item("KPIT", 20, new BigDecimal("9.999")));

        Map<String, List<Item>> counting = items.stream()
                .collect(Collectors.groupingBy(Item::getName, Collectors.toList()));

    }
}

My Output will look like this:

KPIT: 10,10,20
CTS:20,10
WIPRO:10
INFOSYS:10
TCS:20
Lino
  • 19,604
  • 6
  • 47
  • 65
waseem
  • 1
  • 3
  • 1
    And where are you having problems with / what is your **question**? – Lino Jun 05 '19 at 12:33
  • 1
    In which order? Maps are by default not ordered. You can use a LinkedHashMap for insertion order, or a TreeMap that sorts elements based on your self written Comparator. – GhostCat Jun 05 '19 at 12:34
  • Related: [How to sort a HashMap](https://stackoverflow.com/questions/780541/how-to-sort-a-hashmap-in-java) and [How to preserve insertion order in HashMap?](https://stackoverflow.com/questions/10710193/how-to-preserve-insertion-order-in-hashmap) – Lino Jun 05 '19 at 12:36
  • For each name have some Items(objects). so requirement is for each name like KPIT new Item("KPIT", 10, new BigDecimal("9.99"), new Item("KPIT", 10, new BigDecimal("9.999")),new Item("KPIT", 20, new BigDecimal("9.999")) because we are grouping by iteam name. So i cant able to get the appropriate result to get like that – waseem Jun 05 '19 at 12:40

1 Answers1

0
public class Java8Examples3 {

public static void main(String[] args) {

    // 3 apple, 2 banana, others 1
    List<Item> items = Arrays.asList(new Item("KPIT", 10, new BigDecimal("9.99")),
            new Item("CTS", 20, new BigDecimal("19.99")), new Item("WIPRO", 10, new BigDecimal("29.99")),
            new Item("INFOSYS", 10, new BigDecimal("29.99")), new Item("ESTUATE", 20, new BigDecimal("9.99")),
            new Item("KPIT", 10, new BigDecimal("9.999")), new Item("CTS", 10, new BigDecimal("19.99")),
            new Item("KPIT", 20, new BigDecimal("9.999")));

    Map<String, List<Item>> counting = items.stream()
            .collect(Collectors.groupingBy(Item::getName, Collectors.toList()));

    Map<String, List<Integer>> mapQtyData = listQtyData(counting);

    System.out.println(mapQtyData);

}

private static Map<String, List<Integer>> listQtyData(Map<String, List<Item>> counting) {
    int qty = 0;
    List<Integer> listQty = new ArrayList<>();
    Map<String, List<Integer>> data = new HashMap<>();
    String key = "";
    for (String m : counting.keySet()) {
        key = m;
        List<Item> iteams = counting.get(key);

        for (int i = 0; i < iteams.size(); i++) {
            qty = iteams.get(i).getQty();
            listQty.add(qty);
        }
        data.put(key, listQty);
    }

    return data;
}

}

O/p:{CTS=[20, 10, 10, 10, 20, 20, 10, 10], KPIT=[20, 10, 10, 10, 20, 20, 10, 10], ESTUATE=[20, 10, 10, 10, 20, 20, 10, 10], WIPRO=[20, 10, 10, 10, 20, 20, 10, 10], INFOSYS=[20, 10, 10, 10, 20, 20, 10, 10]}

waseem
  • 1
  • 3