collections

Sorting a HashMap, while keeping duplicates

寵の児 提交于 2019-12-24 00:33:50
问题 I'm trying to sort a HashMap in two ways. The default way: alphabetically by the value, the second way: numerically by the key, with the higher number being at the top. I have searched around but can't find anything on the subject, and what I do find, doesn't work. If it's not possible to sort both of them (I want the person with the highest key at the top, decreasing as people have lower keys, then alphabetically sort all of the rest (the people with 0 as their key). Here's what I've tried

Java ListSet somewhere?

烂漫一生 提交于 2019-12-24 00:30:05
问题 Looking for a insertion order collection that also allows efficient querying and subset views of positions (like sublist). Seems the most straightforward option for this would be to take the linked list approach of List, embed the nodes as map values and expose part or all of the list interface on the class. Would someone bitch to Oracle about this? Having NavigableMap/Set added for sorted maps and sets and not having the far more common insertion order equivalent... edit: please don't

How do aggregate operations work on Java streams?

浪尽此生 提交于 2019-12-24 00:26:55
问题 In the following excerpt from the Java tutorials in aggregate operations, we map the names of the people to their sexes. Map<Person.Sex, List<String>> namesByGender = roster .stream() .collect( Collectors.groupingBy( Person::getGender, Collectors.mapping( Person::getName, Collectors.toList()))); I understand that the collect operation: 1) Groups each Person in the stream by the result of getGender. 2) Maps each Person to the result of getName. 3) Forms a list from the results and. 4)

How to choose the output collection type in Seq.map?

元气小坏坏 提交于 2019-12-23 23:34:39
问题 I'd like to implement something like: def f(s: Seq[Int]): Vector[String] = s.map(_.toString).toVector But I'd like it to create directly the output Vector without executing the map first, making whatever Seq, before copying it into a Vector. Seq.map takes an implicit canBuilFrom parameters which induces the collection output type. So I tried s.map(...)(Vector.canBuildFrom[String]) which gives the error: found : scala.collection.generic.CanBuildFrom[Vector.Coll,String,scala.collection

Search collection and retrieve model backbonejs

随声附和 提交于 2019-12-23 19:09:18
问题 I am trying to search a collection for a model attribute and then grab and return the entire model ? var myModel = Backbone.Model.extend({ defaults: { a: '', b: '', c: '', d: '', e: '' } }); My collection has around 100 of myModels. I am trying to search through the collection by a , find it and then return the entire myModel of a so I can access the other attributes ? 回答1: If I understand your question correctly, you want to use the where method on Backbone collections, here in the docs:

contains() method is not working for Arrays.asList in java

不问归期 提交于 2019-12-23 18:23:23
问题 I have a string object that looks like: String color = "black, pink, blue, yellow"; Now I want to convert it into an array and find a color. Something like this: boolean check = Arrays.asList(color).contains("pink"); Which always gives false. Can anyone help me with this? 回答1: Your string variable color is not an array, so first of all you need to create array from that string variable with split(String dilemeter) method and create ArrayList from splitted string, like this: List<String>

Java double entry table

坚强是说给别人听的谎言 提交于 2019-12-23 18:13:56
问题 Does anyone know a double entry table implementation in Java I can download ? I need to do something like this 1 2 3 _______ a| x y z b| h l m c| o a k table.get(a,1) would return x Of course, it should use any Object as key, value, etc 回答1: There are two basic approaches, depending on your needs. One is to make a Hashtable (or similar) of Hashtable s. Hashtable<Integer, Hashtable<String, String>> = ...; Another approach is to build your own datatype that represents an (Integer, String) pair,

How to improve this function?

戏子无情 提交于 2019-12-23 18:06:41
问题 Suppose I've got a data structure like that: case class B(bx: Int) case class A(ax: Int, bs: Seq[B]) I am writing a function A => Seq[(Int, Option[Int])] as follows: def foo(a: A): Seq[(Int, Option[Int])] = if (a.bs.isEmpty) Seq((a.ax, None)) else a.bs.map(b => (a.ax, Some(b.bx))) It seems working but I don't like the branching. How would you improve foo ? 回答1: Another option - add an auxiliary function that takes a Seq[T] and returns a Seq[Option[T]] where the output is never empty - if the

How to merge two collections into one

杀马特。学长 韩版系。学妹 提交于 2019-12-23 17:39:02
问题 I have two collections created within Silverlight each collection is created at separate times by different methods. Method 1 creates a List<> titled person ( contains fields first name, last name age) Method 2 created a List<> titled Phone ( contains phone number, cell number) Is there a way within SL to combine these two Lists into one Collection that could then be bound to the view for display? Example: combined into a collection that contained all properties (first name, last name age,

Collections.sort method sometimes throws ConcurrentModificationException in multithreaded environment . List is not being modified structurally

喜欢而已 提交于 2019-12-23 17:16:25
问题 package CollectionsTS; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; public class ArrayListTS { public static void main(String[] args) { HashSet<Integer> hset = new HashSet<Integer>(); for (int i = 0; i <= 1000; i++) { hset.add(i); } MyRunnable mr = new MyRunnable(); mr.addElements(hset); Thread t1 = new Thread(mr,"t1"); Thread t2 = new Thread(mr,"t2"); Thread t3 = new Thread(mr,"t3"); t1.start(); t2.start(); t3.start(); } } class