1

I have a chatprogram which contains users and channels. My next quest is to get a list of channels which one user is in. How should this be done?

Here are the codes as of now:

ChatFrontImpl:

private Hashtable<String, ArrayList<String>> channels = new Hashtable<String, ArrayList<String>>();
private ArrayList<Client> clients;
public synchronized boolean registerClient(Client client, String password) throws RemoteException {
if(!u.logIn(client.findName(), password)){
 System.out.println("Wrong username or password!");
 return false;
}
if (!clients.contains(client)) {
 try {
 clients.add(client);
 updateJlist();
 System.out.println(client.findName() + " registered.");
 } 
 catch (Exception e){
 System.out.println("error in method registerClient(): " + e);
 }
 return true;
}else
 return false;
} 
public void connectChannel(String username, String channel) throws RemoteException{ 
 if(isUserRegistered(username)){
 if (!channels.containsKey(channel)) {
 String message = "User " + username + " entered the channel";
 channels.put(channel, new ArrayList<String>());
 channels.get(channel).add(username);
 notifyChannelSystem(channel, "SYSTEM", message);
 notifySelf(username, "Write /? for avaliable commands");
 }
 else{
 if(channels.get(channel).contains(username)){ 
 } 
 else {
 channels.get(channel).add(username);
 String message = "User " + username + " just entered the channel";
 notifyChannelSystem(channel, "SYSTEM", message); 
 }
 }
 }
}
asked Jan 19, 2012 at 12:20
3
  • Can you expand your question to include the definition of the variable kanal please. The programme makes sense if kanal should actually be channel. Commented Jan 19, 2012 at 12:32
  • oh, the code is usually in norwegian, but i translated(obviously just parts of it). The variable should be channel wherever it says kanal ofc :) Commented Jan 19, 2012 at 12:49
  • I corrected the variable + changed the method to void, as i dont need returns really Commented Jan 19, 2012 at 12:51

2 Answers 2

1

I'd use a different data structure - but assuming that you wish to continue with this one (in order to answer the question):

public List<String> getChannelsForUsername(String username) {
 List<String> userChannels = new ArrayList<String>();
 for (String channel : channels.keySet()) {
 if (channels.get(channel).contains(username)) {
 userChannels.add(channel);
 }
 }
 return userChannels;
}
answered Jan 19, 2012 at 12:30

3 Comments

Are you sure this works? Shouldn't i be using enumeration to go through the channels list?
public List<String> getChannelsForUsername(String username) { List<String> userChannels = new ArrayList<String>(); for (Enumeration e = channels.elements() ; e.hasMoreElements() ;) { if (channels.get(channels).contains(username)) { userChannels.add(channels); } } return userChannels; } Shouldn't it be something like this? I'm getting an awful error tho. I'm not allowed to use .add(channels). But i can add username, which is kinda odd
The foreach loop will go over anything that's Iterable, and channels is a HashMap. I'll update my answer to iterate over the keys of the Map - sorry about that!
1

Have a map

private HashMap<Client, channlesList> clientsAndRooms;
private ArrayList channels = ArrayList <channel>();

I don't know why you have hashTable there, I would try to avoid it.

while connecting user to a channel

1) check if hasmap already has that user You can get keySet and do Contains. If there, get channelList and new channel to this list and save to map again. 2) If this is first channel,

channelList.add(channel);
clientAndRooms.put(userName,channelList);

Note: There may be syntax errors, I just typed it here.

answered Jan 19, 2012 at 12:29

4 Comments

I bet this structure is better than mine?
If the hashtable is bad for business i might try to change to this code
Hashtables are synchronised HashMaps. Given that you're going to be affected by concurrency, I would stick to Hashtable or move to ConcurrentHashMap.
@brainzzy: I agree. If concurrency is the case, still I prefer ConcurrentHashMap.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.