We help IT Professionals succeed at work.

JAVA unique string username Array or list or ArrayList

yarek
yarek asked
on
4,955 Views
Last Modified: 2013-11-23
I am newbie to JAVA
I need to maintain a list of UNIQUE connected usernames. I guess something like  this snippet:

users = new something; // array ? Lists ? Iterator ?

function check_user_exist(newUserName:String):boolean; {
  if (users.indexOf(newUserName)>0 ) retrun true else return false;
// something like this ? This is DELPHI like !
}
The Goal is to maintain a array (lists ? Objects ? ArrayLists ?) of UNIQUE usernames, and CHECK if the username is already present in this array

userName are Strings and not Objects


Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
public void add(String name)
{
   if (!list.contains(name))
   {
        list.add(name);
   }
}

Author

Commented:
ok
But how to declare list ?
list=new WHAT ?

Commented:
if you use a Set you dont need to check if it is availabe because Set automatically wont insert if it has already inserted

users  = new HashSet<String>()

but please note this is only works if you add string if you want to add any other object you should implement two methods "equals()" and "hashCode()" so the Sit structure will work fine

Commented:
Set users = new HashSet();

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>connected usernames

What do you mean by that?

Commented:
I think he means the users that authenticated by his system and a session has been created for them

Commented:
for a simple sample;

public static void addUser(ArrayList list, String user) {              
  if (!list.contains(user))
     list.add(user);
}
public static boolean checkUser(ArrayList list, String user) {
   return (list.contains(user) ? true : false);
}
public static void main(String[] args) {
  ArrayList list = new ArrayList();
  addUser(list, "xxx");
  if (checkUser(list, "xxx"))
    System.out.print("yes");
   else
     System.out.print("no");
}
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
private List<String> list = new ArrayList<String();

public void add(String name)
{
   if (!list.contains(name))
   {
        list.add(name);
   }
}

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.