map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法
package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /*1.请简述Map 的特点 2.请简述HashMap的特点 3.请简述LinkedHashMap的特点 4.使用代码依次完成: a).将如下键值对信息存入Map集合中: "黄晓明", "Baby" "邓超", "孙俪" "李晨", "范冰冰" "大黑牛", "范冰冰" b).将"李晨"对应的值替换成"白百合" c).获取"大黑牛"对应的值,如果是"范冰冰"就删除以"大黑牛"为键的元素 5.使用代码完成:往一个Map集合中添加若干元素.获取Map中的所有key, 并使用增强for和迭代器遍历输出每个key 6.使用代码完成:往一个Map集合中添加若干元素.获取Map集合中所有的键值对(Entry)对象, 并使用增强for和迭代器遍历输出每个key和value 7.使用代码完成:将学员的信息作为元素存入HashMap中, 提示:1.每位学生(姓名,年龄)都有自己的家庭住址 2.将学生对象和家庭住址存储到map集合中。学生作为键, 家庭住址作为值。 3.学生姓名相同并且年龄相同视为同一名学生*/ public class Demo1 { public static void main(String[] args) { HashMap<String, String> map = new HashMap<String,String>(); map.put("黄晓明", "Baby"); map.put("邓超", "孙俪"); String str1=map.put("李晨", "范冰冰"); map.put("大黑牛", "范冰冰"); System.out.println(map); //如果键值相同,直接put就是替换 String str2=map.put("李晨", "白百合"); System.out.println(map); System.out.println(str1+"..."+str2); String str3=map.get("大黑牛"); if("范冰冰".equals(str3)) { map.remove("大黑牛"); } System.out.println(map); System.out.println("==========================================="); //keySet使用增强for遍历 Set<String> keys = map.keySet(); for(String key:keys) { System.out.println(key+"..."+map.get(key)); } //keySet使用iterator遍历 System.out.println("==========================================="); Set<String> set =map.keySet(); Iterator<String> it=set.iterator(); while(it.hasNext()) { String name = it.next(); System.out.println(name+"..."+map.get(name)); } //entrySet使用增强for遍历 System.out.println("==========================================="); Set<Map.Entry<String,String>> entry = map.entrySet(); for(Map.Entry<String, String> key:entry) { System.out.println(key.getKey()+"..."+key.getValue()); } //entrySet使用iterator遍历 System.out.println("==========================================="); Set<Map.Entry<String,String>> set1 =map.entrySet(); Iterator<Map.Entry<String,String>> it1=set1.iterator(); while(it1.hasNext()) { Map.Entry<String,String> entry1= it1.next(); System.out.println(entry1.getKey()+"..."+entry1.getValue()); } HashMap<Student, String> students = new HashMap<Student,String>(); students.put(new Student("zhangsan",20), "北京"); students.put(new Student("lisi",21), "上海"); students.put(new Student("wangwu",22), "广州"); students.put(new Student("zhaoliu",23), "深圳"); students.put(new Student("zhaoliu",23), "香港"); for(Student stu:students.keySet()) { System.out.println(stu.toString()+"..."+students.get(stu)); } } } class Student{ private String name; private int age; private String address; 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Student(String name, int age, String address) { super(); this.name = name; this.age = age; this.address = address; } public Student(String name, int age) { super(); this.name = name; this.age = age; } public Student() { super(); // TODO Auto-generated constructor stub } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((address == null) ? 0 : address.hashCode()); 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 (address == null) { if (other.address != null) return false; } else if (!address.equals(other.address)) return false; 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; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", address=" + address + "]"; } }
去除相同值的元素
package com.swift.tuozhan; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /*Map中有{张三=3,李四=4,王五=5,赵六=6,孙七=7,田七=7,周八=5}这些元素. 将Map中value相同的元素从Map中去除(value为7和5是相同的,都要删除). 再将Map中剩余元素的key存放到一个ArrayLis中,并打印到控制台上*/ public class Tuozhan1 { public static void main(String[] args) { Map<String,Integer> map=new HashMap<String,Integer>(); List<Integer> chongfulie = new ArrayList<Integer>(); map.put("张三",3); map.put("李四",4); map.put("王五",5); map.put("赵六",6); map.put("孙七",7); map.put("田七",7); map.put("周八",5); for(String str:map.keySet()) { int n=map.get(str); chongfulie.add(n); } System.out.println(chongfulie); //也可以下面这样做,得到重复值 Collection<Integer> x = map.values(); ArrayList<Integer> a=new ArrayList<Integer>(x); System.out.println(a); //得到重复值 List<Integer> chongfuzhi= new ArrayList<Integer>(); for(int i=0;i<chongfulie.size();i++) { int zhi=chongfulie.get(i); for(int j=i+1;j<chongfulie.size();j++) { if(zhi==chongfulie.get(j)) { chongfuzhi.add(zhi); } } } System.out.println(chongfuzhi); //得到重复键 List<String> chongfujian= new ArrayList<String>(); for(String str:map.keySet()) { Integer n1 = map.get(str); if(chongfuzhi.contains(n1)) { chongfujian.add(str); } } //遍历重复键,将他们都删掉 for(String str:chongfujian) { map.remove(str); } System.out.println(map); //剩余的key 打印 Set<String> key=map.keySet(); ArrayList<String> arr=new ArrayList<>(); arr.addAll(key); System.out.println(arr); } }
去除相同值,又敲了一遍,看看有什么不同
package com.swift.tuozhan; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; /*Map中有{张三=3,李四=4,王五=5,赵六=6,孙七=7,田七=7,周八=5}这些元素. 将Map中value相同的元素从Map中去除(value为7和5是相同的,都要删除). 再将Map中剩余元素的key存放到一个ArrayLis中,并打印到控制台上*/ public class Tuozhan1 { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("张三", 3); map.put("李四", 4); map.put("王五", 5); map.put("赵六", 6); map.put("孙七", 7); map.put("田七", 7); map.put("周八", 5); //得到相同的值 Set<Integer> set=sameValue(map); //想成循环中循环时,想一下contains List<String> list=new ArrayList<>(); for(String str:map.keySet()) { if(set.contains(map.get(str))){ list.add(str); } } System.out.println(list); //list中是要干掉的键值key for(String str:list) { map.remove(str); } System.out.println(map); } private static Set<Integer> sameValue(Map<String, Integer> map) { List<Integer> list = new ArrayList<>(); for (String str : map.keySet()) { int n = map.get(str); list.add(n); } System.out.println(list); Set<Integer> set = new HashSet<>(); for (int i = 0; i < list.size(); i++) for (int j = i + 1; j < list.size(); j++) { if (list.get(i) == list.get(j)) { set.add(list.get(i)); } } System.out.println(set); return set; } }
Properties的流操作,长久保存
package com.swift.lianxi; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; import java.util.Set; /*1.请简述下Properties的特点 不能放null 线程安全,速度慢 2.使用代码完成:使用Properties集合,完成把集合内容存储到IO流所对应文件中的操作 3.使用代码完成:从属性集文件prop.properties中取出数据,保存到集合中 4.简述可变参数的作用 5.使用代码完成: a).ArrayList集合中依次添加元素:1,2,3,4,5; b).使用Collections工具类将元素顺序打乱后打印所有元素 c).使用Collections工具类将元素顺序排序后打印所有元素*/ public class Demo2 { public static void main(String[] args) throws FileNotFoundException, IOException { //将数据添加到Properties集合 Properties pro = new Properties(); pro.setProperty("zhangsan", "beijing"); pro.setProperty("lisi", "shanghai"); pro.setProperty("wangwu", "tianjin"); pro.setProperty("zhaoliu", "shenzhen"); //Properties集合内容存储到文件 pro.store(new FileWriter("mingzhi.txt"),"a"); pro.load(new FileReader("prop.properties")); Set<String> names = pro.stringPropertyNames(); for(String str:names) { System.out.println(str+"..."+pro.getProperty(str)); } } }
两种用map记录单词或字母个数的方法
package com.swift.kaoshi; import java.util.HashMap; //If you want to change your fate I think you must come to the dark horse to learn java 统计单词个数 public class Count { public static void main(String[] args) { String str = "If you want to change your fate I think you must come to the dark horse to learn java "; str.trim(); String[] words = str.split("\\s+"); //两遍法,第一遍放到map中去重,第二遍放到map中计数 for (String word : words) { System.out.println(word); } HashMap<String, Integer> map = new HashMap<String, Integer>(); // for (String word : words) { // // map.put(word, 0); // } // System.out.println(map); // for (String s : words) { // int num = map.get(s) + 1; // map.put(s, num); // } // System.out.println(map); //另一种方法 遍历一遍,如果值为空则放1,如果值不为空则取出加一 for(String word:words) { if(map.get(word)==null) { map.put(word, 1); }else { map.put(word,map.get(word)+1); } } System.out.println(map); } }
另一种方法可以使用map的containsKey方法,先判断map集合中是否有这个单词或字母,如果没有,则把他的值放入1,如果有则把他的值取出并加1
变为如下代码:
if(!map.containsKey(word)){
map.put(word,1);
}else{
map.put(word,map.get(word)+1);
}
Never waste time any more, Never old man be a yong man