2

I have a Firebase database which has this structure

{
  "foo" : {
    "data" : {
      "2019-12-01" : [ {
        "item1" : 8,
        "item2" : 16,
        "name" : "user1"
      }, {
        "item1" : 9,
        "item2" : 18,
        "name" : "user2"
      } ],
      "2019-12-02" : [ {
        "item1" : 2,
        "item2" : 26,
        "name" : "user1"
      }, {
        "item1" : 6,
        "item2" : 6,
        "name" : "user2"
      } ]
    }
  }
}

I'm filling a recyclerviewer in the main activity in a very simple way:

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;

    private RecyclerView.LayoutManager layoutManager;
    private Adapter adapter;

    final List<Model> tempList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        recyclerView = findViewById(R.id.simpleRecycler);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);

        Query statsRef = FirebaseDatabase.getInstance()
                .getReference("foo")
                .child("data")
                .orderByKey()
                .startAt("2019-12-01")
                .endAt("2019-12-02");

        // Read from the database
        statsRef.addValueEventListener(new ValueEventListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot itemSnapshot : dataSnapshot.getChildren()){
                    for (int i = 0; i < 2; i++) {
                        Model model = itemSnapshot.child(String.valueOf(i)).getValue(Model.class);
                        tempList.add(model);
                    }
                }

                adapter = new Adapter(tempList);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }
}

And the adapter is:

public class Adapter extends RecyclerView.Adapter {

    List<Model> models;

    public Adapter(List<Model> models) {
        this.models = models;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_row, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder)holder;

        vh.name.setText(models.get(position).getName());
        vh.item1.setText(String.valueOf(models.get(position).getItem1()));
        vh.item2.setText(String.valueOf(models.get(position).getItem2()));
    }

    @Override
    public int getItemCount() {
        return models.size();
    }
}

The result is a table like this:

+-------+---+----+
| user1 | 8 | 16 |
+-------+---+----+
| user2 | 9 | 18 |
+-------+---+----+
| user1 | 2 | 26 |
+-------+---+----+
| user2 | 6 |  6 |
+-------+---+----+

The problem is that I want to group the list by the name and then sum all the other relative fields like below

+-------+----+----+
| user1 | 10 | 42 |
+-------+----+----+
| user2 | 15 | 24 |
+-------+----+----+

I have tried with no success:

tempList.stream().collect(Collectors.groupingBy(Model::getName))
                    .entrySet().stream()
                    .collect(Collectors.toMap(x -> {
                        int item1 = x.getValue().stream().mapToInt(Model::getItem1).sum();
                        int item2 = x.getValue().stream().mapToInt(Model::getItem2).sum();

                        return new Model(x.getKey(), item1, item2);

                    }, Map.Entry::getValue));

I've added the above code above before setting the adapter to the recyclerviewer.

Any help, please?

Naman
  • 27,789
  • 26
  • 218
  • 353
Erjon
  • 923
  • 2
  • 7
  • 32

1 Answers1

5

You can use a Collectors.toMap operation with a mergeFunction defined to perform the aggregation such as:

List<Model> output = new ArrayList<>(tempList.stream()
        .collect(Collectors.toMap(Model::getName, Function.identity(),
                Model::mergeSimilarNames))
        .values()); // interested in merged output 

where the merge function could be defined within Model class as:

static Model mergeSimilarNames(Model one, Model two) {
    return new Model(one.getName(), one.getItem1() + two.getItem1(), one.getItem2() + two.getItem2());
}

To relate this with your existing approach, you need a reducing operation after grouping the elements, such as:

Map<String, Optional<Model>> grouping = tempList.stream()
        .collect(Collectors.groupingBy(Model::getName,
                Collectors.reducing(Model::mergeSimilarNames)));

But then you are only interested in getting the final view as the List<Model> amongst the value of above Map, hence your resultant code would be in such case :

List<Model> output = tempList.stream()
        .collect(Collectors.groupingBy(Model::getName,
                Collectors.reducing(Model::mergeSimilarNames)))
        .values().stream()
        .filter(Optional::isPresent)
        .map(Optional::get)
        .collect(Collectors.toList());

But do note, toMap(f1,f2,f3) is preferred over groupingBy(f1, reducing(f2,f3)).

Naman
  • 27,789
  • 26
  • 218
  • 353