Giter Club home page Giter Club logo

pivotj's Introduction

PivotJ

This is a data structure which likes the pivot table in Excel. It could be used to hierarchically accumulate value in Java, Especially 'Group By' operations.

Why?

In many object-oriented languages, some times it is hard to do 'Group By' like operations to get the value we want just once. We may use a lot of map structures or loops to calculate different values in different layers, which is annoying.

For instance, Hibernate, a Java persistence framework for relational mapping and query databases. We are used to manipulating data in tables likes manipulate objects in Java.

But for 'Group By' operations, we could execute them both in Java and in databases by query(SQL, HQL...etc). In Java implementation, it's very complicate and difficult because of its object-oriented feature.

How

You may query data like this:

SELECT a.company, a.department, a.team, COUNT(*) FROM
Employee a 
Group By a.company, a.department, a.team

and in Java:

....
Query qry = EntityManager.createQuery(queryString);
....	
return qry.getResultList();

query.getResultList() will return a list of Object Arrays:

[...
Object[]{"CompanyA", "DepartmentA", "TeamA", 3},
Object[]{"CompanyA", "DepartmentA", "TeamB", 2},
Object[]{"CompanyB", "DepartmentB", "TeamB", 1},
...]

you might use these result as key to build a Hierarchical Statistic Tree:

IHierarchicalStatisticTree HST = new SumTree();
HST.buildTree(list);

then you cound get the accumulated value by calling:

HST.getResult(new Object[]{"CompanyB", "DepartmentB"});
//1
HST.getResult(new Object[]{"CompanyA"});
//5

The difference between SumTree.java and CountTree.java is that former treats the last element in object array as calculating value, but latter just count every element.

Examples

  • See SumTreeDemo.java, CountTreeDemo.java.

pivotj's People

Contributors

yyc1217 avatar

Stargazers

 avatar Kaizad Avari avatar jiniechen avatar mauricio gamarra avatar  avatar

Watchers

James Cloos avatar Kaizad Avari avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.