Detailed explanation of the three traversal methods of Map in Java
Three traversal methods of map!
A very important operation of collections---traversal. I have learned three traversal methods. Each of the three methods has its own advantages and disadvantages~~
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cn.tsp2c.liubao; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; /** * * @author Administrator */ public class TestMap { public static void main(String[] args) { Map<String, Student> map = new HashMap<String, Student>(); Student s1 = new Student("宋江", "1001", 38); Student s2 = new Student("卢俊义", "1002", 35); Student s3 = new Student("吴用", "1003", 34); map.put("1001", s1); map.put("1002", s2); map.put("1003", s3); Map<String, Student> subMap = new HashMap<String, Student>(); subMap.put("1008", new Student("tom", "1008", 12)); subMap.put("1009", new Student("jerry", "1009", 10)); map.putAll(subMap); work(map); workByKeySet(map); workByEntry(map); } //最常规的一种遍历方法,最常规就是最常用的,虽然不复杂,但很重要,这是我们最熟悉的,就不多说了!! public static void work(Map<String, Student> map) { Collection<Student> c = map.values(); Iterator it = c.iterator(); for (; it.hasNext();) { System.out.println(it.next()); } } //利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!! public static void workByKeySet(Map<String, Student> map) { Set<String> key = map.keySet(); for (Iterator it = key.iterator(); it.hasNext();) { String s = (String) it.next(); System.out.println(map.get(s)); } } //比较复杂的一种遍历在这里,呵呵~~他很暴力哦,它的灵活性太强了,想得到什么就能得到什么~~ public static void workByEntry(Map<String, Student> map) { Set<Map.Entry<String, Student>> set = map.entrySet(); for (Iterator<Map.Entry<String, Student>> it = set.iterator(); it.hasNext();) { Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next(); System.out.println(entry.getKey() + "--->" + entry.getValue()); } } } class Student { private String name; private String id; private int age; public Student(String name, String id, int age) { this.name = name; this.id = id; this.age = age; } @Override public String toString() { return "Student{" + "name=" + name + "id=" + id + "age=" + age + '}'; } }
For more detailed explanations of the three traversal methods of Map in Java, please pay attention to PHP Chinese. net!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
