Import java.util.ArrayList; Import java.util.Collection; Import java.util.Collections; Import java.util.Comparator; Import java.util.List; / ** * @author Ma Jiali * @version Created: 2019 September 18 * @Description: TODO Comparator group test * / public class ComparatorGroup { class Nubia { public String name; // phone's name public String color; // color public int weight; // weight of public Nubia(String name, String color, int weight) { super(); this.name = name; this.color = color; this.weight = weight; } @Override public String toString() { return "Nubia [name=" + name + ",color=" + color + "色, weight=" + weight + "g]"; } } /** * @Title:divider * @author:马家立 * @date:2019年9月17日 * @Description:TODO 按条件分组 * @param<T> * @param datas_ data packet to be List * @param determines whether the same conditions condition_ group * @return List <List <T >> * / public static <T> List <List <T >> Divider (Collection <T> DATAS, Comparator <? Super T> for condition Condition) { // initialization packet words List <List <T >> List <List <T >> Result = new new the ArrayList <List <T >> (); // identifying whether the same set of data Boolean isSameGroup = to false ; // traverse the data packet to be complete List for (T data:dates) { isSameGroup= false; for (int j = 0; j < result.size(); j++) { if (condition.compare(data, result.get(j).get(0)) == 0) { isSameGroup = true; result.get(j).add(data); break; } } if (!isSameGroup) { // 创建分组List List<T> groupList = new ArrayList<T>(); result.add(groupList); groupList.add(data); } } ReturnResult; } / ** * @Title: sortListNubia * @author : Ma Jiali * @date: 2019 dated. 17 years. 9 Day * @Description: TODO arranged in ascending order of the packet by weight List * @param sortNubia be sorted List * void * / public static void sortListNubia (List <List <Nubia >> sortNubia) { // packet in ascending order of weight List Comparator <List <>> Nubia Comparator = new new Comparator <List <Nubia >> () { @Override public int Compare (List <Nubia> NB1, List <Nubia> NB2) { returnnb1.get (0) .Weight - nb2.get (0 ) .Weight; } }; the Collections.sort (sortNubia, Comparator); } / ** * @Title: main * @author : Ma Jiali * @date: 2019 years September 17 * @Description: TODO main function tests * @param args * void * / public static void main (String [] args) { // initialization List data List <Nubia> List = new new the ArrayList <> (); List .add ( new new ComparatorGroup (). new newNubia ( "Nubian z7mini", "black gold", 143 )); List.add ( new new . ComparatorGroup () new new Nubia ( "Nubian z11mini", "black gold", 138 )); List.add ( new new ComparatorGroup . () new new Nubia ( "Nubian Mars Red Devils", "black and red", 193 )); List.add ( new new . ComparatorGroup () new new Nubia ( "Nubian Red Devils 3", "black and red", 215 ) ); List.add ( new new . ComparatorGroup () new new Nubia ( "Nubian X", "blue silver", 181 )); List.add ( new new . ComparatorGroup () new new Nubia ( "Nubian Prague" grouped by color)); // List <List <Nubia >> byColors = Divider (List, new new Comparator <Nubia> () { @Override public int Compare (Nubia NB1, NB2 Nubia) { // grouped by color return nb1.color.compareTo (nb2.color) ; } }); // by weight in ascending sortListNubia (byColors); System.out.println ( "grouped by color:" ); for (List <Nubia> List2: byColors) { System.out.println (List2); } // grouped by weight List <List <Nubia >> byWeight = Divider (List, new new Comparator <Nubia> () { @Override public int Compare (Nubia NB1, NB2 Nubia) { // weight range return ((nb1.weight / 30) == (nb2.weight / 30)) 0:?. 1 ; } }); // by weight in ascending sortListNubia (byWeight); System.out.println ( "heavyweight grouped by:" ); for (List <Nubia> List2: byWeight) { System.out.println (List2); } } }
operation result:
Grouped by color: [Nubia [name = Nubian z7mini, color = black and gold, weight = 143g], Nubia [ name = Nubian z11mini, color = black and gold, weight = ] 138 g of] [Nubia [name = Nubi alkylene X, color = blue and silver, weight = 181g], Nubia [ name = Nubian Prague, color = blue and silver, weight = 140 g of]] [Nubia [name = Nubian reds Mars, color = black and red, weight = 193g], Nubia [name = Nubian reds 3, color = black and red, weight = 215g]] by heavyweight groups: [Nubia [name = Nubian z7mini, color = black and gold, weight = 143g], Nubia [ name = Nubian z11mini, color = black and gold, weight = 138g], Nubia [ name = Nubian Prague, color = blue and silver, weight = 140 g of]] [Nubia [name = Nubian reds Mars, color = black red, weight = 193g], Nubia [ name = Nubian X, color = blue and silver, weight = 181g]] [Nubia [name = Nubian reds 3, color = black and red, weight = 215g]]