java中如何将列表转换为集合
时间:2020-02-23 14:35:03 来源:igfitidea点击:
在本教程中,我们将看到如何在java中将列表转换为集合.我们将转换为hashset。
由于HashSet不允许重复,当您将ArrayList转换为HashSet时,所有重复项都将被丢弃。
我们可以简单地使用HashSet的构造函数将ArrayList转换为HashSet。
HashSet set=new HashSet(list);
这是一个简单的例子:
package org.igi.theitroad; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class ListToSetMain { public static void main(String args[]) { List<String> listofCountries=new ArrayList<>(); listofCountries.add("Netherlands"); listofCountries.add("China"); listofCountries.add("Bhutan"); listofCountries.add("Nepal"); listofCountries.add("Netherlands"); System.out.println("======================="); System.out.println("List of Countries: "); System.out.println("======================="); for(String country:listofCountries) { System.out.println(country); } //Converting list to set Set<String> countriesSet=new HashSet<String>(listofCountries); System.out.println("======================="); System.out.println("Set of Countries: "); System.out.println("======================="); for(String country:countriesSet) { System.out.println(country); } } }
运行上面的程序时,我们将得到以下输出
======================= List of Countries: ======================= Netherlands China Bhutan Nepal Netherlands ======================= Set of Countries: ======================= Bhutan China Nepal Netherlands
Java 8符合示例列表
我们可以使用Java 8 Stream API将列表转换为设置。
//Converting list to set in java 8 Set<String> countriesSet=listofCountries.stream().collect(Collectors.toCollection(HashSet::new));
只需更改ListToSetMain.java的第29行,我们将获得相同的输出。
列出以在自定义对象的情况下设置
当我们转换要设置的自定义对象列表时,我们需要非常小心。
让我们在简单的例子的帮助下了解:
创建一个名为"country"的类。
我们将把此类的对象放在列表中,然后将其转换为列表。
package org.igi.theitroad; public class Country { String name; long population; public Country(String name, long population) { super(); this.name = name; this.population = population; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } @Override public String toString() { return "Country [name=" + name + ", population=" + population + "]"; } }
创建一个名为listtosetcustomobjectmain的类:
package org.igi.theitroad; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class ListToSetCustomObjectMain { public static void main(String args[]) { List<Country> listofCountries=new ArrayList<>(); listofCountries.add(new Country("Netherlands",20000)); listofCountries.add(new Country("China",30000)); listofCountries.add(new Country("Bhutan",1000)); listofCountries.add(new Country("Nepal",3000)); listofCountries.add(new Country("Netherlands",20000)); System.out.println("======================="); System.out.println("List of Countries: "); System.out.println("======================="); for(Country country:listofCountries) { System.out.println(country); } //Converting list to set Set<Country> countriesSet=new HashSet<Country>(listofCountries); System.out.println("======================="); System.out.println("Set of Countries: "); System.out.println("======================="); for(Country country:countriesSet) { System.out.println(country); } } }
运行上面的程序时,我们将得到以下输出:
======================= List of Countries: ======================= Country [name=Netherlands, population=20000] Country [name=China, population=30000] Country [name=Bhutan, population=1000] Country [name=Nepal, population=3000] Country [name=Netherlands, population=20000] ======================= Set of Countries: ======================= Country [name=Netherlands, population=20000] Country [name=Nepal, population=3000] Country [name=Netherlands, population=20000] Country [name=Bhutan, population=1000] Country [name=China, population=30000]
如我们所见,我们在输出中有国家[Name = Netherlands,人口= 20000],但这些是重复的条目,但HashSet不会将其视为重复。
你知道为什么吗?
因为我们没有实施Hashcode并等于Methodin国家程序。
让我们将以下方法添加到国家程序。
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + (int) (population ^ (population >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Country other = (Country) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (population != other.population) return false; return true; }
添加以下两种方法后再次运行上面的程序时。
我们将得到以下输出:
======================= List of Countries: ======================= Country [name=Netherlands, population=20000] Country [name=China, population=30000] Country [name=Bhutan, population=1000] Country [name=Nepal, population=3000] Country [name=Netherlands, population=20000] ======================= Set of Countries: ======================= Country [name=Netherlands, population=20000] Country [name=Nepal, population=3000] Country [name=China, population=30000] Country [name=Bhutan, population=1000]