java学习心得之集合类浅谈

盛年不再来,一日难再晨,及时当自勉,岁月不待人。
java.util包中提供一些集合类,这些集合类又被称为容器。容器——数组。
数组和集合的区别:数组长度固定,集合长度可变;数组用来存放基本类型的数据,集合用来存放对象的引用。常用的集合有list集合,set集合,map集合,其中list与set继承collection,各接口还提供了不同的实现类。在这里插入图片描述
集合类图片链接
集合类包含collection体系——list集合-(了解泛型),set集合,map体系集合。
●List集合:无序、无下标、元素不可重复。( HashSet. LinkedHashSet. TreeSet )
●Map集合:存储-对数据,无序、无下标,键不可重复,值可重复。( HashMap、 HashTable )
●Collections :集合工具类,定义了除了存取以外的集合常用方法。
代码例题:

package com.lz.t0;

import java.util.HashSet;
import java.util.Set;

public class TestHashSet3 {

	public static void main(String[] args) {
		Student s1=new Student("tom",20,"male",99.0);
		Student s2=new Student("jack",23,"male",98.0);
		Student s3=new Student("mickel",22,"male",97.0);
		Student s4=new Student("annie",18,"male",96.0);
		Student s5=new Student("tom",20,"male",99.0);
		Set<Student> students =new HashSet<Student>();
		
		students.add(s1);
		students.add(s2);
		students.add(s3);
		students.add(s4);
		students.add(s1);
		for (Student s : students) {
			
			System.out.println(s.toString());
			
		}
		System.out.println(s1.equals(s5));
		
	}

}
class Student {
	String name;
	Integer age;
	String sex;
	Double score;
	public Student(String name, Integer age, String sex, Double score) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.score = score;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", score=" + score + "]";
	}
	
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 123;
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("--------------------");
		if(this==obj) {
			return true;
			
		}
		if(obj==null) {
			return false;
			
		}
		if(this.getClass()!=obj.getClass()) {
			return false;
		}
		
		Student stu=(Student)obj;
		if(this.name.equals(stu.name)&&this.age.equals(stu.age)&&this.sex.equals(stu.sex)&&this.score.equals(stu.score)) {
			return true;
		}
		return false;
	}
	
	
}

```java
package com.lz.home1;

import java.util.HashMap;
import java.util.Scanner;

public class TestHashMap5 {

	public static void main(String[] args) {
		Scanner  sc = new Scanner(System.in);
        System.out.print("请输入随机产生年份:");
        String len = sc.nextLine();
        HashMap<String,String> a=new HashMap<String,String>();
        a.put("1930", "乌拉圭");
        a.put("1934", "意大利");
        a.put("1938", "意大利");
        a.put("1950", "乌拉圭");
        a.put("1954", "德国");
        a.put("1958", "巴西");
        a.put("1962", "巴西");
        a.put("1966", "英格兰");
        a.put("1970", "巴西");
        a.put("1974", "德国");
        a.put("1978", "阿根廷");
        a.put("1982", "意大利");
        a.put("1986", "阿根廷");
        a.put("1990", "德国");
        a.put("1994", "巴西");
        a.put("1998", "法国");
        a.put("2002", "巴西");
        a.put("2006", "意大利");
		
		
        if(a.containsKey(len)==true)
        {
            System.out.println("该年夺得世界杯的是:"+a.get(len));
        }
        else
        {
            System.out.println("这年没世界杯");
        }
		Scanner sc1=new Scanner(System.in);
		System.out.println("输入一个国家查看获得大礼神杯的年份");
		
		String len1=sc1.nextLine();
		if(a.containsValue(len1)==true) {
			for (String k  : a.keySet()) {
				if(a.get(k).equals(len1)) {
					System.out.println(k);
				}
			}
		}
		else {
			System.out.println("这个国家没有获得大力神杯");
		}
	}

}

![在这里插入图片描述](https://img-blog.csdnimg.cn/2020030714124912.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hyYWluYW5kc25vdw==,size_16,color_FFFFFF,t_70)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值