找到关于 Java 8 的4330 篇文章
131 次浏览
首先,让我们创建一个 Java Map 并进行初始化 −Map map = new HashMap(); map.put(1, "Tom"); map.put(2, "John"); map.put(3, "Kevin"); map.put(4, "Jacob"); map.put(5, "Ryan"); 现在,将 Map 转换为 List −ArrayList key = new ArrayList(map.keySet()); ArrayList value = new ArrayList(map.values()); 示例 下面是将 Map 转换为 List 的 Java 程序 −import java.util.HashMap; import java.util.ArrayList; import java.util.Map; public class Demo { public static void main(String args[]) { Map map = new HashMap(); map.put(1, "Tom"); map.put(2, "John"); map.put(3, "Kevin"); map.put(4, "Jacob"); map.put(5, "Ryan"); ... 阅读更多
92 次浏览
假设我们的字符串如下所示 −String str = "My String"; 现在,将上述字符串转换为 IntStream −IntStream stream = str.chars(); 示例 下面是 Java 中将字符串转换为 IntStream 的程序 −import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { String str = "My String"; System.out.println("String: " + str); IntStream stream = str.chars(); System.out.println("IntStream..."); stream.forEach(System.out::println); } } 输出 String: My String IntStream... 77 121 32 83 116 114 105 110 103
101 次浏览
首先让我们创建一个 IntStream −IntStream stream = "Ryan".chars(); 现在,将此 IntStream 转换为字符串 −String str = stream.collect(StringBuilder::new,StringBuilder::appendCodePoint,StringBuilder::append).toString(); 示例 下面是 Java 中将 IntStream 转换为字符串的程序 −import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { IntStream stream = "Ryan".chars(); String str =stream.collect (StringBuilder::new,StringBuilder::appendCodePoint,StringBuilder::append).toString(); System.out.println("String (IntStream to string) = " + str); } } 输出 String (IntStream to string) = Ryan
699 次浏览
首先创建一个 HashMap −Map map = new HashMap(); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("4", "Four"); map.put("5", "Five"); map.put("6", "Six"); 现在,将上述 HashMap 转换为 TreeMap −Map treeMap = new TreeMap(); treeMap.putAll(map); 示例 下面是 Java 中将 HashMap 转换为 TreeMap 的程序 −import java.util.*; import java.util.stream.*; public class Demo { public static void main(String args[]) { Map map = new HashMap(); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("4", "Four"); map.put("5", "Five"); map.put("6", "Six"); ... 阅读更多
132 次浏览
装箱数组是以对象形式而不是原始类型定义的数组。 示例 下面是 Java 中将装箱数组转换为流的程序 −import java.util.*; import java.util.stream.*; public class Demo { public static void main(String args[]) { String arr[] = { "Laptop", "Mobile", "Notebook", "Desktop" }; System.out.println("Array = "+ Arrays.toString(arr)); Streams = Stream.of(arr); System.out.println("Stream (array to stream) = "+ Arrays.toString(s.toArray())); } } 输出 Array = [Laptop, Mobile, Notebook, Desktop] Stream (array to stream) = [Laptop, Mobile, Notebook, Desktop]
734 次浏览
首先,让我们设置一个字符串的 ArrayList −ArrayList arrList = new ArrayList(); arrList.add("Bentley"); arrList.add("Audi"); arrList.add("Jaguar"); arrList.add("Cadillac"); 现在,使用 toArray() 将其转换为字符串数组 −int size = arrList.size(); String res[] = arrList.toArray(new String[size]); 示例 下面是 Java 中将字符串的 ArrayList 转换为字符串数组的程序 −import java.util.*; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); arrList.add("Bentley"); arrList.add("Audi"); arrList.add("Jaguar"); arrList.add("Cadillac"); arrList.add("Mazda"); arrList.add("Land Rover"); arrList.add("Porsche"); ... 阅读更多
482 次浏览
首先,设置一个包含字符串值的列表 −List myList = new ArrayList( Arrays.asList("One", "Two", "Three", "Four")); 现在,使用 String.join() 将它们设置为逗号分隔的列表 −String str = String.join(", ", myList); 示例 下面是 Java 中将字符串转换为逗号分隔列表的程序 −import java.util.*; public class Demo { public static void main(String args[]) { List myList = new ArrayList(Arrays.asList("One", "Two", "Three", "Four")); System.out.println("List = " + myList); // 逗号分隔 String str = String.join(", ", myList); System.out.println("String (Comma Separated) = " ... 阅读更多
506 次浏览
首先,创建一个字符串集合 −Set setStr = new HashSet(Arrays.asList("osCommerce", "PrestaShop", "Magento", "Wordpres", "Drupal")); 现在,使用 toArray() 方法将其转换为字符串数组 −String[] arrStr = setStr.toArray(new String[0]); 示例 下面是 Java 中将字符串集合转换为字符串数组的程序 −import java.util.Arrays; import java.util.Set; import java.util.HashSet; public class Demo { public static void main(String[] args) { Set setStr = new HashSet( Arrays.asList("osCommerce", "PrestaShop", "Magento", "Wordpres", "Drupal")); System.out.println("Set of String: " + setStr); String[] arrStr = setStr.toArray(new String[0]); System.out.println("Array ... 阅读更多
614 次浏览
假设我们的 ArrayList 如下所示 −List arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan"); 现在,使用 toCollection() 将此 ArrayList 转换为 LinkedList −List myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new)); 示例 下面是 Java 中将 ArrayList 转换为 LinkedList 的程序 −import java.util.*; import java.util.stream.*; public class Demo { public static void main(String args[]) { List arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan"); System.out.println("ArrayList = " + arrList); List myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new)); System.out.println("LinkedList (ArrayList to LinkedList) = " + myList); } } 输出 ArrayList = [John, Jacob, Kevin, Katie, Nathan] LinkedList (ArrayList to ... 阅读更多