Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created March 15, 2016 13:50
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 naoto-ogawa/096eb924bad779a38604 to your computer and use it in GitHub Desktop.
Save naoto-ogawa/096eb924bad779a38604 to your computer and use it in GitHub Desktop.
Grouping Samples
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class GroupingBefore8 {
public static void main(String[] args) {
List<Integer> in = new ArrayList<>();
in.add(1);
in.add(2);
in.add(3);
in.add(1);
in.add(1);
in.add(3);
in.add(3);
in.add(3);
log("** input");
logList(in);
log("** grouping 1");
GroupingBefore8 t = new GroupingBefore8();
List<Integer> ret = t.grouping1(in, Collections.<Integer>reverseOrder());
logList(ret);
log("** grouping 2");
List<List<Integer>> ret2 = t.grouping2(in, Collections.<Integer>reverseOrder());
logListList(ret2);
log("** grouping 3");
List<List<Integer>> ret3 = t.grouping3(in, Collections.<Integer>reverseOrder());
logListList(ret3);
log("** grouping 4");
Map<Integer, List<Integer>> ret4 = t.grouping4(in, Collections.<Integer>reverseOrder());
log(ret4);
}
public <T> List<T> grouping1(List<T> list, Comparator<T> comp) {
List<T> ret = new ArrayList<T>();
List<T> tmp = new ArrayList<T>(list);
while(tmp.size()>0) {
T myT = tmp.get(0);
tmp.remove(myT);
ret.add(myT);
Iterator<T> iter = tmp.iterator();
while(iter.hasNext()) {
T yourT = iter.next();
if (comp.compare(myT, yourT) == 0) {
iter.remove();
}
}
}
return ret;
}
public <T> List<List<T>> grouping2(List<T> list, Comparator<T> comp) {
List<List<T>> ret = new ArrayList<List<T>>();
List<T> tmp = new ArrayList<T>(list);
while(tmp.size()>0) {
T myT = tmp.get(0);
tmp.remove(myT);
List<T> tmpInner = new ArrayList<T>();
tmpInner.add(myT);
Iterator<T> iter = tmp.iterator();
while(iter.hasNext()) {
T yourT = iter.next();
if (comp.compare(myT, yourT) == 0) {
tmpInner.add(yourT);
iter.remove();
}else {
break;
}
}
ret.add(tmpInner);
}
return ret;
}
public <T> List<List<T>> grouping3(List<T> list, Comparator<T> comp) {
List<List<T>> ret = new ArrayList<List<T>>();
List<T> tmp = new ArrayList<T>(list);
while(tmp.size()>0) {
T myT = tmp.get(0);
tmp.remove(myT);
List<T> tmpInner = new ArrayList<T>();
tmpInner.add(myT);
Iterator<T> iter = tmp.iterator();
while(iter.hasNext()) {
T yourT = iter.next();
if (comp.compare(myT, yourT) == 0) {
tmpInner.add(yourT);
iter.remove();
}
}
ret.add(tmpInner);
}
return ret;
}
public <T> Map<T,List<T>> grouping4(List<T> list, Comparator<T> comp) {
Map<T,List<T>> ret = new HashMap<T, List<T>>();
List<T> tmp = new ArrayList<T>(list);
while(tmp.size()>0) {
T myT = tmp.get(0);
tmp.remove(myT);
List<T> tmpInner = new ArrayList<T>();
tmpInner.add(myT);
ret.put(myT, tmpInner);
Iterator<T> iter = tmp.iterator();
while(iter.hasNext()) {
T yourT = iter.next();
if (comp.compare(myT, yourT) == 0) {
tmpInner.add(yourT);
iter.remove();
}
}
}
return ret;
}
private static <T> void logListList(List<List<T>> list) {
for (List<T> item: list) {
System.out.println(item);
}
}
private static <T> void logList(List<T> list) {
System.out.println(list);
}
private static void log(Object obj) {
System.out.println(obj);
}
}
//* input
//[1, 2, 3, 1, 1, 3, 3, 3]
//* grouping 1
//[1, 2, 3]
//* grouping 2
//[1]
//[2]
//[3]
//[1, 1]
//[3, 3, 3]
//* grouping 3
//[1, 1, 1]
//[2]
//[3, 3, 3, 3]
//* grouping 4
//{1=[1, 1, 1], 2=[2], 3=[3, 3, 3, 3]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment