Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Comparing;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- /**
- * Created by Yura on 20.02.2017.
- */
- public class Names {
- private List<String > names;
- public Names(List<String> names) {
- this.names = names;
- }
- public void addName(String name) {
- names.add(name);
- }
- @Override
- public String toString() {
- return "Names: " + names + "";
- }
- public void namesInAlphabeticalOrder (){
- Collections.sort(names);
- }
- public void namesReversOrder(){
- Collections.sort(names, Collections.reverseOrder());
- }
- public String findMinName() {
- return Collections.min(names);
- }
- public void numberOfLetters() {
- Collections.sort(names, new LengthComparator());
- }
- public void numberOfLettersReversOrder() {
- Collections.sort(names, new numberOfLettersReversOrder());
- }
- private class LengthComparator implements Comparator<String>{
- @Override
- public int compare(String o1, String o2) {
- return Integer.compare(o1.length(), o2.length());
- }
- }
- private class numberOfLettersReversOrder implements Comparator<String>{
- @Override
- public int compare(String o1, String o2) {
- int lengthResult = Integer.compare(o1.length(), o2.length());
- if (lengthResult != 0){
- return o1.compareTo(o2);
- }
- return lengthResult;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement