java学习笔记之Map、HashMap、TreeMap、Collections

Map

 

HashMap


遍历map:

方法一:使用keySet()方法

package com.mcq;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapDemo {
	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap();
		map.put("小明",18);
		map.put("小花",19);
		map.put("小丽",16);
		map.put("小花", 15);
		Set<String> keySet=map.keySet();
		for(String key:keySet){
			System.out.println(key+":"+map.get(key));
		}
	}
}

/*
小明:18
小丽:16
小花:15
*/
//Student.java
package com.mcq;
public class Student{
	private String name;
	private int age;
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	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;
	}
	@Override
	public int hashCode() { //右键source点hashCode()和equals()快捷生成
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}	

//HashMapDemp.java
package com.mcq;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapDemo {
	public static void main(String[] args) {
		Map<Student, Integer> map = new HashMap();
		map.put(new Student("小明", 18),99);
		map.put(new Student("小花",19),98);
		map.put(new Student("小丽",16),97);
		map.put(new Student("小花",20),101);
		Set<Student> keySet=map.keySet();
		for(Student key:keySet){
			System.out.println(key.getName()+" "+key.getAge()+":"+map.get(key));
		}
	}
}

方法二:使用entrySet方法():
 

package com.mcq;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapDemo {
	public static void main(String[] args) {
		Map<Student, Integer> map = new HashMap();
		map.put(new Student("小明", 18), 99);
		map.put(new Student("小花", 19), 98);
		map.put(new Student("小丽", 16), 97);
		map.put(new Student("小丽", 16), 97);
		map.put(new Student("小丽", 16), 97);
		Set<Entry<Student, Integer>> entrySet = map.entrySet();
		for (Entry<Student, Integer> entry : entrySet) {
			System.out.println(entry.getKey().getName()+" "+entry.getKey().getAge()+" "+entry.getValue());
		}
	}
}

TreeMap

匿名内部类方法:

package com.mcq;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;

public class TreeMapDemo {
	public static void main(String[] args) {
		Map<Student, Integer> map=new TreeMap(new Comparator<Student>() {

			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				int t=o1.getAge()-o2.getAge();
				return t==0?o1.getName().compareTo(o2.getName()):t;
			}
			
		});
		map.put(new Student("小明", 18), 99);
		map.put(new Student("小花", 19), 98);
		map.put(new Student("小丽", 16), 97);
		map.put(new Student("小丽", 16), 97);
		map.put(new Student("小丽", 16), 97);
		Set<Entry<Student, Integer>> entrySet = map.entrySet();
		for (Entry<Student, Integer> entry : entrySet) {
			System.out.println(entry.getKey().getName()+" "+entry.getKey().getAge()+" "+entry.getValue());
		}
	}
}

Collections工具类

包含处理集合的一些静态方法

Arrays工具类

包含处理数组的静态方法

实例:

package com.mcq;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionsDemo {
	public static void main(String[] args) {
		List<String> list=new ArrayList<String>();
		list.add("hello");
		list.add("world");
		list.add("haha");
		System.out.println(list);
		
		Collections.sort(list);//排序
		
		System.out.println(list);
		
		Collections.reverse(list);//反转
		
		System.out.println(list);
		
		Collections.shuffle(list);//洗牌,随机顺序
		
		System.out.println(list);
		
		List<Student> list2=Arrays.asList(new Student("a",16),new Student("c", 19),new Student("b", 15));//数组转list
		
		System.out.println(list2);
		
		Collections.sort(list2,new Comparator<Student>() {

			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				int t=o1.getAge()-o2.getAge();
				return t==0?o1.getName().compareTo(o2.getName()):t;
			}
			
		});
		System.out.println(list2);
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值