java集合练习题


习题1、

每个学生都有对应的归属地,学生Student,地址String类型

学生属性:姓名和年龄

注:姓名和年龄相同的是为同一个学生,保证学生的唯一性

import java.util.*;
class MapDemo 
{
	public static void main(String[] args) 
	{
		HashMap<Student,String> hm=new HashMap<Student,String>();//创建HashMap容器来存储信息
		hm.put(new Student("zjd",12),"山东");//添加信息
		hm.put(new Student("zxm",12),"山东");
		hm.put(new Student("zjn",12),"山东");
		hm.put(new Student("dxm",12),"山东");
		hm.put(new Student("wyj",12),"山东");
		hm.put(new Student("zxz",12),"山东");
		hm.put(new Student("zjj",12),"山东");
		hm.put(new Student("zjd",12),"山东");
		hm.put(new Student("zjn",12),"山东");
		hm.put(new Student("dxm",12),"山东");
		hm.put(new Student("wyj",12),"山东");
		Set<Map.Entry<Student,String>> s=hm.entrySet();//使用entrySet来实现HashMap转化为Set集合
		Iterator<Map.Entry<Student,String>> it=s.iterator();
		while(it.hasNext()){
			Map.Entry<Student,String> me=it.next();
			Student stu=me.getKey();
			String address=me.getValue();
			sop(stu.getName()+":"+stu.getAge()+":"+address+"entrySet");
	      }

        Set<Student> s1=hm.keySet();//使用keySet来实现HashMap转化为Set集合
		Iterator<Student> it1=s1.iterator();
		while(it1.hasNext()){
			Student stu1=it1.next();
			sop(stu1.getName()+":"+stu1.getAge()+":"+hm.get(stu1)+"keySet");
		}

	}
	public static void sop(Object obj)
     {
		System.out.println(obj);
	}
}
/*
创建学生类Student
姓名:name
年龄:age
*/
class Student{
	private String name;
	private int age;
	Student(String name,int age){//创建构造函数实现初始化
		this.name=name;
		this.age=age;
	}
	public void setStudent(String name,int age){//创建设置学生信息方法
		this.name=name;
		this.age=age;
	}
	public String getName(){//获取学生姓名
		return this.name;
	}
	public int getAge(){//获取学生年龄
		return this.age;
	}
	public int hashCode(){//覆盖hashCode让其按照Student中的信息进行判断
		return this.name.hashCode()+this.age*37;	
	}
	public boolean equals(Object obj){//覆盖equals来判断姓名和年龄是否都相同
		if(!(obj instanceof Student))//判断传入的是否是Student类型
			return false;
		Student s=(Student)obj;
		return this.name.equals(s.name)&&this.age==s.age;
	}
}

练习2、

获取一个字符串的字母出现的次数:

import java.util.*;
class TestDemo1 
{
	public static void main(String[] args) 
	{
		String str="sdfgesdgsdxjahskjsaxkskfdsfcbxbbfhj";
		char[] ch=str.toCharArray();//将字符串转化为字符数组
		TreeMap<Character,Integer> m=new TreeMap<Character,Integer>();//这地方选用TreeMap,因为他可以自动排序
		for(char c:ch)
		{
			Integer i=m.get(c);//查看是否存在
			if(i==null){
				m.put(c,1);
			}else{
				m.put(c,i+1);
			}
		}
		System.out.println(m);//直接输出
	}
}

集合扩展知识:

Collections类这个类提供了多种操作集合的方法例如

Collections.max()//返回Collection集合中的最大值

Collections.sort()//对List集合进行排序

Collections.binarySearch()//返回List集合相对的值

Collections.fill()//将List集合中的所有元素替换成指定元素

Collections.shuffle()//将List集合中数据重新随机排序

import java.util.*;
class CollectionsTest 
{
	public static void main(String[] args) 
	{
		ArrayList<String> al=new ArrayList<String>();//创建一个ArrayList集合
		al.add("asdasd");
		al.add("sdxa");
		al.add("zjdassa");
		al.add("asdasd");
		al.add("wdad");
		al.add("rdfgx");
		for(Iterator it=al.iterator();it.hasNext();){//遍历结果
			System.out.println(it.next());
		}

		System.out.println("排序后:");
		Collections.sort(al);
		for(Iterator it=al.iterator();it.hasNext();){
			System.out.println(it.next());
		}
		sop("输出最大值:");
		sop(Collections.max(al));

		sop("打乱顺序");
		Collections.shuffle(al);
		for(Iterator it=al.iterator();it.hasNext();){
			System.out.println(it.next());
		}
	}
	public static void sop(Object obj){
		System.out.println(obj);
	}
}

增强for循环也叫高级for循环

格式:for(数组类型变量 变量名:被遍历的集合(Collection)或数组)

JDK1.5新特性

1、可变参数:使用时一定要注意可变参数一定要放在参数列表的最后面

2、静态导入:当类重名是,需要指定具体的包名,当方法重名时,需要指定具备附属的对象或者类

格式:import static  包类.*;

静态导入只能导入类而不能导入方法;





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值