本文共 20039 字,大约阅读时间需要 66 分钟。
equals对比的是值和长度
==对比地址 就当等号不存在,无脑使用equals。System.out.println("a" == "a"); //true System.out.println("a".equals("a")); //true System.out.println("a".equals(new String("a"))); //true System.out.println("a" == new String("a")); //false System.out.println(new String("a") == new String("a")); //false System.out.println(new String("a").equals(new String("a"))); //true
更要命的是,这些方法在特定情况下也是能够返回true的,具体原因可以通过阅读源码获知。
System.out.println("a".concat("") == "a"); //ture //replaceAll由于用到了StringBuffer的toString方法,会一直返回false System.out.println("a".toLowerCase() == "a"); //ture System.out.println("1;2".split("3")[0] == "1;2"); //ture
在JAVA中拼接两个字符串的最简便的方式就是使用操作符”+”了。如果你用”+”来连接固定长度的字符串,可能性能上会稍受影响,但是如果你是在循环中来”+”多个串的话,性能将指数倍的下降。假设有一个字符串,我们将对这个字符串做大量循环拼接操作,使用”+”的话将得到最低的性能。但是究竟这个性能有多差?
1.String 是final对象,不会被修改,每次使用 + 进行拼接都会创建新的对象,而不是改变原来的对象,也属于线程安全的; 2.StringBuffer可变字符串,主要用于字符串的拼接,属于线程安全的;(StringBuffer的append操作用了synchronized) 3.StringBuilder可变字符串,主要用于字符串的拼接,属于线程不安全的;import java.util.ArrayList;import java.util.List;import org.apache.commons.lang3.StringUtils;public class TestString { public static void main(String[] args) { //plus拼接字符串方式 String s = ""; long ts = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { s = s + String.valueOf(i); } long te = System.currentTimeMillis(); System.out.println("Plus cost {"+( te - ts) +"} ms"); //concat拼接字符串方式 String s2 = ""; long ts2 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { s2 = s2.concat(String.valueOf(i)); } long te2 = System.currentTimeMillis(); System.out.println("concat cost {"+(te2 - ts2)+"} ms"); //StringUtils.join拼接字符串方式 Listlist = new ArrayList (); long ts3 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { list.add(String.valueOf(i)); } StringUtils.join(list, ""); long te3 = System.currentTimeMillis(); System.out.println("StringUtils.join cost {"+(te3 - ts3)+"} ms"); //StringBuffer拼接字符串方式 StringBuffer sb = new StringBuffer(); long ts4 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { sb.append(String.valueOf(i)); } sb.toString(); long te4 = System.currentTimeMillis(); System.out.println("StringBuffer cost {"+(te4 - ts4)+"} ms"); //StringBuilder拼接字符串方式 StringBuilder sb5 = new StringBuilder(); long ts5 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { sb5.append(String.valueOf(i)); } sb5.toString(); long te5 = System.currentTimeMillis(); System.out.println("StringBuilder cost {"+(te5 - ts5)+"} ms"); }}
StringBuilder strB = new StringBuilder();
System.out.println("StringBuilder:"+strB.append("ch").append("111").append('c'));//return "StringBuilder:ch111c"
System.out.println("String:"+strB.toString());//return "String:ch111c"
strB.setCharAt(2, 'd');System.out.println("StringBuilder.setCharAt:" + strB);//return "StringBuilder.setCharAt:chd11c"
ystem.out.println("StringBuilder.insertString:"+ strB.insert(2, "LS"));//return "StringBuilder.insertString:chLSd11c"System.out.println("StringBuilder.insertChar:"+ strB.insert(2, 'L'));//return "StringBuilder.insertChar:chLLSd11c"
System.out.println("StringBuilder.delete:"+ strB.delete(2, 4));//return "StringBuilder.delete:chSd11c"
7.StringBuilder类的reverse()实现字符串反转
public static String reverse1(String str) { StringBuilder stringBuilder = new StringBuilder(str); StringBuilder str2 = stringBuilder.reverse(); return str2.toString(); }
,而StringBuilder可以自由扩展大小,StringBuilder内存分配在堆区
StringBuffer线程安全
private void mathMethod(){ // Math.sqrt()//计算平方根 Math.cbrt()//计算立方根 Math.hypot(x,y)//计算 (x的平方+y的平方)的平方根 Log.d("TAG","Math.sqrt(16)----:"+Math.sqrt(16));//4.0 Log.d("TAG","Math.cbrt(8)----:"+Math.cbrt(8));//2.0 Log.d("TAG","Math.hypot(3,4)----:"+Math.hypot(3,4));//5.0 // Math.pow(a,b)//计算a的b次方 Math.exp(x)//计算e^x的值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.pow(3,2)----:"+Math.pow(3,2));//9.0 Log.d("TAG","Math.exp(3)----:"+Math.exp(3));//20.085536923187668 //Math.max();//计算最大值 Math.min();//计算最小值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.max(2.3,4.5)----:"+Math.max(7,15));//15 Log.d("TAG","Math.min(2.3,4.5)----:"+Math.min(2.3,4.5));//2.3 //Math.abs求绝对值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.abs(-10.4)----:"+Math.abs(-10.4));//10.4 Log.d("TAG","Math.abs(10.1)----:"+Math.abs(10.1));//10.1 //Math.ceil天花板的意思,就是返回大的值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.ceil(-10.1)----:"+Math.ceil(-10.1));//-10.0 Log.d("TAG","Math.ceil(10.7)----:"+Math.ceil(10.7));//11.0 Log.d("TAG","Math.ceil(-0.7)----:"+Math.ceil(-0.7));//-0.0 Log.d("TAG","Math.ceil(0.0)----:"+Math.ceil(0.0));//0.0 Log.d("TAG","Math.ceil(-0.0)----:"+Math.ceil(-0.0));//-0.0 Log.d("TAG","Math.ceil(-1.7)----:"+Math.ceil(-1.7));//-1.0 //Math.floor地板的意思,就是返回小的值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.floor(-10.1)----:"+Math.floor(-10.1));//-11.0 Log.d("TAG","Math.floor(10.7)----:"+Math.floor(10.7));//10.0 Log.d("TAG","Math.floor(-0.7)----:"+Math.floor(-0.7));//-1.0 Log.d("TAG","Math.floor(0.0)----:"+Math.floor(0.0));//0.0 Log.d("TAG","Math.floor(-0.0)----:"+Math.floor(-0.0));//-0.0 //Math.random 取得一个大于或者等于0.0小于不等于1.0的随机数[0,1 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.random()----:"+Math.random());//输出[0,1)间的随机数 0.8979626325354049 Log.d("TAG","Math.random()*100----:"+Math.random()*100);//输出[0,100)间的随机数 32.783762836248144 // Math.rint 四舍五入 返回double值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.rint(10.1)----:"+Math.rint(10.1));//10.0 Log.d("TAG","Math.rint(10.7)----:"+Math.rint(10.7));//11.0 Log.d("TAG","Math.rint(-10.5)----:"+Math.rint(-10.5));//-10.0 Log.d("TAG","Math.rint(-10.51)----:"+Math.rint(-10.51));//-11.0 Log.d("TAG","Math.rint(-10.2)----:"+Math.rint(-10.2));//-10.0 Log.d("TAG","Math.rint(9)----:"+Math.rint(9));//9.0 //Math.round 四舍五入 float时返回int值,double时返回long值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.round(10.1)----:"+Math.round(10.1));//10 Log.d("TAG","Math.round(10.7)----:"+Math.round(10.7));//11 Log.d("TAG","Math.round(-10.5)----:"+Math.round(-10.5));//-10 Log.d("TAG","Math.round(-10.51)----:"+Math.round(-10.51));//-11 Log.d("TAG","Math.round(-10.2)----:"+Math.round(-10.2));//-10 Log.d("TAG","Math.round(9)----:"+Math.round(9));//9 //Math.nextUp(a) 返回比a大一点点的浮点数 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.nextUp(1.2)----:"+Math.nextUp(1.2));//1.2000000000000002 //Math.nextDown(a) 返回比a小一点点的浮点数 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.nextDown(1.2)----:"+Math.nextDown(1.2));//1.1999999999999997 //Math.nextAfter(a,b) 返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.nextAfter(1.2, 2.7)----:"+Math.nextAfter(1.2, 2.7));//1.2000000000000002 Log.d("TAG","Math.nextAfter(1.2, -1)----:"+Math.nextAfter(1.2, -1));//1.1999999999999997}
获取系统当前毫秒值
long start = System.currentTimeMillis();
结束正在运行的Java程序
System.exit(0);
用来运行JVM中的垃圾回收器,完成内存中垃圾的清除。
class Student{ //清除垃圾时,会默认调用被清空对象的finalize方法。 public void finalize() { System.out.println("垃圾已经被收取啦!"); }}public class SystemDemo { public static void main(String[] args) { new Student(); new Student(); new Student(); new Student(); new Student(); System.gc(); }}
System类方法复制数组
public class SystemDemo { public static void main(String[] args) { int[] src = { 1,22,333,4444,5555,666666,7777777}; int[] dest = { 10,20,30}; System.arraycopy(src, 2, dest, 0, 2); for(int i=0;i
hashCode()方法
public class MyTest { public static void main(String[] args) { Object obj = new Object(); //创建Object类对象 //int hashCode () 返回该对象的哈希码值。不同对象的哈希码值,是不一样的。 //System.out.println(obj); 如果执行这条语句,打印的是局部变量obj里存放的地址值 int i = obj.hashCode(); System.out.println(i); Object obj2 = new Object(); int i1 = obj2.hashCode(); System.out.println(i1); }}
getClass()方法
public class MyTest2 { public static void main(String[] args) { // Class getClass () 返回该类的字节码文件对象 //万物皆对象 //Object.class 字节码文件加载进内存-----JVM就会为Object.class文件创建一个对象。 Object obj = new Object(); Class clazz = obj.getClass(); //Object.class---->字节码文件对象。 Object obj2 = new Object(); Class aClass = obj2.getClass(); Object obj3 = new Object(); Class aClass1 = obj3.getClass(); System.out.println(obj==obj2); //false //局部变量中存放的是地址值,对象不一样,所以地址值肯定不一样 System.out.println(clazz==aClass); //true System.out.println(aClass==aClass1);//true 因为类只有一个字节码文件, }}
toString()方法
public class MyTest { public static void main(String[] args) { Object obj = new Object(); String s = obj.toString(); //获取该对象的地址值的字符串表现形式java.lang.Object@1540e19d System.out.println(s); Object o = new Object(); System.out.println(o.toString()); System.out.println(o); }}
equals()方法
/** * @Author: Administrator * @CreateTime: 2019-04-20 10:03 */public class MyTest { public static void main(String[] args) { Object obj = new Object(); Object obj2 = new Object(); // boolean equals (Object obj) 判断两个对象的地址值是否相同 System.out.println(obj == obj2); boolean b = obj.equals(obj2); System.out.println(b); System.out.println("---------------------"); Student s1 = new Student("张三", 23); Student s2 = new Student("张三", 23); System.out.println(s1 == s2);//false //System.out.println(s1.equals(s2)); //true /*我觉得,s1.equals(s2) 比较两个对象的地址值,是否相同,对我来说意义不大, 我认为的是,只要两个对象的成员变量的值一样,我就认为这两个对象一样。 那么我就得重写父类的equals()方法,让他去比较两个对象的成员变量的值是否相同 */ boolean b1 = s1.equals(new Teacher());//ClassCastException 类型转换异常 System.out.println(b1); System.out.println("---------------"); boolean b2 = s1.equals(s1); System.out.println(b2); }}class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Student)) { return false; } //向下转型 Student stu = (Student) obj; //字符串类,也认为父类的equals方法不满意,字符串这个类想要比较,两个字符串的内容是否相同 // 所以字符串也重写了父类的equals方法 去比较两个字符串的内容是否相同 return this.name.equals(stu.name) && this.age == stu.age; }}class Teacher { }
Arrays.asList()方法
注意:该方法返回的是 Arrays 内部静态类 ArrayList,而不是我们平常使用的 ArrayList,,该静态类 ArrayList 没有覆盖父类的 add(), remove() 等方法,所以如果直接调用,会报 UnsupportedOperationException 异常Listlist = Arrays.asList(1, 2, 3);list.forEach(System.out::println); // 1 2 3Integer[] data = { 1, 2, 3};List list = Arrays.asList(data);list.forEach(System.out::println); // 1 2 3
Arrays.fill(Object[] array, Object obj)方法
用指定元素填充整个数组 (会替换掉数组中原来的元素)Integer[] data = { 1, 2, 3, 4};Arrays.fill(data, 9);System.out.println(Arrays.toString(data)); // [9, 9, 9, 9]Integer[] data = { 1, 2, 3, 4};//用指定元素填充数组,从起始位置到结束位置,取头不取尾 (会替换掉数组中原来的元素)Arrays.fill(data, 0, 2, 9);System.out.println(Arrays.toString(data)); // [9, 9, 3, 4]
Arrays.sort(Object[] array)
对数组元素进行排序 (串行排序)String[] data = { "1", "4", "3", "2"};System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]Arrays.sort(data);System.out.println(Arrays.toString(data)); // [1, 2, 3, 4]
Arrays.equals(Object[] array1, Object[] array2)
判断两个数组是否相等 数组元素为基本数据类型时,依次比较值 数组元素为引用数据类型时,依次调用元素的 equals() 方法进行比较Integer[] data1 = { 1, 2, 3};Integer[] data2 = { 1, 2, 3};System.out.println(Arrays.equals(data1, data2)); // true//判断两个多维数组是否相等Integer[][] data1 = { { 1,2,3}, { 1,2,3}};Integer[][] data2 = { { 1,2,3}, { 1,2,3}};System.out.println(Arrays.deepEquals(data1, data2)); // true
Arrays.hashCode(Object[] array)
返回数组的哈希值Integer[] data = { 1, 2, 3};System.out.println(Arrays.hashCode(data)); // 30817//返回多维数组的哈希值Integer[][] data = { { 1, 2, 3}, { 1, 2, 3}};System.out.println(Arrays.deepHashCode(data)); // 987105
Arrays.toString(Object[] array)
返回数组元素的字符串形式Integer[] data = { 1, 2, 3};System.out.println(Arrays.toString(data)); // [1, 2, 3]//返回多维数组元素的字符串形式Integer[][] data = { { 1, 2, 3}, { 1, 2, 3}};System.out.println(Arrays.deepToString(data)); // [[1, 2, 3], [1, 2, 3]]
Arrays.setAll(T[] array, IntFunction<? extends T> generator)
让数组中的所有元素Integer[] data = { 1, 2, 3, 4};// i为索引值Arrays.setAll(data, i -> data[i] * 2);System.out.println(Arrays.toString(data)); // [2, 4, 6, 8]Integer[] data = { 1, 2, 3, 4};// i为索引值Arrays.parallelSetAll(data, i -> data[i] * 2);System.out.println(Arrays.toString(data)); // [2, 4, 6, 8]Integer[] data = { 2, 3, 4, 5};// 第一个元素2不变,将其与第二个元素3一起作为参数x, y传入,得到乘积6,作为数组新的第二个元素// 再将6和第三个元素4一起作为参数x, y传入,得到乘积24,作为数组新的第三个元素,以此类推Arrays.parallelPrefix(data, (x, y) -> x * y);System.out.println(Arrays.toString(data)); // [2, 6, 24, 120]Integer[] data = { 2, 3, 4, 5};// 第一个元素2不变,将其与第二个元素3一起作为参数x, y传入,得到乘积6,作为数组新的第二个元素// 再将6和第三个元素4一起作为参数x, y传入,得到乘积24,作为数组新的第三个元素,以此类推Arrays.parallelPrefix(data, 0, 3, (x, y) -> x * y);System.out.println(Arrays.toString(data)); // [2, 6, 24, 5]
Arrays.stream(T[] array)
Integer[] data = { 1, 2, 3, 4};Listlist = Arrays.stream(data).collect(toList());System.out.println(list); // [1, 2, 3, 4]
Date类不常用,很多方法被废弃了,常用它的两个构造方法来new一个Date对象。
Date d1 = new Date(); //不传任何参数,代表当前时间点System.out.println(d1);//输出Sat Jul 13 09:51:50 CST 2019//在Java中以1970年1月1日 00:00:00为时间原点,由于时区问题,祖国时间是8:00:00Date d2 = new Date(4000); //传入一个long型数值,表示在时间原点之后System.out.println(d2);//输出,时间过了4秒Thu Jan 01 08:00:04 CST 1970
@Testpublic void testCollection(){ // 创建Collection接口的实现 Collection collection = new ArrayList<>(); // 添加元素 collection.add("嘻嘻"); String src = "????"; collection.add(src); System.out.println(collection); // 创建Collection的实现 Collectioncoll = new HashSet<>(); coll.add("?"); coll.add("?"); coll.add("?"); System.out.println(coll); // 添加一个集合数据 collection.addAll(coll); // 输出集合的长度 System.out.println(collection); // 判断是否包含 System.out.println(collection.contains("?")); // 移除元素 collection.remove("?"); // 添加对象 System.out.println("-------"); collection.add(null); Collection collection1 = new ArrayList<>(); collection1.add("嘻嘻"); collection1.add("?"); // 求两个集合的交集(只保留collection1存在的元素) collection.retainAll(collection1); System.out.println(collection); // 清空元素 collection.clear(); System.out.println(collection);}
ListsyncList = Collections.synchronizedList(new ArrayList ()); //相当于是把new ArrayList ()这个list包装了下, //编程可同步的新的list作者:Chaweys链接:https://www.jianshu.com/p/b8fcf369969e来源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
List集合的遍历有三种方式:
//使用普通的for循环 for (int i = 0; i < list.size(); i++) { String s = list.get(i); System.out.println(s); } //使用迭代器Iteratoriterator = list.iterator(); while (iterator.hasNext()) { String next = iterator.next(); System.out.println(next); } //用增强for for (String s : list) { System.out.println(s); }
增加 add(null);
删除 remove(news); 对比查找 contains(news); 清空集合 clear(); 获取长度 size();public class NewsHashSet { public static void main(String[] args) { News news = new News(1, "北京终于放晴了!", "新闻社"); News news2 = new News(2, "香港回归纪念日", "人民新闻"); News news3 = new News(3, "假奶粉事件曝光", "人民新闻网"); //创建HashSet集合,储存无序,唯一的数据 // HashSet是是使用equals来进行对象对比,确定数据是唯一的 // 如果两个数据的对象是一致的,那么HashSet将会把这两个合并,只储存一个空间 HashSetset = new HashSet (); set.add(news); set.add(news2); set.add(news3); //由于HashSet储存数据都是无序的,所以不能用get(i);来获取具体对象 // 所以我们必须通过遍历来得到HashSet的各个数据,由于是没有索引的 // 所以不能使用普通类型的for来遍历它 //HashSet只能通过增强型for和迭代器来遍历它 // 增强型for for(News n : set){ System.out.println(n.getID()+"\t"+n.getLitter()+"\t"+n.getAuthor()); } System.out.println("*****************************************"); //迭代器 Iterator it = set.iterator(); while (it.hasNext()) { News n = it.next(); System.out.println(n.getID()+"\t"+n.getLitter()+"\t"+n.getAuthor()); } //set的各种方法 // set增加 set.add(null); //删除 set.remove(news); //对比查找 set.contains(news); //清空集合 set.clear(); //获取长度 set.size(); }}
// 假设set是TreeSet对象,并且set中元素是String类型String[] arr = (String[])set.toArray(new String[0]);//增强for循环for (String str:arr) System.out.printf("for each : %s\n", str);// 假设set是TreeSet对象for(Iterator iter = set.descendingIterator(); iter.hasNext(); ) { iter.next();}public class TreeSetDemo { public static void main(String args[]) { // Create a tree set TreeSet ts = new TreeSet(); // Add elements to the tree set ts.add("C"); ts.add("A"); ts.add("B"); ts.add("E"); ts.add("F"); ts.add("D"); System.out.println(ts); }}//return [A, B, C, D, E, F]
public class Demo02_LinkedHashSet { public static void main(String[] args) { LinkedHashSetlhs = new LinkedHashSet<>(); lhs.add("a"); lhs.add("a"); lhs.add("a"); lhs.add("a"); lhs.add("b"); lhs.add("c"); lhs.add("d"); System.out.println(lhs); }}-
HashMap
TreeMap
LinkedHashMap
HashTable