Skip to content

Instantly share code, notes, and snippets.

@charsyam
Created June 18, 2018 02:32
Show Gist options
  • Save charsyam/0813fb4f898e8cd5ff4180c77bcba067 to your computer and use it in GitHub Desktop.
Save charsyam/0813fb4f898e8cd5ff4180c77bcba067 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.io.FileWriter;
class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
public class MapTest {
public static void main(String [] args) {
Map<String, User> map = new HashMap<>();
User user1 = new User("name1", "[email protected]");
User user2 = new User("name2", "[email protected]");
User user3 = new User("name3", "[email protected]");
User user4 = new User("name4", "[email protected]");
map.put(user1.getName(), user1);
map.put(user2.getName(), user2);
map.put(user3.getName(), user3);
map.put(user4.getName(), user4);
for (Map.Entry<String, User> entry : map.entrySet()) {
String key = entry.getKey();
User tmpUser = entry.getValue();
System.out.println(key + " : " + tmpUser.getName() + " : " + tmpUser.getEmail());
}
try {
FileWriter fw = new FileWriter("D:\\user.txt");
for (String key : map.keySet()) {
User tmpUser = map.get(key);
String line = tmpUser.getName() + " " + tmpUser.getEmail();
System.out.println(line);
fw.write(line + "\r\n");
}
fw.close();
} catch(Exception e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment