Java Collections - Array 和 String 常用函数

Posted 逆風的薔薇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Collections - Array 和 String 常用函数相关的知识,希望对你有一定的参考价值。

Array基本操作

完整代码请戳?GitHub

Array to String

使用 Arrays.toString(...) 将数组转换为基本字符串。

String[] array = new String[] "One", "Two", "Three";

System.out.println(array); // [Ljava.lang.String;@61bbe9ba
System.out.println(Arrays.toString(array)); // [One, Two, Three]

 使用Array.deepToString(...) 转换多维数组。

String[] arr1 = new String[] "Four", "Five";
String[] arr2 = new String[] "Six", "Seven";

String[][] arrayOfArray = new String[][] arr1, arr2;
System.out.println(arrayOfArray); // [[Ljava.lang.String;@610455d6
System.out.println(Arrays.deepToString(arrayOfArray)); // [[Four, Five], [Six, Seven]]

Clone Array

  1. array.clone()
  2. Array.copyOf(...)
  3. System.copyOf(...)
String[] names = "Jeff", "Terry", "Ria";
String[] cloneOfNames = names.clone(); //Using arr.clone() method -- Recommended

String[] copyOfNames = Arrays.copyOf(names, names.length); //Using Arrays.copyOf() method -- most readable

String[] copyOfNames2 = new String[names.length];
System.arraycopy(names, 0, copyOfNames2, 0, copyOfNames2.length); //Using System.arraycopy() method -- Equally efficient but less readable

System.out.println(Arrays.toString(names));
System.out.println(Arrays.toString(cloneOfNames));
System.out.println(Arrays.toString(copyOfNames));
System.out.println(Arrays.toString(copyOfNames2));

Copy Array by Range

Arrays.copyOfRange(...)

String[] partialNames = Arrays.copyOfRange(names, 0, 2);
List<String> nameSubList = Arrays.asList(Arrays.copyOfRange(names, 0, 2));

System.out.println(Arrays.toString(partialNames));
System.out.println(nameSubList);

 Array 并集 与 交集

利用HashSet获取数据的并集与交集。

Integer[] firstArray = 0, 1, 2, 3, 4;
Integer[] secondArray = 3, 4, 4, 5, 6;
HashSet<Integer> unionSet = new HashSet();
unionSet.addAll(Arrays.asList(firstArray));
unionSet.addAll(Arrays.asList(secondArray));

System.out.println(unionSet); // [0, 1, 2, 3, 4, 5, 6]

HashSet<Integer> intersectionSet = new HashSet();
intersectionSet.addAll(Arrays.asList(firstArray));
intersectionSet.retainAll(Arrays.asList(secondArray));

System.out.println(intersectionSet); // [3, 4]

 Array 删除重复元素

利用LinkedHashSet删除数组重复元素。

LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>(Arrays.asList(secondArray));
System.out.println(linkedHashSet);

Integer[] secondArrayWithoutDuplicates = linkedHashSet.toArray(new Integer[] );
System.out.println(Arrays.toString(secondArrayWithoutDuplicates));

Array 浅拷贝与深拷贝

完整代码请戳?GitHub

利用 SerializationUtils 完成数组浅拷贝与深拷贝。

Employee[] employees = new Employee[2];
employees[0] = new Employee(100, "Ria", "Yang", new Department(1, "HR"));
employees[1] = new Employee(200, "Jeff", "Shen", new Department(2, "Finance"));

System.out.println(employees[0]);
System.out.println(employees[0].getDepartment());


// Shallow copy
Employee[] clonedEmployees = employees.clone();

// Deep copy
Employee[] copiedEmployees = SerializationUtils.clone(employees);


employees[0].setFirstName("Unknown");
employees[0].getDepartment().setName("Unknown");

// Verify the copied objects
System.out.println(clonedEmployees[0].getFirstName()); // Unknown
System.out.println(clonedEmployees[0].getDepartment().getName()); // Unknown

System.out.println(copiedEmployees[0].getFirstName()); // Ria
System.out.println(copiedEmployees[0].getDepartment().getName()); // HR

String基本操作

完整代码请戳?GitHub

split(...)

String str = "A,B,C,D";

String[] strArray = str.split(",");

System.out.println(Arrays.toString(strArray));

Pattern.split(...)

Pattern pattern = Pattern.compile(",");
String[] strArray2 = pattern.split(str);

System.out.println(Arrays.toString(strArray2));

String.join(...)

String[] elements = "Learn", "to", "be", "happy";
String strElement = String.join(" ", elements);

System.out.println(strElement);

Byte[] to String

String data = "Being a happier programmer.";
byte[] byteArray = data.getBytes();

String strFromByte = new String(byteArray);
String strFromByte2 = new String(byteArray, Charset.defaultCharset());

System.out.println("byteArray: " + byteArray); // byteArray: [B@7852e922
System.out.println("Arrays.toString(byteArray): " + Arrays.toString(byteArray)); // Arrays.toString(byteArray): [66, 101, 105, 110, 103, 32, 97, 32, 104, 97, 112, 112, 105, 101, 114, 32, 112, 114, 111, 103, 114, 97, 109, 109, 101, 114, 46]

System.out.println("strFromByte: " + strFromByte); // strFromByte: Being a happier programmer.

System.out.println("strFromByte2: " + strFromByte2);// strFromByte2: Being a happier programmer.

System.out.println("Charset.defaultCharset(): " + Charset.defaultCharset()); // Charset.defaultCharset(): UTF-8

Learned from HowToDoInJava, happy learning yeah!

以上是关于Java Collections - Array 和 String 常用函数的主要内容,如果未能解决你的问题,请参考以下文章

Java Collections - Array 和 String 常用函数

Java容器---Arrays & Collections工具类

Java排序小问题 (怎么改成Collections.sort(liste lements)排序)

更高效地刷OJ——Java中常用的排序方法,Array.sort(),Arrays.parallelSort(), Collections.sort()

java.lang.Collections

回忆Java的Collections