Set<String> phoneNumbers = new HashSet<>(); for (Contact c : phone_contacts) { phoneNumbers.add(c.getPhone()); } Set<String> overlappingNumbers = new HashSet<>(); for (Contact registered : registered_users) { if (phoneNumbers.contains(registered.getPhone())) { overlappingNumbers.add(registered.getPhone()); } } List<Contact> common_contacts = new ArrayList<>(); for (Contact contact : phone_contacts) { if (overlappingNumbers.contains(contact.getPhone())) { common_contacts.add(contact); } } }
Map<Long, String> hm = new HashMap<>(); final Contact c = ...; Optional<Long> idOpt = hm.entrieSet().stream() .filter((e) -> e.getValue().equals(c.getPhone()) .map((e2) -> e2.getKey()) .findAny(); idOpt.ifPresent(id -> System.out.println(id)); long id = idOpt.orElse(-1L);
HashMap hm = new HashMap(); for (Contact ru : registered_users) { hm.put(ru.getPhone(), ru.getId()); } if(hm.containsKey(c.getPhone())) { registered_phone_contacts.add(new Contact(hm.get(c.getPhone()), c.getName()); ^^ // value = hm.get(c.getPhone()) }
/** * Constructs a new instance initialized with the values from the specified metadata object. * This is a <cite>shallow</cite> copy constructor, since the other metadata contained in the * given object are not recursively copied. * * @param object the metadata to copy values from, or {@code null} if none. * * @see #castOrCopy(Contact) */ public DefaultContact(final Contact object) { super(object); if (object != null) { hoursOfService = object.getHoursOfService(); contactInstructions = object.getContactInstructions(); if (object instanceof DefaultContact) { final DefaultContact c = (DefaultContact) object; phones = copyCollection(c.getPhones(), Telephone.class); addresses = copyCollection(c.getAddresses(), Address.class); onlineResources = copyCollection(c.getOnlineResources(), OnlineResource.class); contactType = c.getContactType(); } else { phones = singleton(object.getPhone(), Telephone.class); addresses = singleton(object.getAddress(), Address.class); onlineResources = singleton(object.getOnlineResource(), OnlineResource.class); } } }
/** * Constructs a new instance initialized with the values from the specified metadata object. * This is a <cite>shallow</cite> copy constructor, since the other metadata contained in the * given object are not recursively copied. * * @param object the metadata to copy values from, or {@code null} if none. * * @see #castOrCopy(Contact) */ public DefaultContact(final Contact object) { super(object); if (object != null) { hoursOfService = object.getHoursOfService(); contactInstructions = object.getContactInstructions(); if (object instanceof DefaultContact) { final DefaultContact c = (DefaultContact) object; phones = copyCollection(c.getPhones(), Telephone.class); addresses = copyCollection(c.getAddresses(), Address.class); onlineResources = copyCollection(c.getOnlineResources(), OnlineResource.class); contactType = c.getContactType(); } else { phones = singleton(object.getPhone(), Telephone.class); addresses = singleton(object.getAddress(), Address.class); onlineResources = singleton(object.getOnlineResource(), OnlineResource.class); } } }