第十章 集合2
1.Set
public static void main(String[] args) {
Set<String> c =new HashSet<String>();
c.add("hello");
c.add("world");
c.add("java") ;
for(String s:c){
System.out.println(s);
}
}
结果:
world
java
hello
Process finished with exit code 0
2.HashSet
案例:
public class Test{
public static void main(String[] args) throws ParseException {
HashSet<Student> c =new HashSet<Student>();
Student h1=new Student("Lily",18);
Student h2=new Student("Bob",19);
Student h3=new Student("Jack",20);
c.add(h1);
c.add(h2);
c.add(h3);
for(Student s:c){
System.out.println(s.getName()+"----"+s.getAge());
}
Iterator<Student> it =c.iterator();
while(it.hasNext()){
Student s= it.next();
System.out.println(s.getName()+"------"+s.getAge());
}
}
}
3.Map集合
成员方法及获取功能:
public class Test{
public static void main(String[] args) throws ParseException {
Map<String,String> c =new HashMap<String,String>();
c.put("001","lily");
c.put("002","bob");
c.put("003","jack");//添加元素
System.out.println(c);
Set<String> set= c.keySet();//获取所有键的集合
for(String key:set){
System.out.println(key);
}
Collection<String> s=c.values();//获取所有值的集合
for(String value:s){
System.out.println(value);
}
c.remove("001");//删除元素
System.out.println(c);
System.out.println(c.get("002"));//根据键获取值
System.out.println(c.containsKey("001"));//是否包含某键,这里输出false
System.out.println(c.containsValue("lily"));//是否包含某值,这里输出false
System.out.println(c.size());//输出键值对个数
c.clear();//清空
System.out.println(c.isEmpty());//判空
}
}
遍历方法:(1)根据键找值
public static void main(String[] args) throws ParseException {
Map<String,String> c = new HashMap<>();
c.put("001","lily");
c.put("002","bob");
c.put("003","jack");//添加元素
System.out.println(c);
Set<String> set= c.keySet();//获取所有键的集合
for(String key:set){
String value=c.get(key);
System.out.println(key+"---"+value);
}
}
Set<Map.Entry<String,String>> set= c.entrySet();//获取所有键值对象的集合
for(Map.Entry<String,String> me:set){
String key= me.getKey();
String value= me.getValue();;
System.out.println(key+"---"+value);
}
4.HashMap
public class Test{
public static void main(String[] args) throws ParseException {
HashMap<String,Student> c = new HashMap<String, Student>();
Student h1=new Student("lily",18);
Student h2=new Student("bob",19);
Student h3=new Student("jack",20);
c.put("001",h1);
c.put("002",h2);
c.put("003",h3);//添加元素
//System.out.println(c);
Set<String> set1= c.keySet();//获取所有键的集合
for(String key:set1){
Student value= c.get(key);;
System.out.println(key+"---"+value.getName()+"---"+value.getAge());
}
Set<Map.Entry<String,Student>> set= c.entrySet();//获取所有键值对象的集合
for(Map.Entry<String,Student> me:set){
String key= me.getKey();
Student value= me.getValue();;
System.out.println(key+"---"+value.getName()+"-----"+value.getAge());
}
}
}
public class Test{
public static void main(String[] args) throws ParseException {
HashMap<Student,String> c = new HashMap<Student, String>();
Student h1=new Student("lily",18);
Student h2=new Student("bob",19);
Student h3=new Student("jack",20);
Student h4=new Student("jack",20);
c.put(h1,西安);
c.put(h2,上海);
c.put(h3,北京);
c.put(h4,香港);//添加元素
//System.out.println(c);
Set<Student> set1= c.keySet();//获取所有键的集合
for(Student key:set1){
String value= c.get(key);;
System.out.println(key.getName()+"---"+key.getAge()+"-----"+value);
}
}
}
5.集合的嵌套练习
public class Test{
public static void main(String[] args) throws ParseException {
ArrayList<HashMap<String,String>> c = new ArrayList<HashMap<String,String>>();
HashMap<String,String> h1=new HashMap<String,String>();
h1.put("lily","18");
h1.put("betty","19");
c.add(h1);
HashMap<String,String> h2=new HashMap<String,String>();
h2.put("jack","20");
h2.put("jason","19");
c.add(h2);
HashMap<String,String> h3=new HashMap<String,String>();
h3.put("remi","20");
h3.put("lucy","19");
c.add(h3);
for(HashMap<String,String> hm:c){
Set<String> set= hm.keySet();
for(String key:set){
String value =hm.get(key);
System.out.println(key+"-----"+value);
}
System.out.println("----------------------------");
}
}
}
public class Test{
public static void main(String[] args) throws ParseException {
HashMap<String,ArrayList<String>> c = new HashMap<String,ArrayList<String>>();
ArrayList<String> h1=new ArrayList<String>();
h1.add("lily");
h1.add("betty");
c.put("001",h1);
ArrayList<String> h2=new ArrayList<String>();
h2.add("jack");
h2.add("jason");
c.put("002",h2);
ArrayList<String> h3=new ArrayList<String>();
h3.add("remi");
h3.add("lucy");
c.put("003",h3);
Set<String> set1= c.keySet();//获取所有键的集合
for(String key:set1){
System.out.println(key);
ArrayList<String> value= c.get(key);
for(String s:value){
System.out.println(s);
}
System.out.println("------------------");
}
}
}