I have two ArrayList
s. One is of the Double
type and contains distances. The other one is of the String
type and contains city types.
These are the lists:
private ArrayList<Double> distance_list = new ArrayList<Double>();
distance.add(123.43);
distance.add(450.43);
distance.add(230.65);
private ArrayList<String> city_list = new ArrayList<String>();
city_list.add("Munich");
city_list.add("Berlin");
city_list.add("Frankfurt");
Collections.sort(distance_list); //to get the closest distance first
How can I sort the city_list
in the same order to the matching distances?
NavigableMap
/ SortedMap
Use a Map
instead. A map pairs two items together, in a key-value relationship. Ask for the key to access its value partner.
Using a NavigableMap
or SortedMap
keeps, the keys in order.
NavigableMap< Double , String > map = new TreeMap<>() ;
map.add( 123.43d , "Munich" ) ;
…
You can then loop that map. See: How to Loop in NavigableMap in Java ;
for( Double key : map.keySet() )
{
String city = map.get( key ) ;
System.out.println( city + " = " + key ) ;
}
Here is a diagram graphic I made as an overview of the Map
implementations bundled with Java 11. You can see SortedMap
interface was added first, then later extended by NavigableMap
interface.
You'll want to create a class that pairs both values and implements the Comparable interface, such as:
public class City implements Comparable<City> {
private Double distance;
private String name;
// constructor, getters, setters
@Override
public int compareTo(City city) {
return (double)(this.distance - city.getDistance());
}
}
Then build a Treemap or some other Map type that keeps things sorted. I assume you wish to sort by distance.
//Create TreeMap
TreeMap<Double, String> cities = new TreeMap();
//Add cities
cities.put(123.43, "Munich");
The items will be sorted by the distances values!
Try it out and post your code if you need more help.
You can do it in 2 steps
class DistanceCity implements Comparable<DistanceCity> {
private Double distance;
private String city;
public DistanceCity(Double distance, String city) {
this.distance = distance;
this.city = city;
}
@Override
public int compareTo(DistanceCity o) {
return distance.compareTo(o.distance);
}
@Override
public String toString() {
return "DistanceCity{" + "distance=" + distance + ", city='" + city + "'}";
}
}
private ArrayList<DistanceCity> liste;
liste = new ArrayList<>();
liste.add(new DistanceCity(123.43,"Munich"));
liste.add(new DistanceCity(450.43,"Berlin"));
liste.add(new DistanceCity(230.65,"Frankfurt"));
System.out.println(liste); // unordered
//[DistanceCity{distance=123.43, city='Munich'}, DistanceCity{distance=450.43, city='Berlin'}, DistanceCity{distance=230.65, city='Frankfurt'}]
Collections.sort(liste);
System.out.println(liste); // ordered
// [DistanceCity{distance=123.43, city='Munich'}, DistanceCity{distance=230.65, city='Frankfurt'}, DistanceCity{distance=450.43, city='Berlin'}]
class City implements Comparable<City>{
public String name = null;
public Double distance = 0.0;
public City(String name, Double distance){
this.name = name;
this.distance = distance;
}
@Override
public int compareTo(City o){
return this.distance.compareTo( o.distance );
}
@Override
public String toString(){
return this.name;
}
}
public class CitiesDemo{
public static void main (String[] args ){
City munich = new City("Munich", 123.43);
City berlin = new City("Berlin", 450.43);
City frankfurt = new City("Frankfurt", 230.65);
java.util.List<City> cities = new java.util.ArrayList<City>();
cities.add( munich );
cities.add( berlin );
cities.add( frankfurt );
java.util.Collections.sort( cities );
System.out.println( cities );
}
}
Will return:
~/ (main)$ javac CitiesDemo.java
~/ (main)$ java CitiesDemo
[Munich, Frankfurt, Berlin]
~/ (main)$
Or, you can test it here: https://ideone.com/fbbLv6
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.