Set_HashSet 接口类的方法(day2)

一、Set_HashSet 接口类的方法,add,contains,clear,containsAll,remove,removaAll.分别用代码加注释说明其用法
1.封装标题类
package Set_HashSet;
public class NewsTitle {
private int id;
private String title;
private String author;
public NewsTitle(){}
public NewsTitle(int id, String title) {
this.id = id;
this.title = title;
this.author = author;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2.使用Set_HashSet接口类相关方法
package Set_HashSet;
import java.util.*;
public class NewsMgr {
public static void main(String[] args) {
//创建标题对象
NewsTitle title1 = new NewsTitle(1,“安徽黄山”);
NewsTitle title2 = new NewsTitle(2,“安徽九华山”);
NewsTitle title3 = new NewsTitle(3,“安徽天柱山”);
NewsTitle title4 = new NewsTitle(3,“安徽浮山”);
//创建集合对象,并且将标题加入到集合中
Set set = new HashSet();
//Set–Collection(add(Object))
set.add(title1);
set.add(title2);
set.add(title3);
set.add(title4);
set.remove(title1);
System.out.println(set.contains(title1));
set.clear();
//获取标题的总数
System.out.println(“标题一共有”+set.size()+“条”);
二、自己写一个实体类:信息部人员表,列:姓名、年龄、入职日期。然后装载到 ArrayList容器中,最后增强for循环输出姓名.
1.封装人员类
package Pesonnel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Person {
private String name;
private int age;
private Date hireDate;

public Person(String name, int age, String hireDate) {
    this.name = name;
    this.age = age;
    DateFormat format = new SimpleDateFormat("yyyy-MM");
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public Date getHireDate() {
    return hireDate;
}
public void setHireDate(Date hireDate) {
    this.hireDate = hireDate;
}

}
2.装载到 ArrayList容器中,用增强for循环输出姓名
package Pesonnel;
import java.util.ArrayList;
public class TestPerson {
public static void main(String[] args) {
Person person1 = new Person(“WC”, 36, “2018-07”);
Person person2 = new Person(“MYC”, 35, “2008-04”);
ArrayList list = new ArrayList();
list.add(person1);
list.add(person2);
System.out.println(“人员信息共有:” + list.size() + “条”);
System.out.println();
for(Object obj : list){
Person title = (Person) obj;
System.out.println(title.getName());
}
}
}
三、用List封装几组数据,然后输出Map的值
package Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class CountriesInfo {
public static void main(String[] args) {
//创建集合对象,并将国家信息键值对放入集合
Map<String, String> countries = new HashMap<String, String>();
countries.put(“China”, “中国”);
countries.put(“USA”, “美国”);
countries.put(“Japan”, “日本”);
countries.put(“France”, “法国”);
//获取集合中存储元素个数(键值对对数)
System.out.println(countries.size());
//获取集合中特定的key对应的value–有了泛型,数据类型不需要强制转换
String country = countries.get(“China”);
System.out.println(country);
四、用迭代器(Iterator)方式循环输出几组Set
//迭代器Iterator遍历key的集合(Set)
Set keys = countries.keySet();
Iterator itor = keys.iterator();
while (itor.hasNext()) {
String key = (String) itor.next(); //获取到了Map中的每一个key
String value = (String) countries.get(key); //根据Map中的每个key去获取对应的value
System.out.println(key + “–” + value);
五、用两种方式分别遍历一组Map
获取Map中的所有键值对,然后在键值对中分别获取key和value
方法一:
Set<Map.Entry<String, String>> set = countries.entrySet(); //获取Map中的键值对
for(Object obj : set){
Map.Entry me = (Map.Entry)obj;
String key = (String)me.getKey(); //获取出来键值对中的键
String value = (String)me.getValue(); //获取出来键值对中的值
System.out.println(key+"–"+value);
}
方法二:
Set<Map.Entry<String, String>> set = countries.entrySet(); //获取Map中的键值对
//遍历键值对的集合,把每个键值对(obj)–Map.Entry(键值对的类型)拿出来
for (Map.Entry<String, String> me : set) {
String key = me.getKey(); //获取出来键值对中的键
String value = me.getValue(); //获取出来键值对中的值
System.out.println(key + “–” + value);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值