Java Continuous Data grouping

Source: Internet
Author: User

Requirement: divide the number of consecutive numbers into one group, and disconsecutive groups into another group. For example, 1, 2, 3, 5, 7, and 8, output 1-3, 5-5, and 7-8.

The bean object stores the maximum and minimum values of the group. You can also add a number to the group.CodeAs follows:

 
Public class bean {private int minvalue; private int maxvalue; Public Boolean add (INT value) {If (value> = minvalue & value <= maxvalue) {// do nothingreturn true;} If (value = minvalue-1) {// smaller than the minimum value 1, expanding the minimum value minvalue = value; return true;} If (value = maxvalue + 1) {// if the value is 1 greater than the maximum value, maxvalue = value; return true;} is extended ;} // otherwise, this interval is added. Return false;} public int getminvalue () {return minvalue;} public void setminvalue (INT minvalue) {This. minvalue = minvalue;} public int getmaxvalue () {return maxvalue;} public void setmaxvalue (INT maxvalue) {This. maxvalue = maxvalue ;}}

The beanlist class is a simple encapsulation of the list set. You can also add a number to a group. Because you add a number to a group, the two groups may be merged into one group. For example, after adding 1-3 and 5-6 groups to 4, it must be merged into 1-6 groups. The following method implements logic: If a number is successfully added to the two groups, the two groups can be merged.

Public class beanlist {private list <bean> beanlist = new arraylist <bean> (); Public void add (INT value) {bean existbean = NULL; For (bean: beanlist) {If (bean. add (value) {If (existbean! = NULL) {// Add a data record to merge Merge (bean, existbean); return;} existbean = bean;} If (existbean = NULL) {addifnotexist (value) ;}} private void addifnotexist (INT value) {bean = new bean (); bean. setminvalue (value); bean. setmaxvalue (value); beanlist. add (bean);} private void Merge (bean existbean, bean deletebean) {If (existbean. getmaxvalue () = deletebean. getminvalue () {existbean. setmaxvalue (deletebean. getmaxvalue (); beanlist. remove (deletebean);} else if (existbean. getminvalue () = deletebean. getmaxvalue () {existbean. setminvalue (deletebean. getminvalue (); beanlist. remove (deletebean) ;}} public void print () {for (bean: beanlist) {system. out. println (bean. getminvalue () + "======" + bean. getmaxvalue ());}}}

 

Test class. Beanlist simply prints the results.

 
Public class test {public static void main (string [] ARGs) {list <integer> List = new arraylist <integer> (); list. add (8); list. add (10); list. add (9); list. add (1); list. add (3); list. add (4); list. add (5); list. add (7); list. add (6); beanlist = new beanlist (); For (integer AA: List) {beanlist. add (AA. intvalue ();} beanlist. print ();}}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.