Skip to main content

groupingby-and-mapping-example Map in andorid




 

https://www.mkyong.com/java8/java-8-collectors-groupingby-and-mapping-example/

 
Map<String, List<PieDataChart>> map = new HashMap<String, List<PieDataChart>>();



for (PieDataChart student : lstItemArry) {

    String key  = String.valueOf(student.getProduct_id());

    if(map.containsKey(key)){

        List<PieDataChart> list = map.get(key);

        list.add(student);



    }else{

        List<PieDataChart> list = new ArrayList<PieDataChart>();

        list.add(student);

        map.put(key, list);

    }



}
 
 
for (Map.Entry<String, List<PieDataChart>> entry : map.entrySet()) {

   //  System.out.println("Key : " + entry.getKey() + " value : " + entry.getValue());



    // for (int i=0 ; i< entry.getKey().length(); i++){

     int sum = 0, value;

         for (int j = 0; j < entry.getValue().size(); j++){



             value = Integer.valueOf(String.valueOf(entry.getValue().get(j)));

             //System.out.println(j+ "value = " + value);

             sum += value;



           //  System.out.println(j+"chgevalue = " + sum);

         }

// }

     System.out.println( "Key : " + entry.getKey() + "qty = " + sum);



 }
 

 

2.2 Group by the name + Count or Sum the Qty.
Java8Examples3.java
package com.mkyong.java8;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Java8Examples3 {

    public static void main(String[] args) {

        //3 apple, 2 banana, others 1
        List<Item> items = Arrays.asList(
                new Item("apple", 10, new BigDecimal("9.99")),
                new Item("banana", 20, new BigDecimal("19.99")),
                new Item("orang", 10, new BigDecimal("29.99")),
                new Item("watermelon", 10, new BigDecimal("29.99")),
                new Item("papaya", 20, new BigDecimal("9.99")),
                new Item("apple", 10, new BigDecimal("9.99")),
                new Item("banana", 10, new BigDecimal("19.99")),
                new Item("apple", 20, new BigDecimal("9.99"))
        );

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

        System.out.println(counting);

        Map<String, Integer> sum = items.stream().collect(
                Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));

        System.out.println(sum);

    }
}
Copy
output
//Group by + Count
{
        papaya=1, banana=2, apple=3, orang=1, watermelon=1
}

//Group by + Sum qty
{
        papaya=20, banana=30, apple=40, orang=10, watermelon=10
}

 

 

https://www.codingame.com/playgrounds/6162/6-ways-to-iterate-or-loop-a-map-in-java

Using entrySet()



// { autofold
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {

public static void main(String[] args) {
// }

    Map<Integer, String> customers = new HashMap<>();
              customers.put(1, "Jhon");
              customers.put(2, "Smith");
              customers.put(3, "Sally");

    for (Map.Entry<Integer, String> entry : customers.entrySet()) {
      System.out.println("Key : " + entry.getKey() + " value : " + entry.getValue());
    }





https://www.baeldung.com/java-array-sum-average
2.1. Sum Using a For Loop
In order to find the sum of all elements in an array, we can simply iterate the array and add each element to a sum accumulating variable.
This very simply starts with a sum of 0 and add each item in the array as we go:
1
2
3
4
5
6
7
public static int findSumWithoutUsingStream(int[] array) {
    int sum = 0;
    for (int value : array) {
        sum += value;
    }
    return sum;
}



Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App § ...

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap th...