Java Collection test

Source: Internet
Author: User

Package src.com.ant.test;

Import java.util.ArrayList;

Import Java.util.HashMap;

Given a collection of strings, the format is as follows:
{AAA BBB CCC}, {bbb ddd},{eee fff},{ggg},{ddd HHH}
Requires merging collections where the intersection is not empty, requiring that there be no intersection between the collections after the completion of the merge, such as the previous example should

Output
{AAA BBB CCC DDD Hhh},{eee FFF}, {GGG}
(1) Please describe your idea of solving this problem;
(2) Please give the main processing flow, algorithm, and the complexity of the algorithm
(3) Please describe possible improvements (the direction of improvement such as effect, performance, etc., which is an open issue).

/**

* Collection Topics

* <p> (1) 22 to determine whether there is an intersection, there is a change in the record set of the value of the collection, otherwise save, and finally set the collection also save

* <p> (2) Implementation of the following check method and Unite method in the worst case detection times will not exceed the longest set of detection times

* <p> with N sets, the longest set element number m, in the worst case will detect m*m* (n-1) times, and merging will not exceed this worst-case

* <p> the whole process down to the complexity of O (m*m*n*n)

* <p> (3) can be used in the check method and Unite method to use other methods to quickly judge and fetch the collection

* @author Savagegarden

*

*/

public class HelloWorld {

public static int length = 0;

public static ArrayList initlist = new ArrayList ();

public static ArrayList resultlist = new ArrayList ();

public static void Main (string[] args) {

Initlist = init ();

System.out.println ("Before merging: Initlist-----------");

for (int i = 0; i < initlist.size (); i++) {

for (int j = 0; J < ((string[)) Initlist.get (i)). length; J + +) {

System.out.print (((string[)) Initlist.get (i)) [j]+ ",");

}

System.out.println ();

}

Resultlist = MainFunc (initlist);

System.out.println ("merged: Resultlist---------");

for (int i = 0; i < resultlist.size (); i++) {

for (int j = 0; J < ((string[)) Resultlist.get (i)). length; J + +) {

System.out.print (((string[)) Resultlist.get (i)) [j]+ ",");

}

System.out.println ();

}

}

/**

* Initialize list to get an array of strings to test

* @author Savagegarden

* @return

*/

public static ArrayList init () {

ArrayList list = new ArrayList ();

Initializing an array

String[] Array1 = {"AAA", "BBB", "CCC"};

String[] Array2 = {"BBB", "ddd"};

String[] Array3 = {"Eee", "fff", "RRR"};

String[] Array4 = {"GGG", "RRR"};

String[] Array5 = {"ddd", "HHH"};

Add to List

List.add (array1);

List.add (array2);

List.add (ARRAY3);

List.add (ARRAY4);

List.add (ARRAY5);

return list;

}

/**

* Merging two strings to get the collection of their contents

* @author Savagegarden

* @param a

* @param b

*/

public static string[] Unite (string[] a,string[] b) {

if (a.length > 0 && b.length > 0) {

First instantiate a new string array to store the merged string array

Length of the global variable

string[] unitestring = new String[length];

int status = 0;//record unitestring change of subscript

If a is a smaller length, the a,b is replaced

if (A.length < b.length) {

String[] C = A;

A = b;

b = C;

}

First, the smaller string array is placed inside the unitestring

for (int i = 0; i < b.length; i++) {

Unitestring[i] = B[i];

}

Status = b.length;//record unitestring change of subscript

for (int i = 0; i < a.length i + +) {

Boolean flag = true;//tag A,b has the same value

for (int j = 0; J < B.length; J + +) {

if (A[i].equals (B[j])) {

Flag = false;

}

}

if (flag) {

Unitestring[status] = A[i];

Status + +;

}

}

return unitestring;

}

return null;

}

/**

* Verify that there is no intersection between the strings, return true if any, otherwise return false

* @param a

* @param b

* @return

*/

public static Boolean check (string[] a,string[] b) {

Boolean result = false;

if (a.length > 0 && b.length > 0) {

int minlength = A.length < b.length?a.length:b.length;

The result is set to true as long as one element is the same and the intersection is indicated

length = minlength;

if (A.length < b.length) {

String[] C = A;

A = b;

b = C;

}

Boolean flag = false;//tag A,b has the same value

for (int i = 0; i < a.length i + +) {

for (int j = 0; J < B.length; J + +) {

if (A[i].equals (B[j])) {

result = true;

Flag = true;

}

}

if (!flag) {

length + +;

}

Flag = false;

}

}

return result;

}

/**

* To determine whether a string to be added is already in the array

* @param s

* @param list

* @return

*/

public static Boolean Canadd (string[] s,arraylist list) {

for (int i = 0; i < list.size (); i++) {

if (List.get (i). Equals (s)) {

return false;

}

}

return true;

}

/**

* Main method

* <p>for cycle to determine whether there is a intersection between 22, there is a change in the value of unitestring, otherwise added to the Resultlist

* @param initlist

* @return

*/

public static ArrayList MainFunc (ArrayList initlist) {

HashMap HM = new HashMap ();

for (int i = 0; i < initlist.size (); i++) {

Hm.put (New Long (i), "true");

}

string[] unitestring = null;

ArrayList resultlist = new ArrayList ();

for (int i = 0; i < initlist.size (); i++) {

if (Hm.get (The new Long (i)). Equals ("true")) {

for (int j = i+1; j< initlist.size (); j + +) {

if (unitestring = = null) {

if (check (string[)) Initlist.get (i), (string[)) Initlist.get (j)) {

Hm.put (New Long (i), "false");

Hm.put (New Long (J), "false");

Unitestring = Unite ((string[)) Initlist.get (i), (string[)) Initlist.get (j));

}else{

if (Canadd (string[]) Initlist.get (j), Resultlist)) {

Resultlist.add ((string[]) Initlist.get (j));

}

}

}else{

if (check (unitestring, (string[)) Initlist.get (j)) {

Hm.put (New Long (i), "false");

Hm.put (New Long (J), "false");

unitestring = Unite (unitestring, (string[)) Initlist.get (j));

}else{

if (Canadd (string[]) Initlist.get (j), Resultlist)) {

Resultlist.add ((string[]) Initlist.get (j));

}

}

}

}

}

}

if (unitestring = = null) {

Unitestring = (string[]) initlist.get (0);

}

Resultlist.add (unitestring);

if (initlist.size () = = Resultlist.size ()) {

return resultlist;

}else{

Return MainFunc (resultlist);

}

}

}

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.