How can I duplicate an HashMap with duplicate values - java

I need to create new HashMaps that contain only the duplicate values of my first HashMap :
Original map: {Player1=Hello, Player2=Hi, Player3=Hi, Player4=Hello, Player5=Hello}
For the outputs, I want to get :
Hello map: {Player1=Hello,Player4=Hello, Player5=Hello}
Hi map: {Player2=Hi, Player3=Hi}
What is best way to do?

If you are using Java8+ you can use stream, with groupingBy and toMap like so :
Map<String, Map<String, String>> collect = map.entrySet().stream()
.collect(Collectors.groupingBy(Map.Entry::getValue,
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
For e simple map, your outputs can be :
Hi - {Player2=Hi, Player3=Hi}
Hello - {Player5=Hello, Player1=Hello, Player4=Hello}
Ideone demo

class Player {
private String name;
private int id;
public Player(int id) {
this.id = id;
this.name = "Player " + id;
}
#Override
public int hashCode() {
return this.id;
}
#Override
public String toString() {
return this.name;
}
}
Player[] players = new Player[5];
IntStream.range(0, players.length).forEach(i -> players[i] = new Player(i + 1));
// -------------------------------------------------------------------------
HashMap<Player, String> original = new HashMap<>();
original.put(players[0], "Hello");
original.put(players[1], "Hi");
original.put(players[2], "Hi");
original.put(players[3], "Hello");
original.put(players[4], "Hello");
HashMap<String, HashMap<Player, String>> duplicates = new HashMap<>();
original.keySet().stream().forEach(key -> {
String value = original.get(key);
HashMap<Player, String> duplicate = duplicates.get(value);
if (duplicate == null) {
duplicate = new HashMap<>();
duplicates.put(value, duplicate);
}
duplicate.put(key, value);
});
System.out.println("Original: " + original);
duplicates.forEach((key, value) -> {
System.out.println(key + ": " + value);
});
//
Original: {Player 1=Hello, Player 2=Hi, Player 3=Hi, Player 4=Hello, Player 5=Hello}
Hi: {Player 2=Hi, Player 3=Hi}
Hello: {Player 1=Hello, Player 4=Hello, Player 5=Hello}

Related

How to compare and sort nested Map in java based on Item price?

How to fix this issue/error ( How to sort nested TreeMap in java)
When i am trying to sort Map using item price it is not working because TreeMap sort in natural order according to key but i need to sort this by using inner map key. any one help me..
public class SortedTest {
private static Map<Integer, Map<Item, Integer>> tm = new TreeMap<>();
public static void main(String[] args) {
Item item = new Item("beer", 1, 5.0);
Item item1 = new Item("tofu", 2, 3.5);
Item item2 = new Item("ic", 3, 3.2);
Item item3 = new Item("mg", 4, 4.5);
tm.put(item.getId(), new TreeMap<>());
tm.get(item.getId()).put(item, 3);
System.out.println(tm);
tm.put(item1.getId(), new TreeMap<>());
tm.get(item1.getId()).put(item1, 3);
System.out.println(tm);
}
}
package trial;
public class Item implements Comparable<Item> {
private String name;
private Integer id;
private Double price;
// constructor, setter & getter
#Override
public int compareTo(Item o) {
if (getPrice() > o.getPrice()) {
return 1;
}
if (price < o.getPrice()) {
return -1;
}
return 0;
}
#Override
public String toString() {
return "Item{" +
"name='" + name + '\'' +
", id=" + id +
", price=" + price +
'}';
}
}
Actually you are trying to sort map by value, comparable method or comparator can do it with keys only.

How to return a HashMap from a list of objects?

Currently, I am trying to return a HashMap. With a parameter of an ArrayList that has 50 or more entries.
public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) {
HashMap<String, Doctor> hm = new HashMap<>();
for(Doctor doctor : doctorList) {
hm.put(doctor.getId(), doctor);
}
return hm;
}
I am passing the Id as the key and doctor object as the value..
My Doctor class is simple:
public class Doctor {
private String firstName, lastName, id;
public Doctor(String firstName, String lastName, String id) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
}
//getter and setters etc.
}
Not sure why you want to do it this way, (you could use a Java8 stream and filter the list for first names), but you are close.
public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) {
HashMap<String, Doctor> hm = new HashMap<>();
for(int i = 0; i < doctorList.size(); i++) {
hm.put(doctorList.get(i).getFirstName(), doctorList.get(i));
}
return hm;
}
Or, more simply
public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) {
HashMap<String, Doctor> hm = new HashMap<>();
for(Doctor d : doctorList) {
hm.put(d.getFirstName(), d);
}
return hm;
}
Then, you have to Doctor d = doctorMap.get("firstname") for some firstname
This is not a solution, but something that can be used for deriving one from it
Due to that you haven't stated what the output of the console was, I'm not able to know which specific errors you have.
Nevertheless, I created the following code for giving myself an idea:
public class StartingPoint {
static String[] doctorNames = {"Potato", "Chocolate", "Something", "Name", "Unnamed"};
public static void main(String[] args) {
ArrayList<Doctor> doctorList = new ArrayList<>(5);
for (int i = 0; i < 5; i++) {
doctorList.add(new Doctor(doctorNames[i], doctorNames[i] + " last name", String.valueOf(i)));
}
HashMap<String, Doctor> someHashMap = getDoctorHash(doctorList);
for (int i = 0; i < 5; i++) {
System.out.println("The ID of the doctor number " + String.valueOf(i + 1) + " is: ");
System.out.println(someHashMap.get(doctorNames[i]).getId());
}
}
public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) {
HashMap<String, Doctor> hm = new HashMap<>();
for(Doctor doctor : doctorList) {
System.out.println(doctor);
hm.put(doctor.getId(), doctor);
}
return hm;
}
}
It turns out that the compiler acts as if there weren't such a thing as a Doctor object being the value of an item's ID (that acts as a key). Nevertheless, it can be seen that when one tries to print out the location in memory of each one of the Doctor items of the ArrayList passed to the getDoctorHash() function in its definition, there are no problems at all.
I don't have the slightest idea about what the reason behind of this is.
But, if instead of using the Doctor objects as values we use one of the Strings that can be obtained by making use of one of its methods, everything turns out well:
public class StartingPoint {
static String[] doctorNames = {"Potato", "Chocolate", "Something", "Name", "Unnamed"};
public static void main(String[] args) {
ArrayList<Doctor> doctorList = new ArrayList<>(5);
for (int i = 0; i < 5; i++) {
doctorList.add(new Doctor(doctorNames[i], doctorNames[i] + " last name", String.valueOf(i)));
}
HashMap<String, String> someHashMap = getDoctorHash(doctorList);
for (int i = 0; i < 5; i++) {
System.out.println("The first name of the doctor number " + String.valueOf(i + 1) + " is: ");
System.out.println(someHashMap.get(String.valueOf(i)));
}
}
public static HashMap<String, String> getDoctorHash(ArrayList<Doctor> doctorList) {
HashMap<String, String> hm = new HashMap<>();
for(Doctor doctor : doctorList) {
hm.put(doctor.getId(), doctor.getFirstName());
}
return hm;
}
}

