int size()
import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; public class 获取功能 { public static void main(String[] args) { //创建Map集合对象 Map<String, String> m = new HashMap<String,String>(); //添加元素 m.put("Ash", "Thermite"); m.put("Twitch", "Montagne "); m.put("Glaz", "Fuze"); m.put("Blitz", "IQ"); //V get(Object key)返回指定键所映射的值 String a = m.get("Ash"); System.out.println("Values:"+a); //Set<K> keySet():获取所有的键的集合 Set<String> k = m.keySet(); System.out.println("AllKey:"+k); //Collection<V> values():获取所有的值的集合 Collection<String> v =m.values(); System.out.println("AllValues:"+v); } } //out: //Values:Thermite //AllKey:[Blitz, Twitch, Ash, Glaz] //AllValues:[IQ, Montagne , Thermite, Fuze] import java.util.HashMap; import java.util.Map; public class 添加删除长度功能 { public static void main(String[] args) { Map<String,String> m = new HashMap<String,String>(); //给集合添加元素 m.put("Ash", "Thermite"); m.put("Twitch", "Montagne "); m.put("Glaz", "Fuze"); m.put("Blitz", "IQ"); //void clear() 移除所有映射关系 // m.clear(); // System.out.println(m);//{} //V remove(Object key):删除键,返回值 System.out.println(m.remove("Ash"));//Thermite System.out.println(m.remove("Glaz"));//Fuze System.out.println(m);//{Blitz=IQ, Twitch=Montagne } //int size(); System.out.println(m.size());//2 } }i.
import java.util.HashMap; import java.util.Map; import java.util.Set; public class Map集合的遍历_键找值 { public static void main(String[] args) { //创建Map集合对象 Map<String, String> m = new HashMap<String,String>(); //给集合添加元素 m.put("Ash", "Thermite"); m.put("Twitch", "Montagne "); m.put("Glaz", "Fuze"); m.put("Blitz", "IQ"); //获取所有键的值 Set<String> s = m.keySet(); //遍历所有键的集合 for (String k : s) { String v = m.get(k); System.out.println(k+"---"+v); } } } //out: // Blitz---IQ // Twitch---Montagne // Ash---Thermite // Glaz---Fuze ii. import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Map集合的遍历_键值对对象找键和值 { public static void main(String[] args) { //创建Map集合对象 Map<String, String> m = new HashMap<String,String>(); //给集合添加元素 m.put("Ash", "Thermite"); m.put("Twitch", "Montagne "); m.put("Glaz", "Fuze"); m.put("Blitz", "IQ"); //获取 键值对 对象的集合 Set<Entry<String, String>> s = m.entrySet(); //遍历 键值对 对象的集合 for (Entry<String, String> me : s) { //键值对 对象中有两个方法 关于 Key 和 Values //K getKey():获取键 //V getValue():获取值 String key = me.getKey(); String values = me.getValue(); System.out.println(key+"---"+values); } } } //out: // Blitz---IQ // Twitch---Montagne // Ash---Thermite // Glaz---Fuze
两种排序:自然排序,比较器排序
import java.util.Set; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) { //创建TreeMap集合对象 TreeMap<String, Integer> t = new TreeMap<String, Integer>(); //添加元素 t.put("Ash", 21); t.put("Fuze", 21); t.put("Glaz", 21); t.put("Tom", 21); //遍历 Set<String> keySet = t.keySet(); for (String k : keySet) { Integer v = t.get(k); System.out.println(k+"---"+v); } } } //out: // Ash---21 // Fuze---21 // Glaz---21 // Tom---21
public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } 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; } } import java.util.Comparator; import java.util.Set; import java.util.TreeMap; public class TreeMapStudentDemo { public static void main(String[] args) { TreeMap<Student, String> t = new TreeMap<Student, String>(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { //按照年龄大小排序 int a = o1.getAge()-o2.getAge(); //按照名字内容 int b = a == 0? o2.getName().compareTo(o1.getName()):a; return b; } }); //创建学生对象 Student s1 = new Student("Tom",21); Student s2 = new Student("Ash",22); Student s3 = new Student("Peter",20); Student s4 = new Student("Rush",21); //添加 t.put(s1, "自动化"); t.put(s2, "自动化"); t.put(s3, "计算机"); t.put(s4, "计算机"); //获取所有的键的集合 Set<Student> keySet = t.keySet(); for (Student k : keySet) { String v = t.get(k); System.out.println(k.getName()+"---"+k.getAge()+"---"+v); } } } //out: // Peter---20---计算机 // Tom---21---自动化 // Rush---21---计算机 // Ash---22---自动化
public static void shuffle(List<?> list) 随机置换,打乱顺序
import java.util.ArrayList; import java.util.Collections; public class Demo01 { public static void main(String[] args) { ArrayList<Integer> a = new ArrayList<Integer>(); //添加元素: a.add(20) ; a.add(17); a.add(19) ; a.add(18) ; a.add(24) ; a.add(23) ; a.add(22) ; System.out.println(a); // public static <T> void sort(List<T> list) // 默认自然排序:将集合中的元素升序排序 Collections.sort(a); System.out.println(a); // public static <T> int binarySearch(List> list,T key):二分查找搜索法:key:查找的元素 int index = Collections.binarySearch(a, 20); System.out.println(index); // public static void reverse(List list):反转功能 Collections.reverse(a); System.out.println(a); // public static void shuffle(List<?> list):随机置换,打乱顺序 Collections.shuffle(a); System.out.println(a); } }