From Link: http://www.cnblogs.com/davidwang456/p/4514659.html
first, why use tuple tuple ?
Tuples, like list lists, can be used for data storage, including multiple data, but unlike lists: Lists can only store the same data type, and tuples are different, and they can store various data types, such as storing int, string, list, etc. at the same time, and can be infinitely expanded as required.
For example, in Web applications, often encounter a problem is the data paging problem, query paging needs to include several information: the current page, page size, query results return data is: the current pages of data records, but if you need to display the current page, page size, total number of pages and other information in the foreground, There must be another message: The total number of data records, and then based on the above information to calculate the total pages and other information. This time to query a page of information needs to return two data types, one is the list (the current data record), one is an int (total number of records). Of course, these two values can be obtained entirely in two methods and two database connections. In fact, when querying the list, has been through the SQL query total record number, if you open a method, then do a database connection to query the total record number, unavoidably a bit superfluous, wasting time, wasting code, wasting life. Strongly worded ~ In this case, we can use the two-tuple, in a database connection, to get the total record, the current page records, and stored in it, simple and clear!
Ii. Examples of source code
Two-tuple:
| 1234567891011121314151617 |
/** <p>Title: TwoTuple</p> * <p>Description: 两个元素的元组,用于在一个方法里返回两种类型的值</p> * @author [email protected] * @site http://wzwahl36.net * @version 2012-3-21 上午11:15:03 * @param <A> * @param <B> */publicclassTwoTuple<A, B> { publicfinalA first; publicfinalB second; publicTwoTuple(A a, B b) { this.first = a; this.second = b; }} |
Expand to triples (click here to expand arbitrarily)
| 123456789101112131415161718 |
/** * <p>Title: ThreeTuple</p> * <p>Description: 三个元素的元组,用于在一个方法里返回三种类型的值</p> * @author [email protected] * @site http://wzwahl36.net * @version 2012-3-21 上午11:15:50 * @param <A> * @param <B> * @param <C> */publicclassThreeTuple<A, B, C> extendsTwoTuple<A, B> { publicfinalC third; publicThreeTuple(A a, B b, C c) { super(a, b); this.third = c; }} |
Tuple Operations Tool class, Test class (customizable on Demand)
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
import java.util.ArrayList;importjava.util.List;importcom.bluesea.bean.GoodsBean;/** * <p>Title: TupleUtil</p> * <p>Description: * 元组辅助类,用于多种类型值的返回,如在分页的时候,后台存储过程既返回了查询得到的 * 当页的数据(List类型),又得到了数据表中总共的数据总数(Integer类型),然后将这 * 两个参数封装到该类中返回到action中使用 * 使用泛型方法实现,利用参数类型推断,编译器可以找出具体的类型 * </p> * @author [email protected] * @site http://wzwahl36.net * @version 2012-3-21 上午09:59:39 * @param <A> * @param <B> */publicclassTupleUtil { publicstatic<A, B> TwoTuple<A, B> tuple(A a, B b) { returnnewTwoTuple<A, B>(a, b); } publicstatic<A, B, C> ThreeTuple<A, B, C> tuple(A a, B b, C c) { returnnew ThreeTuple<A, B, C>(a, b, c); } // 测试 publicstaticvoidmain(String[] args) { List<GoodsBean> goodsBeans = new ArrayList<GoodsBean>(); for(inti = 1; i < 26; i++) { GoodsBean goodsBean = newGoodsBean(); goodsBean.setGoodsId(i); goodsBeans.add(goodsBean); } Integer totalProperty = 47;// TupleUtil<List<GoodsBean>, Integer> tupleUtil = new TupleUtil<List<GoodsBean>, Integer>(goodsBeans, totalProperty); TwoTuple<List<GoodsBean>, Integer> twoTuple = TupleUtil.tuple(goodsBeans, totalProperty); List<GoodsBean> list = twoTuple.first; System.out.println(list); System.out.println(twoTuple.second); }} |
Java tuple usage instance (GO)