1. the first way:
Sorting with Google Guava’s ComparisonChain
Collections.sort(pizzas, new Comparator<Pizza>() { @Override public int compare(Pizza p1, Pizza p2) { return ComparisonChain.start().compare(p1.size, p2.size).compare(p1.nrOfToppings, p2.nrOfToppings).compare(p1.name, p2.name).result(); // or in case the fields can be null: /* return ComparisonChain.start() .compare(p1.size, p2.size, Ordering.natural().nullsLast()) .compare(p1.nrOfToppings, p2.nrOfToppings, Ordering.natural().nullsLast()) .compare(p1.name, p2.name, Ordering.natural().nullsLast()) .result(); */ } });
2. the second way:
Sorting with Apache Commons CompareToBuilder
Collections.sort(pizzas, new Comparator<Pizza>() { @Override public int compare(Pizza p1, Pizza p2) { return new CompareToBuilder().append(p1.size, p2.size).append(p1.nrOfToppings, p2.nrOfToppings).append(p1.name, p2.name).toComparison(); } });
相关推荐
Now revised to reflect the innovations of Java 5.0, Goodrich and Tamassia’s Fourth Edition of Data Structures and Algorithms in Java continues to offer accessible coverage of fundamental data ...
Now revised to reflect the innovations of Java 5.0, Goodrich and Tamassia’s Fourth Edition of Data Structures and Algorithms in Java continues to offer accessible coverage of fundamental data ...
* Focused, tutorial-based instruction in key sockets programming techniques allows reader to quickly come up to speed on Java applications. * Concise and up-to-date coverage of the most recent ...
and the JSSE for encryption and authentication The ways in which padding mechanisms work in ciphers and how to spot and fix typical errors An understanding of how authentication mechanisms are ...
If you're viewing this document online, you can click any of the topics below to link directly to that section. 1. Tutorial tips 2 2. Introducing the JavaMail API 3 3. Reviewing related ...
Now revised to reflect the innovations of Java 5.0, Goodrich and Tamassia’s Fourth Edition of Data Structures and Algorithms in Java continues to offer accessible coverage of fundamental data ...
The emphasis is not on teaching advanced concepts of the Java language, per se, but rather on using Java to teach advanced programming concepts that any aspiring programmer should know. The major ...
Lists I- How to use lists in Excel 2003.xlt
Lists I- How to use lists in Excel 2003(2).xlt
Based on the latest version of Java 9, this book illustrates the impact of new features on data structures Gain exposure to important concepts such as Big-O Notation and Dynamic Programming Book ...
access lists are built, and give examples of how to apply those access lists in different situations. Along the way, there are a number of sidebars and notes about concepts and information important...
After an overview of preliminary concepts, this text introduces stacks and queues using arrays along with a discussion of array-based lists. This is followed by an introduction to linked lists and the...
在"An AJAX image list control to manage lists of items displayed with icons"中,这个控件可能是一个前端组件,用于展示一系列带有图标的项目。这些图标可能是用来表示不同类型的项目,或者代表项目的状态。当...
and the use of assertions to validate the expected behavior of code. <br>Chapter 13Strings and Regular Expressionsdescribes the built-in language and runtime support for String objects, the ...
Now, Core Java®, Volume II—Advanced Features, Tenth Edition, has been extensively updated to reflect the most eagerly awaited and innovative version of Java in years: Java SE 8. Rewritten and ...