在多个ArrayLists中搜索整数量

时间:2018-05-25 15:37:35

标签: java list arraylist

我一直在考虑这个问题,但我还没有找到解决方案。

我有9个ArrayLists,可以有1到10个整数。

我想做的是: 查找2个数字(从1到10),发生在完全 2这些ArrayLists中(必须是相同的数组。例如,数字2和3都只出现在List1和List中)4。最好也知道这些数字出现在哪些ArrayLists中。

感谢您提前提供任何帮助

1 个答案:

答案 0 :(得分:0)

一种方法:

首先:创建一个Map<Integer, BitSet>。称之为m。 下一步:遍历您拥有的9 ArrayLists。对于您在数组#n中遇到的每个数字a

m.putIfAbsent(n, new BitSet(9));  // Make there the number has a BitSet to start
m.get(n).set(a);  // Set the bit indicating number n is in array a

之后,您只需要在BitSets Map cardinality() == 2BitSet.equals()查找两个com.ibm.mq.allclient-9.0.5.0.jar com.ibm.mq.jar javax.jms-api-2.0.1.jar 并且import java.util.Enumeration; import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueBrowser; import javax.jms.QueueConnection; import javax.jms.Session; import javax.jms.TextMessage; import org.springframework.stereotype.Component; import com.ibm.mq.jms.MQQueueConnection; import com.ibm.mq.jms.MQQueueConnectionFactory; import com.ibm.msg.client.wmq.WMQConstants; @Component public class TestMessage { MQQueueConnection mqQueueConnection = null; public static void main(String[] args) { try { TestMessage messageUtil = new TestMessage(); messageUtil.connect(); System.out.println(messageUtil.messageExists("queuename", "searchvalue")); } catch (Exception e) { e.printStackTrace(); } } public final String className = "TestMessage"; public QueueConnection connect() { String methodName = "connect"; MQQueueConnection connection = null; try { String hostname = "xxxxxx";//Replace with correct value String channel = "xxxxx";//Replace with correct value String queueManager = "xxxx";//Replace with correct value Integer port = 0;//Replace with correct value MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory(); connectionFactory.setHostName(hostname); connectionFactory.setPort(port); connectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT); connectionFactory.setQueueManager(queueManager); connectionFactory.setChannel(channel); connection = (MQQueueConnection) connectionFactory.createQueueConnection(); connection.start(); mqQueueConnection = connection; } catch (JMSException jmse) { jmse.printStackTrace(); } return connection; } public boolean messageExists(String queueName, String searchValue) { boolean returnValue = false; QueueConnection connection = mqQueueConnection; Session session = null; QueueBrowser browser = null; int i = 0; try { session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); Queue queue = session.createQueue(queueName); browser = session.createBrowser(queue); Enumeration<TextMessage> en = browser.getEnumeration(); while (en.hasMoreElements()) { TextMessage message = (TextMessage) en.nextElement(); i++; if (message.getText().contains(searchValue)) { System.out.println(message); System.out.println(message.getText()); returnValue = true; break; } } System.out.println("finished"); } catch (Exception jmse) { jmse.printStackTrace(); } catch (Error jmse) { jmse.printStackTrace(); } finally { try { if (browser != null) { browser.close(); } if (session != null) { session.close(); } if (connection != null) { connection.close(); } } catch (Exception jmse) { jmse.printStackTrace(); } } return returnValue; } } 相等。