Skip to content

Instantly share code, notes, and snippets.

@ashik94vc
Created February 27, 2017 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashik94vc/77edcf5fc3f4b022e4cbd22f25f4c162 to your computer and use it in GitHub Desktop.
Save ashik94vc/77edcf5fc3f4b022e4cbd22f25f4c162 to your computer and use it in GitHub Desktop.
Java Procedure to Read a list of stringLists to identify recurrences of the string in other lists.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
public class Arrayception {
public static void main(String[] args) {
ArrayList<String> docs1 = new ArrayList();
ArrayList<String> docs2 = new ArrayList();
ArrayList<String> docs3 = new ArrayList();
ArrayList<ArrayList> master = new ArrayList<ArrayList>();
Map<String,ArrayList> sad = new HashMap<>();
//Hardcoded ArrayList Initialization
docs1.add("d1");
docs1.add("d2");
docs1.add("d3");
docs1.add("d4");
docs2.add("d1");
docs2.add("d5");
docs2.add("d6");
docs2.add("d7");
docs2.add("d8");
docs3.add("d1");
docs3.add("d2");
docs3.add("d10");
docs3.add("d11");
//Combine string lists to an arrayList.
master.add(docs1);
master.add(docs2);
master.add(docs3);
int arrayListIndex = 1;
for(ArrayList<String> child:master) {
for(String value: child) {
ArrayList<Integer> lists = sad.containsKey(value)?sad.get(value):new ArrayList<>();
lists.add(arrayListIndex);
sad.put(value,lists);
}
arrayListIndex += 1 ;
}
for(Map.Entry<String,ArrayList> entry : sad.entrySet()) {
System.out.println(entry.getKey()+":"+Arrays.toString(entry.getValue().toArray()));
}
}
}
// Output
// d4:[1]
// d5:[2]
// d6:[2]
// d10:[3]
// d7:[2]
// d8:[2]
// d11:[3]
// d1:[1, 2, 3]
// d2:[1, 3]
// d3:[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment