去除相同值的元素 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);
}