0
class IdPwPair {

  private String pw = "";
  private String id = "";

  //Constructor

  //getter & setter

 }

That is because I want to put a large number of IDs and their passwords into an array.

And, I may want to add a field accountType, then Map won't work anymore.

bijiDango
  • 1,385
  • 3
  • 14
  • 28
  • may be you can to use char[] for pw,check http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords – upog Nov 06 '13 at 12:41
  • Variables should be in lowerCamelcase. Passwords should be stored as `char[]`. – Boris the Spider Nov 06 '13 at 12:43
  • @BoristheSpider Why is using a `char[]` suggested ? – An SO User Nov 06 '13 at 12:45
  • I corrected it. Forum made me a little nervous. I know HashMap can do the thing. However I don't feel good with ArrayList>. Then the question becomes, is HashMap the common solution? – bijiDango Nov 06 '13 at 12:50
  • 1
    @LittleChild this is for security reasons. If you use a `String` then it is immutable - even if you deference the `String` it will be on the heap/in the pool. If you use a `char[]` you can zero it when you're done thereby removing the password from memory. – Boris the Spider Nov 06 '13 at 12:51
  • @BoristheSpider Yeah I looked for it. Found `Jon Skeet`'s answer. – An SO User Nov 06 '13 at 12:52
  • @BoristheSpider I see your point. – bijiDango Nov 06 '13 at 12:55

4 Answers4

4

Yes, it makes sense to create class and later uses it as list<class>... better is to use char[] for password to get benefits of security, processing and manipulating purposes..

Imran
  • 5,376
  • 2
  • 26
  • 45
  • 1
    Would I be correct in saying `char[]`s are better for passwords because it is easier to control when they are destroyed whereas a String can hang around before being garbage collected? – Richard Tingle Nov 06 '13 at 12:49
  • 1
    @RichardTingle Yes, that is the thinking behind the recommendation. – Boris the Spider Nov 06 '13 at 12:51
  • 1
    Yes, agree with you... and manipulation and processing is also easy on password e.g. making some encrypt/decrypt logic etc... – Imran Nov 06 '13 at 12:52
  • 1
    I think it would be helpful to explain _why_ this is a good idea. – TwoThe Nov 06 '13 at 13:01
1

My suggestion for a class would be as follows:

class IdPwPair {
  final public String id, password;

  public IdPwPair(String id, String password) {
    this.id = id;
    this.password = password;
  }
}

Explanation

What you want to design is a data-holder class. It is strongly recommended to use the final keyword here and not add a setter, because this class does not control the data, only hold it together.

This not only makes accessing the data easier (and faster), it as well allows for a much simpler code structure. Instead of having to track when and where you change the content of a dataset, you just use them and throw them away if they are no longer valid. This ensures that a given set of data at any time either holds the exact one set of information, or doesn't exist anymore. Not even a chance to mess up here by accidentally changing the data while someone else was still using it.

Note: There are security concerns for keeping passwords in JVM memory, because someone could live-hack the JVM and pull out the passwords. So if you are working on a software where this is a concern (which I doubt), you should use char[] instead and overwrite the content once it is no longer used.

TwoThe
  • 13,879
  • 6
  • 30
  • 54
0

You may want to use an ArrayList<String> for both Pw and Id

or

HashMap<String,String>which will map each id to its password.

Using a char[] is preferred for storing passwords because you can explicitly wipe the data after you are done even before GC comes in.

An SO User
  • 24,612
  • 35
  • 133
  • 221
0

First of all use lowercase start as follows

  private String pw = "";
  private String id = "";

creating a class is depends on your requirement. you can use your HashMap<String,String> for this.

 Map<String,String> idOverPassword= new HashMap<String,String>();
 idOverPassword.put("id","password");
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115