Best data structure to "group by" and aggregate values in Java?

I created an ArrayList of Array type like below,
ArrayList<Object[]> csvArray = new ArrayList<Object[]>();
As you can see, each element of the ArrayList is an array like {Country, City, Name, Age}.
Now I'm wanting to do a "group by" on Country and City (combined), followed by taking the average Age of the people for each Country+City.
May I know what is the easiest way to achieve this? Or you guys have suggestions to use data structures better than ArrayList for this "group by" and aggregation requirements?
Your answers are much appreciated.
You will get lot of options in Java 8.
Example
Stream<Person> people = Stream.of(new Person("Paul", 24), new Person("Mark",30), new Person("Will", 28));
Map<Integer, List<String>> peopleByAge = people
.collect(groupingBy(p -> p.age, mapping((Person p) -> p.name, toList())));
System.out.println(peopleByAge);
If you can use Java 8 and no specific reason for using a data structure, you can go through below tutorial
http://java.dzone.com/articles/java-8-group-collections
You could use Java 8 streams for this and Collectors.groupingBy. For example:
final List<Object[]> data = new ArrayList<>();
data.add(new Object[]{"NL", "Rotterdam", "Kees", 38});
data.add(new Object[]{"NL", "Rotterdam", "Peter", 54});
data.add(new Object[]{"NL", "Amsterdam", "Suzanne", 51});
data.add(new Object[]{"NL", "Rotterdam", "Tom", 17});
final Map<String, List<Object[]>> map = data.stream().collect(
Collectors.groupingBy(row -> row[0].toString() + ":" + row[1].toString()));
for (final Map.Entry<String, List<Object[]>> entry : map.entrySet()) {
final double average = entry.getValue().stream()
.mapToInt(row -> (int) row[3]).average().getAsDouble();
System.out.println("Average age for " + entry.getKey() + " is " + average);
}
You can check the collections recommended by #duffy356. I can give you an standard solution related with java.utils
I'd use a common Map<Key,Value> and being specific a HashMap.
For the keys, as I can see, you'll need and extra plain object which relates country and city. The point is create a working equals(Object) : boolean method. I'd use the Eclipse-auto generator; for me it gives me the following:
class CountryCityKey {
// package visibility
String country;
String city;
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((country == null) ? 0 : country.hashCode());
result = prime * result + ((region == null) ? 0 : region.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CountryCityKey other = (CountryCityKey) obj;
if (country == null) {
if (other.country != null)
return false;
} else if (!country.equals(other.country))
return false;
if (region == null) {
if (other.region != null)
return false;
} else if (!region.equals(other.region))
return false;
return true;
}
}
Now we can group or objects in a HashMap<CountryCityKey, MySuperObject>
The code for that could be:
Map<CountryCityKey, List<MySuperObject>> group(List<MySu0perObject> list) {
Map<CountryCityKey, MySuperObject> response = new HashMap<>(list.size());
for (MySuperObject o : list) {
CountryCityKey key = o.getKey(); // I consider this done, so simply
List<MySuperObject> l;
if (response.containsKey(key)) {
l = response.get(key);
} else {
l = new ArrayList<MySuperObject>();
}
l.add(o);
response.put(key, l);
}
return response;
}
And you have it :)
you could use the brownies-collections library of magicwerk.org (http://www.magicwerk.org/page-collections-overview.html)
they offer keylists, which fit your requirements.(http://www.magicwerk.org/page-collections-examples.html)
I would recommend an additional step. You gather your data from CSV in Object[]. If you wrap your data into a class containing these data java8 collections will easily help you. (also without but it is more readable and understandable)
Here is an example - it introduces a class Information which contains your given data (country, city,name, age). The class has a constructor initializing these fields by a given Object[] array which might help you to do so - BUT: the fields have to be fixed (which is usual for CSV):
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CSVExample {
public static void main(String[] args) {
ArrayList<Information> csvArray = new ArrayList<>();
csvArray.add(new Information(new Object[] {"France", "Paris", "Pierre", 34}));
csvArray.add(new Information(new Object[] {"France", "Paris", "Madeleine", 26}));
csvArray.add(new Information(new Object[] {"France", "Toulouse", "Sam", 34}));
csvArray.add(new Information(new Object[] {"Italy", "Rom", "Paul", 44}));
// combining country and city with whitespace delimiter to use it as the map key
Map<String, List<Information>> collect = csvArray.stream().collect(Collectors.groupingBy(s -> (s.getCountry() + " " + s.getCity())));
//for each key (country and city) print the key and the average age
collect.forEach((k, v) -> System.out.println(k + " " + v.stream().collect(Collectors.averagingInt(Information::getAge))));
}
}
class Information {
private String country;
private String city;
private String name;
private int age;
public Information(Object[] information) {
this.country = (String) information[0];
this.city = (String) information[1];
this.name = (String) information[2];
this.age = (Integer) information[3];
}
public Information(String country, String city, String name, int age) {
super();
this.country = country;
this.city = city;
this.name = name;
this.age = age;
}
public String getCountry() {
return country;
}
public String getCity() {
return city;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
#Override
public String toString() {
return "Information [country=" + country + ", city=" + city + ", name=" + name + ", age=" + age + "]";
}
}
The main shows a simple output for your question.
In java 8 the idea of grouping objects in a collection based on the values of one or more of their properties is simplified by using a Collector.
First, I suggest you add a new class as follow
class Info {
private String country;
private String city;
private String name;
private int age;
public Info(String country,String city,String name,int age){
this.country=country;
this.city=city;
this.name=name;
this.age=age;
}
public String toString() {
return "("+country+","+city+","+name+","+age+")";
}
// getters and setters
}
Setting up infos
ArrayList<Info> infos =new ArrayList();
infos.add(new Info("USA", "Florida", "John", 26));
infos.add(new Info("USA", "Florida", "James", 18));
infos.add(new Info("USA", "California", "Alan", 30));
Group by Country+City:
Map<String, Map<String, List<Info>>>
groupByCountryAndCity = infos.
stream().
collect(
Collectors.
groupingBy(
Info::getCountry,
Collectors.
groupingBy(
Info::getCity
)
)
);
System.out.println(groupByCountryAndCity.get("USA").get("California"));
Output
[(USA,California,James,18), (USA,California,Alan,30)]
The average Age of the people for each Country+City:
Map<String, Map<String, Double>>
averageAgeByCountryAndCity = infos.
stream().
collect(
Collectors.
groupingBy(
Info::getCountry,
Collectors.
groupingBy(
Info::getCity,
Collectors.averagingDouble(Info::getAge)
)
)
);
System.out.println(averageAgeByCountryAndCity.get("USA").get("Florida"));
Output:
22.0
/* category , list of cars*/
Please use the below code : I have pasted it from my sample app !Happy Coding .
Map<String, List<JmCarDistance>> map = new HashMap<String, List<JmCarDistance>>();
for (JmCarDistance jmCarDistance : carDistanceArrayList) {
String key = jmCarDistance.cartype;
if(map.containsKey(key)){
List<JmCarDistance> list = map.get(key);
list.add(jmCarDistance);
}else{
List<JmCarDistance> list = new ArrayList<JmCarDistance>();
list.add(jmCarDistance);
map.put(key, list);
}
}
Best data structure is a Map<Tuple, List>.
Tuple is the key, i.e. your group by columns.
List is used to store the row data.
Once you have your data in this structure, you can iterate through each key, and perform the aggregation on the subset of data.

Sort List by Map, using key and after value

I need to sort List by Map, using key of Map. Firstly look at code, afterwards listen to me. I would like to sort List by Key, and after by Value. The result after all should be the following(return only value in List):
/* The result(List):
str3
str1
str2
str4 */
--
List<String> list = ArrayList<>();
list.add("str1");
list.add("str1");
list.add("str3");
list.add("str4"); .......
Map<String, Integer> counts = new HashMap<>();
for (String item:list) {
Integer count = counts.get(item);
if (count == null) {
count = 1;
} else {
count = count + 1;
}
counts.put(item, count);
}
for (Entry<String, Integer> entry : counts.entrySet()) {
System.out.println(entry.getValue() + " " + entry.getKey());
}
--
/* The result:
2 str1
3 str2
1 str3
3 str4 */
Make a custom comparator:
Collections.sort(list, new Comparator<String>() {
#Override
public int compare(String left, String right) {
return Integer.compare(counts.get(left), counts.get(right));
}
});
Note that you need to make counts final for this to work.
Running this on your example:
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("str1");
list.add("str2");
list.add("str3");
list.add("str4");
final Map<String, Integer> counts = new HashMap<>();
counts.put("str1", 2);
counts.put("str2", 3);
counts.put("str3", 1);
counts.put("str4", 3);
Collections.sort(list, new Comparator<String>() {
#Override
public int compare(String left, String right) {
return Integer.compare(counts.get(left), counts.get(right));
}
});
System.out.println(list);
}
}
Yields:
[str3, str1, str2, str4]
When you want to read the Mapentries sorted by key, you could use a Treemap.
Buy maybe you can rephrase the question, it's not very clear what the result should be.

Sorting a HashMap, while keeping duplicates

I'm trying to sort a HashMap in two ways. The default way: alphabetically by the value, the second way: numerically by the key, with the higher number being at the top. I have searched around but can't find anything on the subject, and what I do find, doesn't work. If it's not possible to sort both of them (I want the person with the highest key at the top, decreasing as people have lower keys, then alphabetically sort all of the rest (the people with 0 as their key).
Here's what I've tried so far:
private HashMap<String, Integer> userGains = new HashMap<String, Integer>();
public void sortGains(int skill, int user) {
userGains.put(users.get(user).getUsername(), users.get(user).getGainedExperience(skill));
HashMap<String, Integer> map = sortHashMap(userGains);
for (int i = 0; i < map.size(); i++) {
Application.getTrackerOutput().getOutputArea(skill).append(users.get(user).getUsername() + " gained " + map.get(users.get(user).getUsername()) + " experience in " + getSkillName(skill) + ".\n");
}
}
public LinkedHashMap<String, Integer> sortHashMap(HashMap<String, Integer> passedMap) {
List<String> mapKeys = new ArrayList<String>(passedMap.keySet());
List<Integer> mapValues = new ArrayList<Integer>(passedMap.values());
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
Collections.sort(mapValues);
Collections.sort(mapKeys);
Iterator<Integer> it$ = mapValues.iterator();
while (it$.hasNext()) {
Object val = it$.next();
Iterator<String> keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();
if (comp1.equals(comp2)) {
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String) key, (Integer) val);
break;
}
}
}
return sortedMap;
}
Since you cannot run that here is an SSCCE:
private HashMap<String, Integer> userGains = new HashMap<String, Integer>();
private Object[][] testUsers = { { "Test user", 15 }, { "Test", 25 }, { "Hello", 11 }, { "I'm a user", 21 }, { "No you're not!", 14 }, { "Yes I am!", 45 }, { "Oh, okay. Sorry about the confusion.", 0 }, { "It's quite alright.", 0 } };
public static void main(String[] arguments) {
new Sorting().sortGains();
}
public void sortGains() {
for (Object[] test : testUsers) {
userGains.put((String) test[0], (Integer) test[1]);
}
HashMap<String, Integer> map = sortHashMap(userGains);
for (int i = 0; i < map.size(); i++) {
System.out.println(testUsers[i][0] + " gained " + map.get(testUsers[i][0]) + " experience.");
}
}
public LinkedHashMap<String, Integer> sortHashMap(HashMap<String, Integer> passedMap) {
List<String> mapKeys = new ArrayList<String>(passedMap.keySet());
List<Integer> mapValues = new ArrayList<Integer>(passedMap.values());
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
Collections.sort(mapValues);
Collections.sort(mapKeys);
Iterator<Integer> it$ = mapValues.iterator();
while (it$.hasNext()) {
Object val = it$.next();
Iterator<String> keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();
if (comp1.equals(comp2)) {
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String) key, (Integer) val);
break;
}
}
}
return sortedMap;
}
The output of the program is currently:
Test user gained 15 experience.
Test gained 25 experience.
Hello gained 11 experience.
I'm a user gained 21 experience.
No you're not! gained 14 experience.
Yes I am! gained 45 experience.
Oh, okay. Sorry about the confusion. gained 0 experience.
It's quite alright. gained 0 experience.
When I need it to be:
Yes I am! gained 45 experience. // start numeric sorting here, by highest key.
Test gained 25 experience.
I'm a user gained 21 experience.
Test user gained 15 experience.
No you're not! gained 14 experience.
Hello gained 11 experience.
It's quite alright. gained 0 experience. // start alphabetical sorting here, if possible.
Oh, okay. Sorry about the confusion. gained 0 experience.
Any insight?
It's not possible to sort a HashMap at all. By definition, the keys in a HashMap are unordered. If you want the keys of your Map to be ordered, then use a TreeMap with an appropriate Comparator object. You can create multiple TreeMaps with different Comparators if you want to access the same data multiple ways.
You made a mistake in displaying the values.
HashMap<String, Integer> map = sortHashMap(userGains);
for (int i = 0; i < map.size(); i++) {
System.out.println(testUsers[i][0] + " gained " + map.get(testUsers[i][0]) + " experience.");
}
You need to display the map's values instead of the original array's values.
This should do:
HashMap<String, Integer> map = sortHashMap(userGains);
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " gained " + entry.getValue() + " experience.");
}
You only have to reverse the order. Further I recommend to declare against Map instead of HashMap or LinkedHashMap to avoid confusion by yourself and others. Also your sorting can simpler be done with a Comparable. Here's an improvement:
private Map<String, Integer> userGains = new HashMap<String, Integer>();
private Object[][] testUsers = { { "Test user", 15 }, { "Test", 25 }, { "Hello", 11 }, { "I'm a user", 21 }, { "No you're not!", 14 }, { "Yes I am!", 45 }, { "Oh, okay. Sorry about the confusion.", 0 }, { "It's quite alright.", 0 } };
public static void main(String[] arguments) {
new Sorting().sortGains();
}
public void sortGains() {
for (Object[] test : testUsers) {
userGains.put((String) test[0], (Integer) test[1]);
}
Map<String, Integer> map = createSortedMap(userGains);
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " gained " + entry.getValue() + " experience.");
}
}
public Map<String, Integer> createSortedMap(Map<String, Integer> passedMap) {
List<Entry<String, Integer>> entryList = new ArrayList<Entry<String, Integer>>(passedMap.entrySet());
Collections.sort(entryList, new Comparator<Entry<String, Integer>>() {
#Override
public int compare(Entry<String, Integer> e1, Entry<String, Integer> e2) {
if (!e1.getValue().equals(e2.getValue())) {
return e1.getValue().compareTo(e2.getValue()) * -1; // The * -1 reverses the order.
} else {
return e1.getKey().compareTo(e2.getKey());
}
}
});
Map<String, Integer> orderedMap = new LinkedHashMap<String, Integer>();
for (Entry<String, Integer> entry : entryList) {
orderedMap.put(entry.getKey(), entry.getValue());
}
return orderedMap;
}
This question approaches what you're trying to do, by sorting on value in a TreeMap. If you take the most voted answer and modify the Comparator to sort on value then key, it should give you what you want.
Effectively, you create a Comparator that has a field that points to the TreeMap (so it can look up values). And the TreeMap uses this Comparator. When items are added to the TreeMap, the Comparator looks up the values and does a comparison on
if the value a < value b, return 1
if the value a > value b, return -1
if the key a < key b, return 1
if the key a > key b, return -1
otherwise, return 0
Copying a lot of code from that answer (with no checking to see if the code works, since it's just for the idea):
public class Main {
public static void main(String[] args) {
ValueComparator<String> bvc = new ValueComparator<String>();
TreeMap<String,Integer> sorted_map = new TreeMap<String,Integer>(bvc);
bvc.setBase(sorted_map);
// add items
// ....
System.out.println("results");
for (String key : sorted_map.keySet()) {
System.out.println("key/value: " + key + "/"+sorted_map.get(key));
}
}
}
class ValueComparator implements Comparator<String> {
Map base;
public setBase(Map<String,Integer> base) {
this.base = base;
}
public int compare(String a, String b) {
Integer value_a = base.get(a);
Integer value_b = base.get(b);
if(value_a < value_b) {
return 1;
}
if(value_a>< value_b) {
return -1;
}
return a.compareTo(b);
}
}

Categories