java集合类的相关转换

下面的的案例,基本上是以代码为主,文字的描述较少,后期有时间会继续添加。

ArrayToList public void ArrayToList() { System.out.println("ArrayToList"); Object[] objects = new Object[]{"纽约", "伦敦", "法兰克福", "东京", "多伦多"}; //方法一 List<Object> list = new ArrayList<>(Arrays.asList(objects)); System.out.println(list); //方法二 System.out.println("使用java8特性的方法"); List<Object> list1 = Stream.of(objects).collect(Collectors.toList()); System.out.println(list1); } ArrayToMap public void ArrayToMap() { System.out.println("ArrayToMap"); Object[] objects = new Object[]{"纽约", "伦敦", "法兰克福", "东京", "多伦多"}; Map<Object, Object> map = new HashMap<>(); int i = 1; for (Object o : objects) { map.put(i++, o); } System.out.println(map); } ArrayToSet public void ArrayToSet() { System.out.println("ArrayToMap"); Object[] objects = new Object[]{"纽约", "伦敦", "法兰克福", "东京", "多伦多"}; //方法一 Set<Object> set = new HashSet<>(Arrays.asList(objects)); System.out.println(set); //方法二 System.out.println("使用java8特性的方法"); Set<Object> set1 = Stream.of(objects).collect(Collectors.toSet()); } ListToArray public void ListToArray() { System.out.println("ListToArray"); List<String> list = new ArrayList<>(); list.add("布朗克斯"); list.add("布鲁克林"); list.add("曼哈顿"); list.add("皇后区"); list.add("斯塔滕岛"); //方法一 String[] strings = list.toArray(new String[list.size()]); System.out.println(Arrays.toString(strings)); //方法二 System.out.println("使用java8特性的方法"); String[] strings1 = list.stream().toArray(String[]::new); System.out.println(Arrays.toString(strings1)); } ListToMap

新建一个Book类(model)

class Book { private String name; private Integer releaseYear; private String isbn; @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", releaseYear=" + releaseYear + ", isbn='" + isbn + '\'' + '}'; } public Book(String name, int releaseYear, String isbn) { this.name = name; this.releaseYear = releaseYear; this.isbn = isbn; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getReleaseYear() { return releaseYear; } public void setReleaseYear(int releaseYear) { this.releaseYear = releaseYear; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } }

主要方法

public void ListToMap() { System.out.println("ListToMap"); Map<Object, Object> map = new HashMap<>(); List<String> list = new ArrayList<>(); list.add("布朗克斯"); list.add("布鲁克林"); list.add("曼哈顿"); list.add("皇后区"); list.add("斯塔滕岛"); //方法一 for (int i = 0; i < list.size(); i++) { map.put("第" + i + 1 + "区", list.get(i)); } System.out.println(map); //方法二 List<Book> bookList = new ArrayList<>(); bookList.add(new Book("平凡的世界", 2004, "9787020048045")); bookList.add(new Book("战争与和平", 1989, "9787020014071")); bookList.add(new Book("百年孤独", 1775, "9787215069657")); for (int i = 0; i < bookList.size(); i++) { System.out.println(bookList.get(i).toString()); } Map<String, Integer> map1 = bookList.stream().collect(Collectors.toMap(Book::getName, Book::getReleaseYear)); System.out.println(map1); } ListToSet public void ListToSet() { System.out.println("ListToSet"); Set<Object> set = new HashSet<>(); List<String> list = new ArrayList<>(); list.add("布朗克斯"); list.add("布鲁克林"); list.add("曼哈顿"); list.add("皇后区"); list.add("斯塔滕岛"); for (Object i : list) { set.add(i); } System.out.println(set); } MapToArray public void MapToArray() { System.out.println("MapToArray"); Map<String, String> map = new HashMap<>(); map.put("甘肃", "兰州"); map.put("陕西", "西安"); map.put("新疆", "乌鲁木齐"); map.put("青海", "西宁"); map.put("宁夏", "银川"); Object[][] strings = new Object[map.size()][2]; Iterator iterator = map.entrySet().iterator(); for (int i = 0; i < map.size(); i++) { Map.Entry entry = (Map.Entry) iterator.next(); strings[i][0] = entry.getKey(); strings[i][1] = entry.getValue(); } for (Object[] objects : strings) { for (Object objects1 : objects) { System.out.print(objects1 + " "); } System.out.println(); } } MapToList public void MapToList() { System.out.println("MapToList"); Map<String, String> map = new HashMap<>(); map.put("甘肃", "兰州"); map.put("陕西", "西安"); map.put("新疆", "乌鲁木齐"); map.put("青海", "西宁"); map.put("宁夏", "银川"); List<Object> objects = new ArrayList<>(map.keySet()); System.out.println(objects); //list里面放置list List<List<Object>> lists = new ArrayList<>(); for (Object o : map.keySet()) { List<Object> objects1 = new ArrayList<>(); objects1.add(o); objects1.add(map.get(o)); lists.add(objects1); } System.out.println(lists); } MapToSet public void MapToSet() { System.out.println("MapToSet"); Map<String, String> map = new HashMap<>(); map.put("甘肃", "兰州"); map.put("陕西", "西安"); map.put("新疆", "乌鲁木齐"); map.put("青海", "西宁"); map.put("宁夏", "银川"); Set<Object> objects = new HashSet<>(map.keySet()); System.out.println(objects); } SetToArray public void SetToArray() { System.out.println("SetToArray"); Set<Object> set = new HashSet<>(); set.add("北京"); set.add("上海"); set.add("深圳"); set.add("广州"); Object[] objects = set.toArray(new Object[set.size()]); System.out.println(Arrays.toString(objects)); } SetToList public void SetToList() { System.out.println("SetToList"); Set<Object> set = new HashSet<>(); set.add("北京"); set.add("上海"); set.add("深圳"); set.add("广州"); List<Object> list = new ArrayList<>(set); System.out.println(list); } SetToMap public void SetToMap() { System.out.println("SetToMap"); Set<Object> set = new HashSet<>(); set.add("北京"); set.add("上海"); set.add("深圳"); set.add("广州"); Map<Object, Object> map = new HashMap<>(); Iterator iterator = set.iterator(); for (int i = 0; i < set.size(); i++) { map.put(i, iterator.next()); } System.out.println(map); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfgww.html