[英]Separate a field from list and make a new list
I have this :我有这个 :
List<City> cities = new ArrayList<>();
cities.add(new City("SF","USA"));
cities.add(new City("Agra","India"));
cities.add(new City("Mumbai","India"));
cities.add(new City("NY","USA"));
Required Output:所需输出:
List<"Some thing"> combined = new ArrayList();
combined is like this :
USA
SF
NY
India
Mumbai
Agra
I used this approach :我使用了这种方法:
public class ListBreak {
public static void main(String[] args) {
List<City> cities = new ArrayList<>();
cities.add(new City("SF","USA"));
cities.add(new City("Agra","India"));
cities.add(new City("Mumbai","India"));
cities.add(new City("NY","USA"));
List<ABC> abcs = new ArrayList<>();
HashMap<Country,List<City>> countryListHashMap= new HashMap<>();
// way one
for (City city:cities) {
Country coutry= new Country(city.countryName);
if(!countryListHashMap.containsKey(coutry))
{
countryListHashMap.put(coutry,new ArrayList<>());
}
List<City> list = countryListHashMap.get(coutry);
list.add(city);
countryListHashMap.put(coutry,list);
}
for (Map.Entry<Country,List<City>> entrySet:countryListHashMap.entrySet()) {
abcs.add(entrySet.getKey());
abcs.addAll(entrySet.getValue());
}
System.out.println(abcs);
}
static class City implements ABC{
String cityName;
String countryName;
public City(String cityName, String countryName) {
this.cityName = cityName;
this.countryName = countryName;
}
}
static class Country implements ABC{
String countryName;
public Country(String countryName) {
this.countryName = countryName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Country)) return false;
Country country = (Country) o;
return countryName.equals(country.countryName);
}
@Override
public int hashCode() {
return Objects.hash(countryName);
}
}
} }
Any better, cleaner, faster way任何更好、更清洁、更快的方式
package clean.code;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class ListBreak {
public static class City {
private String cityName;
private String countryName;
public City(String cityName, String countryName) {
this.setCityName(cityName);
this.setCountryName(countryName);
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
public static void main(String[] args) {
List<City> cities = new ArrayList<>();
cities.add(new City("SF", "USA"));
cities.add(new City("Agra", "India"));
cities.add(new City("Mumbai", "India"));
cities.add(new City("NY", "USA"));
cities.stream()
.collect(Collectors.groupingBy(City::getCountryName, collectCityNames()))
.forEach((countryName, cityNames) -> printCityNamesByCountryName(countryName, cityNames));
}
private static Collector<City, ?, List<String>> collectCityNames() {
return Collectors.mapping(City::getCityName, Collectors.toList());
}
private static void printCityNamesByCountryName(String countryName, List<String> cityNames) {
System.out.println(countryName);
cityNames.forEach(System.out::println);
}
}
class City {
String cityName;
String countryName;
//Getter,Setter and Constructors
}
public class CandidateCode {
public static void main(String[] args) {
List<City> cities = new ArrayList<>();
//stuffs
final Map<String, List<String>> collect1 = cities.stream()
.collect(Collectors.groupingBy(City::getCountryName, Collectors.mapping(City::getCityName, Collectors.toList())));
for (Map.Entry str : collect1.entrySet()) {
System.out.println(str.getKey());
for (String s : collect1.get(str.getKey())) {
System.out.println(s);
}
}
}
}
Output :输出:
USA
SF
NY
India
Agra
Mumbai
class City{
String cityName;
String countryName;
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public City(String string, String string2) {
this.cityName = string;
this.countryName=string2;
}
//getter setter ns constructor stuffs
}
public class Test {
public static void main(String[] args) {
List<City> cities = new ArrayList<>();
cities.add(new City("SF","USA"));
cities.add(new City("Agra","India"));
cities.add(new City("Mumbai","India"));
cities.add(new City("NY","USA"));
List<String> result = new ArrayList<>();
Map<String, List<City>> res = cities.stream().collect(Collectors.groupingBy(City::getCountryName));
for(HashMap.Entry<String, List<City>> valuepair:res.entrySet())
{
List<City> value = valuepair.getValue();
City countryName = value.get(0);
String cname = countryName.getCountryName();
result.add(cname);
for (City count : value) {
result.add(count.getCityName());
}
}
for (String string : result) {
System.out.println(string);
}
}
}
Output输出
USA
SF
NY
India
Agra
Mumbai
Please try this.请试试这个。
Map<String, List<String>> countryListHashMap =
cities.stream().collect(Collectors.groupingBy(City::getCountryName, Collectors.mapping(City::getCityName, Collectors.toList())));
countryListHashMap.forEach((k,v)-> {System.out.println(k);
v.forEach(System.out::println);});
Output:输出:
USA
SF
NY
India
Agra
Mumbai
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.