This little annoyance caused me to lose an hour of sleep, and I don't understand why.
I have an ArrayList array which I want to iterate over and conditionally remove items. ... |
- Should I define a hashmap with something like "A" , new Character("A")
- should I use something like a sorted list ? or cast it into an int?
- simple list? (that doesnt seem ...
|
(Using MyBatis v3.0.4.)
I have a problem that I do not know how to solve. My object model is:
Location.java
public class Location {
// ... other content
...
|
I'm needing to store a link structure. As a hierarchy, for example, x link, links children of x, y link, links children of y. Resorting to the use of java object ... |
In my application I use 3rd party library (Spring Data for MongoDb to be exact).
Methods of this library return Iterable<T>, while the rest of my code expects Collection<T>.
Is there any utility ... |
let say we have a collection of collections which results in a table data structure.
Collection<Collection<T>> table;
Which is most efficient way you would use to translate the "rows" into "columns" using Java?
EDIT:
val_11 ... |
I came across something new which i really found hard to understand.. Here is what i have done and it works perfectly fine..
Vector<String[]> v = new Vector<String[]>();
...
|
|
I would like to store information in a java collection that is cached in memory.
I have the following table
less than $1 - (store some data)
$1 to $115,000 - (Store some different ... |
In my Java code I often use the very handy method(Class... args) varargs. As far as I know, they allow you to pass any amount of Class objects or an array ... |
I have couple interesting Java collections in my hand such as:
- http://code.google.com/p/guava-libraries/
- Java 7
- Java 7 concurrent collections
- Scala collections
- Homegrown collections that we have at some company
I wonder what would be the best ... |
If I am going to create a Java Collection, and only want to fill it with elements, and then iterate through it (without knowing the necessary size beforehand), i.e. all ... |
Out of curiosity, I wrote an own simple implementation of Set for a special case (where the set of all possible entries is fixed, but it's no enum). It was actually ... |
... at least not "official".
You can easily google two or three "WeakList" example implementation and for "WeakSet" one very good can be found in NetBeans Platform API sources.
I read ... |
Can any one suggest me which collection to use for this scenario:
Every student has a payment history with details of payment made by the student / family. The system should ensure ... |
As per the title, are there any smells surrounding the provision of a setter that accepts a List instantiation for assignment to a instance variable?
i.e.
public class Test{
private ...
|
I have 90 IDs that I need to something like on the image below. I want the last ID to be popped first and if there are new IDs added to ... |
I have recently started a automated testing project using selenium 2.0. The api feels very low level and I'm looking to refactor some of the common stuff like form handling ... |
Are there "tagged" collections in Java? To clarify, I want to essentially classify each element in the list with some sort of Object (e.g. a String) so I can reference different ... |
I have a server that sends bytes back to a client app, when the client app receives a finished response from the server i want to gather the bytes before the ... |
I have Many-to-Many associated entities information with me.
I would like to show the user the list of "students" and if user chooses an student, show his teachers.
Conversely, user may opt to ... |
|
I have a java program which goes like this
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
pq.add(10);
...
|
|
I have a question for java GC. I know that there is minor collection for young generation and major collection for tenured generation and normally the size of YG is smaller ... |
In Java, is there a short elegant way to combine multiple predicates (Guava Predicate) into one?
Currently, I have some list of predicates:
Collection<Predicate<TypeA>> preds = ...;
And I have some code that loops ... |
I'm wondering if there is a quick/clean way to get the difference between two sets ?
I have:
Set<String> s1 = new HashSet<String>();
s1.add("a");
s1.add("b");
s1.add("c");
Set<String> s2 = new HashSet<String>();
s2.add("b");
I need something like:
Set<String> diff = Something.diff(s1, ...
|
I have a problem with aliasing in XStream.
I've got a set of String items that I would like to serialize to XML like this:
<types>
<type>abc</type>
<type>def</type>
</types>
However, I can't seem ... |
Should I replace a simple setter
public void setCategories(Set<String> categories)
{
this.categories = categories;
}
with something like this:
public void setCategories(Collection<String> categories)
{
this.categories.clear();
if (categories != ...
|
for (Event e : pq)
doesn't iterate in the priority order.
while(!pq.isEmpty()){
Event e = pq.poll();
}
This works but empties the queue.
|
This is kind of a Java trivia question perhaps.
I have used the Stack implementation many times.
I have read that this is considered a legacy class ... |
1.) Are the quantities limited to only 1, 2 and 3 ? 2.) Where is the total cost going to be displayed? Create an action by extending the AbstractAction class. The ... |
Kevin, This sounds suspiciously like a test question. If not, look at the dictionary definition of "composite", and the JavaDoc for java.util.Collection. The difference should be obvious. Cheers! Oralloy |
Hi I'm new to the whole CORBA scene and have written a .idl whilch contains 2 structs (Struct1 and Struct2). I want to have a dynamic collection of Struct1's in Struct2. I have searched the internet with no success. My first though was to include java.util.List in the .idl somehow but didn't have luck with that. Read up about enums but ... |
Hi All, I want to know more about fast fail behavior of collection objects.I know due to this ,if i try to chage my collection object while iterating on it throws ConcurrentModificationException.But if it is not allowed then how is it possible to update the underlying collection object (i mean reduce its size) using iterator objects' remove() method which removes the ... |
Pradeep, Suppose I have a the Dog-Animal-Object hierarchy. Obviously it makes sense to call collectionOfAnimals.containsAll(collectionOfDogs). However, it also makes sense to call collectionOfDogs.containsAll(collectionOfAnimals). I may not know what is in my collectionOfAnimals. It is entirely possible that it contains only dogs (or is empty) and then the call makes sense. Another reason is consistency. It must be valid to call collectionOfDogs.containsAll(collectionOfObjects) ... |
Hello People, I want to compare two Directory, which have a big capacity about 1 GB(until 20 GB) for one Directory, what should i do to make the code effectively ? Should i use Vector or another Java Collection(Set,List,Map,HashMap,etc) to store the Files temporary. The Files come from another network. I need any Hint or Idea, thanks |
|
which collection to use (Java in General forum at JavaRanch) A friendly place for programming greenhorns! Register / Login Java Forums Java Java in General which collection to use Post by: Christopher Arthur, Ranch Hand on Sep 15, 2006 11:29:00 I have a need for a special collection which doesn't seem to fit perfectly in any of the existing ... |
Trying to get a multi-threaded program concepted that opens as much as half the open file handles the underlying os will allow. This pretty much limits us to use of Collections to run efficiently. Today in test run I drilled down to java.lang.ClassCastException: java.util.Collections$SynchronizedMap at Belvedere.WordCount.fetter(WordCount.java:521) at Belvedere.WordCount.run(WordCount.java:387) fetter is just some name I came up with that is not likely ... |
Is the only requirement that the collection should survive a crash? Why not serialize the collection and read it once at load? Probably you can use Externalizable to modify how you serialize your collection. Is it so that some other process can also modify the file? If not, then every read should not translate to an I/O operation(Read once and cache), ... |
There is nothing wrong with keeping this Set as an instance variable of the servlet, providing that it doesn't hold data specific to a particular request. That would be the simplest "cache" - you would want to provide some mechanism to avoid changing it while a request is actually using it. Bill |
|
I call a stored procedure that returns about 9000 rows of information which consist of a part number and part description that I use to populate a dropdown select box in a form. Instead of calling the procedure and returning 9000 rows each time the page is displayed, how can I cache this collection of values that gets returned to my ... |
|
|
The values will be constant most of the time. However, the possibility exists that they could change in the database. When they do change in the database, there will need to be a 'refresh' mechanism in place that can be manually invoked in the event it is needed (i.e. for the static var approch mentioned above, there could be an accompanying ... |
I have a List which is a collection of some objects. Each of the objects is a map which has some columns and values. I need to build two arrays of the columns and values which will have all the columns and values in the individul objects of the list in the same order. Does the following work? for (i=0;i |
Originally posted by Paul Sturrock: The examples you supply are easy: When to use Vectors? Never When to use Hashtables? Again, never. Both are legacy collections. General guidelines for what to use when are given in the Collections Framework docs that come with the API. There is also a tutorial which covers this at Sun. I would disagree with ... |
Hi, I need a method that would change the keys and values of the Properties instance so that keys become values and vice versa. I wrote my own version, but I think there must be already such a method somethere in Collections and I want to use it. Properties pp = new Properties(); //Inverse Values and keys public void inverseValues(){ set ... |
The standard Collection classes have a "removeAll()" method which accepts another Collection as an argument; all the items in the first collection which appear in the second one are removed. So what you could do (if you need to preserve the full collection) is to construct a copy of it, and then use removeAll(). |
I had written a class using Vector, Hashtable, TreeMap with adding elements first using Vector class and passing the Vector instance to Hashtable as the value and key being specified in the put() method so that every unique key will have a group of values. Then I pass the Hashtable instance to the TreeMap for sorting the keys and written seperate ... |
I have no idea what the ? means. This punctuation is not commonly used in Java. In fact, the only place it's possible to use ? is with the ternary operator which means there must also be a :. You don't typically see this used very often. I'm sorry that I don't know how to help since I don't even understand ... |
Hashtable and Vector are the original, synchronized collections. Even if you need a synchronized collection, I'd skip those and either use ArrayList/HashMap wrapped using Collections.synchronizedList/Map() or a collection specifically coded to do better synchronization. For example, JDK 1.5 has ConcurrentHashMap which allows multiple readers and only locks out other threads when writing. It has better multithreaded performance under average conditions (reads ... |
Without knowing the context its a little hard to say, but I suspect "collection" is probably being used to define a data structure used to store objects without the writer wanting to make a specific reference to the Collection interface (which defines a particular type of collection). So an array could be considered a collection, but not a Collection. Understand? |
Hi All, Prior to Tiger, Java did not allow primitives to be used in Collection. Can anybody provide the reason why this was done and why it is changed now? Also mostly when we use Collection, we use like Collection C = new ArrayList(); What is wrong if I say, ArrayList alist = new ArrayList(). Why should I use the interface ... |
Hi frnds.. Hey i just need one small help. Could u please tell me that which collection object i have to use so that while retriving i can get the data in sorted order and just let me know suppose while retriving the data when it gives in sorted data is it dependent on the key of the map. I need ... |
Except for WeakHashMap, are unused keys in HashMap and TreeMap also garbage-collected ? What's "fillratio" in HashMap(int capacity, float fillratio) ?......how can we use it in our programs...any example program ??....PLEASE!!! As per their functionality and use in program how do Set and Map differ?.....for e.g we have TreeSet - TreeMap, HashSet-HashMap....! How to decide appropriate use of the collection frameworks ... |
Hashtable, HashMap and TreeMap are map implementations these collection classes should be used when you need to store data in key/value pairs. Unlike the other two classes TreeMap is a Sorted map , sorts the element in the natural order. Hash Map & Hashtable are Unsorted & Unordered , Difference between Hashmap & hashtable is Hash map allows one null key ... |
|
Hi when i was executed this program i got this error . Can any one Help me with Regards import java.util.*; class SectionLevel { private level Levelobject[]; public void initialise(int i) { Levelobject = new level[i]; // reference are created for(int j = 0; j < i; j++) { Levelobject[j] = new level(); } } public void addQid(int i,int qid) { ... |
Hi all, I need an array to put Car objects Car is a class that extends JPanel This array will be accessed severe times by some classs So searching is involved The size of the array in unknown. So must be growable. And finally 10 entirely separate threads will access this array. This 10 threads will be started like t1.start(); t2.start(); ... |
A Set would work, but it would take too much work to make it work. A Set doesn't have direct access to its members, so you wouldn't be able to detect that the member already existed then pull it out and modify it very easily. What I typically do is something along these lines: //getKey returns some identifying property, a part ... |
|
I have a list that has some elements. What I will have is some elements are all the same except one item that is a String is different. In this case I want to concatenate the Strings of these similar items and remove the element. Here is some code I have now. It isn't working correctly. Iterator iterator = list.iterator(); Object ... |
If an ArrayList object containing references to XYZ objects is set to null then all the objects i.e. ArrayList and XYZ objects would be eligible for garbage collection. Only way to access the elements of the ArrayList is to create new references before destroying the old ones. As a result if we store a reference to the first object in the ... |
Hello sir, friends, mam, I heard that its a good practice that while using collection api you must have an interface of collection type on the left hand side of the assignment operator. Concrete implementations should be used only while making instances. Example (Correct approach) List sList = new Vector(); (Wrong approach) Vector sVector = new Vector(); This is what I ... |
I have a Map object which contains an Arraylist of Street objects. One of the propertys of the Street Object is streetName with a getter & setter. I have a large map object that gets populate with 15000 streets.. I noticed many of the streets have the same name (and also long names). since I'm serializing the object, I decided that ... |
Hey Folks, This is my first post to this forum;I'm a new guy here. I want to learn more about implementations of Collections in java. I'm looking for some examples in java which illustrate all the 'Collections' concepts. Also, could you please tell me when and how they are to be used in website design ? Thanks, Sanjeev |
I have a problem of accessing a specific collection which is included inside a collection object I created. For example, I have a collection of numbers ranges 0-99,100-199,200-299,...This 0-99 is equal to one collection, So therefore I have created a Collection of Collections of numbers. What I want is, I want to get the specific collection on that Collection object. Anyone ... |
Here is the requirement I have to split a collection into n collections each of a given size. Here is the code, I came up with. I am guessing there is a better way to do this or is there a Collection Utility that does it for me. I looked but could not find it (did a google search and Java ... |
when we use ArrayList, when we use Vector, When we use TreeMap, when we use HashMap, when we use Hashtable, when we use Arrays. 1. Never use Vector; always use ArrayList instead. Vector is an old class, left over from Java version 1.1. The main difference between Vector and ArrayList is that Vector is synchronized, while ArrayList is not. That makes ... |
I use a hashtable which contains a String as a key and an arraylist as a value. This hashtable is iterated on the jsp and first the string is displayed, then another iteration takes place for the arraylist for the string key and hence, submenu type of stuffs gets displayed. Now, i want to display a particular String and its arraylist ... |
|
hi friends, I am having a file containing million of words per line and i hav to find the duplicate words in it with their occurence. So i am using tree set collection but after storing around 5,00,000 of words , it gives me error .. running out of heap space... class WordType implements Comparable{ String word=null; int no_Of_Occur=0; List list=null; ... |
|
|
|
I have some data in a flat file like the following : date1 date2 date3 id1 10 12 13 id2 11 12 9 id3 7 9 4 I need to store this in a way so that a value can be easily retrieved by calling something like get(id1,date3), which should return 13. I am currently thinking off something like this Hashmap |
The structure of a Map is different from that of the other collections. A map is a collection but technically speaking Map is not a collection since it does not inherit from the Collection interface. Some methods may not make sense for Maps like add(Object o). This is because of the structure of Maps. |
Hi Guys, I was just working on my program based on collections, However when i run the program successively without making any changes in the code,....even then i get different results in my the console. I am using jdk 1.6 and Eclipse 3.1 Following is the code:- -------------------------------------- ListExample.java -------------------------------------- package dev.data.workSpace.projects.scjp.section9; import java.util.Collection; import java.util.List; public class ListExample { /** ... |
|
Not still clear what you are asking for. If you are asking for comparing the elements in two lists and getting the common elements out, here goes the idea public class Test { public static void main(String args[]) { List list1 = new ArrayList(); list1.add("Element1"); list1.add("Element2"); list1.add("Element3"); list1.add("Element4"); List list2 = new ArrayList(); list2.add("Element10"); list2.add("Element2"); list2.add("Element3"); list2.add("Element14"); Set commonElements = new ... |
Hi All, Thanks a lot. I have created a Vector as final but still i can able to insert new values from which I came to know that because we are not changing its object reference it is allowing. But how to make it unmodifiable w.r.to values also. Please help me out Regards, Sree |
I would think the question is really about what to consider when you are copying something - the difference between a deep and a shallow copy. Lets say I have an ArrayList of Schools that I wish to copy. Each school is composed of Students, Classrooms, etc, etc - do I want to copy the students and classrooms too? Each classroom ... |
|
Hi Problem Statement: Locale-> Value ____________________________ en-Us-> one,two,three fo-fr -> x,y,z I have to make one function like this: public boolean find(String locale, String value). IF i will pass find(en-Us,one)->true else false i have done like this /* * Language.java * * Created on October 17, 2007, 10:26 AM * * To change this template, choose Tools | Template Manager * ... |
Hi, Which collection should I use (and I am limited to java 1.4) which will support my requirement - I need map functionality it that I need to be able to get a boolean return if a given key is already in the map PLUS I need to then get the index of that key (I need to be able to ... |
Hai, please see the below problem..could you please explain Given: 1. public class Drink implements Comparable { 2. public String name; 3. public int compareTo(Object o) { 4. return 0; 5. } 6. } and: 20. Drink one = new Drink(); 21. Drink two = new Drink(); 22. one.name= Coffee; 23. two.name= Tea; 23. TreeSet set = new TreeSet(); 24. set.add(one); ... |
I want to have a HashTable or a HashMap or some other Collection object in my application. I am not able to decide which collection object will meet my requirements. Can anyone please help me which collection construct can I use? The Considerations are, 1.Should be readale as fast as possible 2.It should able to grow(upto 1 million or more) and ... |
In an institute following information is maintained about the students: Student Class Roll Number (Unique identifier of the student) First Name Last Name Year Of Study Activity Choice For each of the following scenarios, specify which of the four core collection interfaces will be most appropriate and why. If you feel, more than one interface is suitable, you can mention all ... |
am facing issue on how to store and then retrieve data . In my program which is simple java class i am retrieving data at 3 places from an xml. i need to store this temporarly in particular fashion as data is related and needed to be used later on at end program to generate report. first data i am retireving ... |
Hi Friends, Objective: To take a txt file as input and produce an output file which states which word repeated in the file how many times. Constraints: We can not use String class for that and have to use Collectons only. My current approach: I took the characters of the file in the ArrayList and tried to create another ArrayList which ... |
Hello , Am trying to understand the load factor concept . Have a couple of questions regarding the same :: Is load factor only applicable to Maps ? Is it applicable to List ? Also was reading this article :: http://www.oracle.com/technology/pub/articles/maps1.html An excerpt :: The load factor itself is a tuning tradeoff between space and time. Smaller load factors will take ... |
|
|
|
Hi this is Kajol, Can somebody expalin me this program.... i couldnt get it import java.util.*; class Mouse { private int mouseNumber; Mouse(int i) { mouseNumber = i; } // Magic method: public String toString() { return "This is Mouse #" + mouseNumber; } void print(String msg) { if(msg != null) System.out.println(msg); System.out.println( "Mouse number " + mouseNumber); } } class ... |
Dear Friend Extremely ...... You know it happens. here is the code....... zzzzzzzzz import java.util.*; class extiterator implements Iterator { public boolean hasNext() { return true; } public Object next() { return (new Object()); } public void remove() { } } public class collection implements Collection { public static void main(String args[]) { collection c=new collection(); String s[]={"1","2","3"}; String[] x=new String[s.length]; ... |
Hi I wanted to store set of information like employee number, employee name, and employee address what class in the collection framework do i have to use so that when i search on the employee number, it would give me the employee name and address, could somebody pls help me with some code. Thanks Chris |
I have looked at HashTable, trees, etc for my situation but having a hard choosing which one is the best choice. My data might look like this each entry has: Social Security Number <- unique Company Number <- unique Job <- not-unique Age <- not-unique Now I need to be able to search Social Security Number or Company Number get the ... |