list,set练习和Map简述

任务一:

按字符串长度排序

定义TreeSet集合

public static void main(String[] args) {
		Com com = new Com();            
		Set set = new TreeSet(com);      // 传入参数
		set.add("adn");
		set.add("ndlkdj");
		set.add("bv");
		set.add("acfun");
		set.add("bilibili");
		System.out.println(set);
					
	}	
}

定义类接comparator接口

class Com implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		if (!(o1 instanceof String && o2 instanceof String)) {
			throw new RuntimeException();
			
		}
		String s1 = (String) o1;
		String s2 = (String) o2;
		if (s1.length()>s2.length()) {
			return 1;
		}else if (s2.length()>s1.length()) {
			return -1;
		}
		if (s1.length()==s2.length()) {            //避免两个字符串长度相时的情况。进行内容比较
			return s1.compareTo(s2);
		}else {
			return s1.length()>s2.length()?1:-1;
		}
//		return 0;
	}
	
}
运行结果

任务二:

将字符串"90 -7 0 18 2 45 4"中的数值进行排序。

public static void main(String[] args) {
		Set set = new TreeSet<>();
		String str = "90 0 -3 56 24 26";
		
		String[] split = str.split(" ");          //空格切割字符串
		for (int i = 0; i < split.length; i++) {     //遍历数组
			String stri = split[i];
			set.add(Integer.valueOf(stri));     //装箱成Integer类
			
		}
		
		System.out.println(set);
	}

Map集合

HashMap     和    遍历取出元素

往后写集合时,要注意加上泛型

	public static void main(String[] args) {
		Map<String,String> map = new HashMap<>();
		map.put("a", "暴风");
		map.put("b", "影音");
		map.put("s", "网址");

		Set<Entry<String, String>> entrySet = map.entrySet();       //遍历map
		
		Iterator<Entry<String, String>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Entry<String, String> entry = iterator.next();
			entry.getKey();
			entry.getValue();
			System.out.println(entry.getKey()+entry.getValue());
		}
	}

更复杂一点的map两种遍历方法

class Student{
	private String name;
	private int num;
	public Student(String name, int num){
		this.name = name;
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", num=" + num + "]";
	}
	
}

主方法先定义list集合,在声明map集合

List<Student> list = new ArrayList<>();
		list.add(new Student("sara",22));
		list.add(new Student("nokita",23));
		list.add(new Student("hakii",25));
		list.add(new Student("whales",26));
		
		Map<String,List> map = new HashMap<>();                 //注意泛型
		map.put("andriod", list);

遍历方法一:while嵌套循环

	Set<Entry<String, List>> entrySet = map.entrySet();             //方法一
		Iterator<Entry<String, List>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {                                       //while循环嵌套
			Map.Entry<String, List> entry = (Map.Entry<String, List>) iterator.next();
			String firstname = entry.getKey();
			List<Student> list2 = entry.getValue();
			
			Iterator<Student> iterator2 = list2.iterator();      
			while (iterator2.hasNext()) {
				Student student = iterator2.next();
				System.out.println(firstname+"---"+student.getName()+"...."+student.getNum());      //获取名字和姓名
			}
		}

遍历方法二:高级for循环(更简洁)

//		for (集合中的元素类型        变量名 : 要遍历的集合) {
		
//		}高级for循环
		for (Entry<String, List> entry : entrySet) {                //方法二
			String key = entry.getKey();
			List<? extends Student> value = entry.getValue();
			
			for (Student student : value) {
				System.out.println(key +"----"+student.getName()+"----"+student.getNum());
			}
		}
运行结果





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值