自动打包&解包:auto-boxing&unboxing

在合适的时机自动打包、解包

  自动将基础类型转换为对象

  自动将对象转换为基础类型

import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.*; public class Test { public static void main(String[] args) { Map m1 = new HashMap();Map m2 = new TreeMap(); //m1.put("one",new Integer(1)); m1.put("one",1); //m1.put("two",new Integer(2)); m1.put("two",2); //m1.put("three",new Integer(3)); m1.put("three",3); //m2.put("A",new Integer(1)); m2.put("A",1); //m2.put("B",new Integer(2)); m2.put("B",2); System.out.println(m1.size()); System.out.println(m1.containsKey("one")); System.out.println(m2.containsValue(1)); //(m2.containsValue(new Integer(1))); if(m1.containsKey("two")) { //int i = ((Integer)m1.get("two")).intValue(); int i = (Integer)m1.get("two"); System.out.println(i); } Map m3 = new HashMap(m1); m3.putAll(m2); System.out.println(m3); /*List l1 = new LinkedList(); l1.add(new Name("Karl","M")); l1.add(new Name("Steven","Lee")); l1.add(new Name("John","O")); l1.add(new Name("Tom","M")); System.out.println(l1); Collections.sort(l1); System.out.println(l1);*/ /* Set s = new HashSet(); s.add("hello"); s.add("world"); s.add(new Name("f1","11")); s.add(new Integer(100)); */ /* s.add("hello"); s.add("hello"); */ //Set /* Set s1 = new HashSet(); Set s2 = new HashSet(); s1.add("a");s1.add("b");s1.add("c"); s2.add("d");s2.add("a");s2.add("b"); Set sn = new HashSet(s1); sn.retainAll(s2); Set su = new HashSet(s1); su.addAll(s2); System.out.println(sn); System.out.println(su); */ /* Collection c = new HashSet(); c.add("hello"); c.add(new Name("f1","11")); c.add(new Name("f2","12")); c.add(new Name("f3","13")); c.add(new Integer(100)); c.remove("hello"); c.remove(new Integer(100)); Iterator i = c.iterator(); while(i.hasNext()) { Name n = (Name)i.next(); System.out.print(n.getfirstName()+" "); }*/ /*System.out.println(c.remove(new Name("f1","11"))); System.out.println(c);*/ } } class Name implements Comparable { private String firstName,secondName; public Name(String firstName,String secondName) { this.firstName = firstName; this.secondName = secondName; } public String getfirstName() {return firstName;} public String getsecondName() {return secondName;} public String toString() { return firstName+" "+secondName; } public boolean equals(Object obj) { if(obj instanceof Name) { Name name = (Name) obj; return (firstName.equals(name.firstName))&&(secondName.equals(name.secondName)); } return super.equals(obj); } public int hashCode() { return firstName.hashCode(); } public int compareTo(Object o) { Name n = (Name) o; int lastCmp = secondName.compareTo(n.secondName); return (lastCmp!=0 ? lastCmp:firstName.compareTo(n.firstName)); } }
原文地址:https://www.cnblogs.com/lsswudi/p/11367657.html